1

I tried to convert an Object variable into a StreamWriter. But, it does not work. What is the mistake ?

StreamWriter file = (StreamWriter) myObject;
Steam
  • 9,368
  • 27
  • 83
  • 122
  • 3
    Well it's a `System.Object` - it's not a `System.IO.StreamWriter`. Where did you get it from, and why did you *expect* to be able to cast it to `StreamWriter`? – Jon Skeet Nov 06 '13 at 18:51
  • myObject is actually an SSIS variable which was obtained using the code `Object myObject = Dts.Variables["yourStreamWriterHere"].Value;` Again, don't worry about that here. Lets look at the c# instead. – Steam Nov 06 '13 at 18:51
  • @JonSkeet - My answer is given below. – Steam Nov 06 '13 at 19:04

4 Answers4

3

The value of myObject is not (convertable to) a StreamWriter.

Rik
  • 28,507
  • 14
  • 48
  • 67
3

Try this:

if (myObject is StreamWriter) 
{
    var file = (StreamWriter) myObject as StreamWriter;
}
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
  • This works regardless of whether or not `myObject` is `null`. However, in C# there is a difference between an object that is `null` and an object that is uninitialized. As such, the above works if `myObject` has been declared and initialized via `object myObject = null` but not if it has just been declared via `object myObject;` without initialization. In your case, I assume that `myObject` is a parameter (of type `object`) to a method and that a `StreamWriter` was passed. So long as that `StreamWriter` has been initialized, even as `StreamWriter sw = null;`, the above will work without error. – Zachary Kniebel Nov 06 '13 at 19:32
2

Another moment of my stupidity. I had commented out the line that does this - Object myObject = Dts.Variables["yourStreamWriterHere"].Value; So, there was nothing inside myObject. Is there a null there ? In Java it is. Also, java gives you a nice nullPointerException. But, visual studio gives you a cryptic "Micro$lop™: Cannot find the cause of error. Contact Billy for more help. Keep your credit card handy just in case. Call center waiting times vary". Just irritating.

Anyway, the real error you get is -

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'System.IO.StreamWriter'.
   at ST_ffrrefr939322939392dfr.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Steam
  • 9,368
  • 27
  • 83
  • 122
  • Yes, this would be a null if you didn't assign it. You don't get a NullReferenceException because there is no derefencing going on. – Rik Nov 06 '13 at 19:00
  • @blasto : yes then it surly contains null, and when you are invoking any function on it throws null refrence Exception – Sudhakar Tillapudi Nov 06 '13 at 19:02
  • @blast - In C#, objects that are `null` actually do have a type; You can cast `null` to any type that you want. For example, you can pass null as a parameter of a method, so long as the null that you pass is of the same type as said parameter of the method. – Zachary Kniebel Nov 06 '13 at 19:07
  • @ZacharyKniebel - why is that allowed ? – Steam Nov 06 '13 at 19:07
  • @blasto dereferencing is what happens when you try to do something with the value of a reference. In your example, myObject is a reference to an object. If you were to call `myObject.ToString()`, for example, you would dereference the reference and because it was referencing null, you would get the NullReferenceException. – Rik Nov 06 '13 at 19:13
  • @blasto - Imagine that you have a method that initializes a given `out` parameter by attempting to parse some string and doing some other logic. The method's call could then take a form like the following: `SomeClass sc = null; InitializeSC("someString", out sc);`. Subsequently, the code could check to see if `sc` was initialized or if it is still `null`, and do something to handle the result of that check, etc. Some actual examples of this pattern are the `TryParse` methods for `Int32` and `DateTime`. – Zachary Kniebel Nov 06 '13 at 19:14
1

Before converting, check if it can be cast to a StreamWriter object by using the is keyword, as below:

if(myObject is StreamWriter)
{
//can be cast
}
else
{
//can not be cast
}
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67