HELP: Cast as Type VS 2003

  • Thread starter Thread starter AMDRIT
  • Start date Start date
A

AMDRIT

I would like to cast an object to a value type specified by a variable of
Type

Function ReturnTest(InputVar as Object) as Object

Dim DataType as Type = GetType(System.String)

If TypeOf (InputVar) Is DataType Then
Return CType(InputVar, Datatype)
End If

End Function

I have tried gettype, system.type.gettype and I am getting no where fast.
Anyone have any ideas what I am doing wrong?
 
You are going nowhere, to be honest.

The bottom line, your function is declared as returning an Object. No matter
what casts you do, it will always return Object.

So even if CType could do what you are trying to make it tdo (which I
believe is not possible), it still would not matter because every return of
your function will be seen as an Object, no matter what.
 
Isn't what I was attempting referred to as Unboxing? Doens't the signature
for the value get set to the desired type? Is everything actually an
object?

example:

Private Sub Testing

Dim dblValue As Double
dblValue = 123
Debug.Print(String.Format("Raw {0}", TypeName(dblValue)))
Debug.Print(String.Format("ReturnTest {0}",
TypeName(ReturnTest(dblValue))))
Debug.Print(String.Format("ReturnTest1 {0}",
TypeName(ReturnTest1(dblValue))))
Debug.Print(String.Format("ReturnTest2 {0}",
TypeName(ReturnTest2(dblValue))))

End Sub

Function ReturnTest(ByVal InputVar As Object) As Object
Return CType(InputVar, Integer)
End Function

Function ReturnTest1(ByVal InputVar As Object) As Object
Return CType(InputVar, String)
End Function

Function ReturnTest2(ByVal InputVar As Object) As Object
Return CType(InputVar, Double)
End Function

Output

Raw Double
ReturnTest Integer
ReturnTest1 String
ReturnTest2 Double
 
In reality, you maybe returning a string, or an ArrayList or whatever.
However, as far as the return type, it is always boxed as an object. As long
as the return type is declared as Object, as far as anything calling the
method is concerned, it is getting an object back. That object can then be
unboxed to whatever it actually is, but as far as the compiler is concerned,
your function is returning something of type Object.
 
I can see how it confuses you... it's a confusing subject.

Whenever you assign a value (such as the number 15) to an object, it gets
'boxed' automatically. So simply calling this function, which assigns the
value to the parameter InputVar, will automatically box it for you -- you
don't have to do anything else.

Having a function that boxes is actually not necessary. All you need to do
is make sure that the variable you're assigning the value to is of type
object, and it's a done deal.

You may find this article of use:
http://www.codersource.net/csharp_boxing_unboxing.html

Even though it's C# the concepts are the same.

Hope that helps,
John
 
Admrit,

Is this what you want to do?
\\\
test("hello")
test(1)

Public Sub test(ByVal obj As Object)
If TypeOf obj Is String Then
MessageBox.Show(obj.ToString)
Else
MessageBox.Show("I am not a string")
End If
End Sub
///

I hope this helps,

Cor
 
Back
Top