Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

casting

Hello,
I have a DataSocket object, a AutoRefresh control and a Gauge.
 
In the AutoRefresh event I have:
 
Gauge1.Value = (Double) dataSocket.Data.Value
 
However, this seems to cause a type casting error at run time.
 
Best regards,
Seb
0 Kudos
Message 1 of 6
(4,360 Views)
You could try,
Gauge1.Value = DataConverter.Convert<Double>(dataSocket.Data.Value);  (if you are using VS2005)
or
Gauge1.Value = (Double)DataConverter.Convert(dataSocket.Data.Value, typeof(Double)); (if you are using VS2003)

But, you need to be sure that what you are sending CAN be converted to Double. If you are sending a Double[], then this may not work. However, if you are sending an Int32 or some other primitive type and you want that to be converted to Double, DataConverter will take care of that for you.
In any case, you can just see
once what dataSocket.Data.Value.GetType() returns, and then change you code to typecast to that type and then get the double value from the typecasted dataSocket.Data.Value.
-Mahesh
National Instruments
0 Kudos
Message 2 of 6
(4,334 Views)
Hello again, How can I find out what data type the OPC server is sending ? Seb
0 Kudos
Message 3 of 6
(4,326 Views)
Will the dataSocket.Data.Value.GetType().ToString() work for you ?
-Mahesh
National Instruments
0 Kudos
Message 4 of 6
(4,290 Views)
GetType().ToString() returns
System.Int32

but casting to System.Int32 does not work


0 Kudos
Message 5 of 6
(4,286 Views)
Are you sure, dataSocket.Data.Value.GetType().ToString() returned System.Int32 and not System.Int32[] ?
As I said earlier, if you are not dealing with arrays, then simply let the DataConverter take care of that for you.
Gauge1.Value = DataConverter.Convert<Double>(dataSocket.Data.Value);

Is the above line also not working for you ?
-Mahesh
National Instruments
0 Kudos
Message 6 of 6
(4,270 Views)