473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

CType wants a literal as type?

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
Nov 21 '05 #1
3 2156
Try to put the ValType into a GetType() function

"FB's .NET Dev PC" <ch**********@hotmail.com> wrote in message news:%2******************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #2
On 2004-12-08, FB's .NET Dev PC <ch**********@hotmail.com> wrote:
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.

Nov 21 '05 #3
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

"FB's .NET Dev PC" <ch**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl... 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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joeri | last post by:
Hi, I'm struggling with this issue, maybe someone has already solved ik before. Here's the problem I Have : clsA witch is a base class clsB inherits from clsA clsC inherits from clsA clsD...
10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
by: Filip D'haene | last post by:
Hi, I want to make a function like CType but can't figure out how to do this. Function ConvertWhithoutException(ByVal Item As Object, ByVal aType As Type) As Object Dim output As Object =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.