CType wants a literal as type?

  • Thread starter Thread starter FB's .NET Dev PC
  • Start date Start date
F

FB's .NET Dev PC

I'm trying to create a general-purpose routine to update a value in a data
location on a remote server. The client-server protocol is OPC, but that
isn't immediately important to my question. The part that is important is
that the value passed to the server has to be the correct data type. I am
trying to convert the input (as an object) to the correct data type, said
type being read from the server immediately prior to update.

The MSDN description states "Expression as object, type as object", so I
tried this:

Sub OPCWrite(ByVal Tag As IDN.IO.IOTag, ByVal Value As Object)
Dim LocVal As Object = Tag.Value
Dim ValType As Object = Tag.DataType
LocVal = CType(Value, ValType)
Tag.Write(LocVal)
End Sub

That yielded a "ValType is not defined", so I tried this:

Sub OPCWrite(ByVal Tag As IDN.IO.IOTag, ByVal Value As Object)
Dim LocVal As Object = Tag.Value
Dim ValType As String = Tag.DataType.ToString
LocVal = CType(Value, ValType)
Tag.Write(LocVal)
End Sub

which yielded the same error. Is there a way to accomplish a generic
type-switch in VB .NET 2003?

Thanks
Fred Bourdelier
Database Admin
Tucson AZ
 
I'm trying to create a general-purpose routine to update a value in a data
location on a remote server. The client-server protocol is OPC, but that
isn't immediately important to my question. The part that is important is
that the value passed to the server has to be the correct data type. I am
trying to convert the input (as an object) to the correct data type, said
type being read from the server immediately prior to update.

The MSDN description states "Expression as object, type as object", so I
tried this:

It kinda lies. CType isn't really a function, it's a compile-time
directive and therefore needs a type literal as the second parameter so
that it knows what kind of code to produce.
Sub OPCWrite(ByVal Tag As IDN.IO.IOTag, ByVal Value As Object)
Dim LocVal As Object = Tag.Value
Dim ValType As Object = Tag.DataType
LocVal = CType(Value, ValType)
Tag.Write(LocVal)
End Sub

I'm not quite grokking what you're trying to accomplish. Tag.Value is
dimmed as an object, presumably, so there's really very little point in
assigning it to a different object reference.

I guess what I'm missing here is the following: What exactly is Value?
That is, what Type is it really (I assume it's not *really* an object).
Also, what's Tag.Value doing in the code above?

if Value really *is* the datatype specified by Tag.DataType, then the
above code isn't going to do anything useful. You may as well write
"Tag.Write(Value)". On the other hand, is Value merely guaranteed to be
some type that is convertible into Tag.DataType (guaranteed by whom, and
why didn't the caller simply pass the right type)?

Another way to put the question is this? Are you trying to achieve
dynamic casting or dynamic conversions? Dynamic casting isn't really
useful, and general dynamic conversions aren't part of the framework.
 
Fred,
As the other have stated, CType is a compile time directive, in that the
type parameter needs to be the name of a type.


You should be able to use Convert.ChangeType, with limitations,
Convert.ChangeType generally will not work on classes that you define unless
they support the IConvertible interface...
Sub OPCWrite(ByVal Tag As IDN.IO.IOTag, ByVal Value As Object)
Dim LocVal As Object = Tag.Value
Dim ValType As System.Type = Tag.DataType

LocVal = System.Convert.ChangeType(Value, ValType)
Tag.Write(LocVal)
End Sub

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Ctype 4
Quick Ctype Question 3
CType question 3
Class boxed in an object 2
Bad Code 2
Is this a loop? 1
Dual Serial Ports 3
How to call BeginInvoke for a sub that has a parameter ? 3

Back
Top