• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 765
  • Last Modified:

cType function not dynamic

I am filling the rows of a datatable in a for loop and want to handle the type conversion for the different columns dynamically.  The problem I was running into is the cType function won't accept a type object as an object.  Here is the code I was trying


       Dim Val As Object
       Dim ColDataType As Type = ThisTable.Columns(CPI.PropName).DataType
       Val = CPI.EvaluatedVal(CType(objCont.Value.Obj, SldWorks.Feature), InDoc:=objCont.Value.ParentDoc)

      ThisRow.Item(CPI.PropName) = CType(Val, coldatatype)'<-------Compiler error  Type 'coldatatype' is not defined.

What is one to do?
0
Corey Scheich
Asked:
Corey Scheich
  • 4
  • 3
1 Solution
 
Bob LearnedCommented:
Are you using Option Strict On?

Does this work?
    ThisRow.Item(CPI.PropName) = Val

Bob

0
 
Corey ScheichDeveloperAuthor Commented:
With Option Strict Off when I did that it was throwing an exception when Val was returned as a numeric string and the type of the column was double.
0
 
Corey ScheichDeveloperAuthor Commented:
(I should add) Even when the String could be cast to a double
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
First of all, Val is a reserved keyword.  If a value is an empty string, and you need to convert it to a number, there 2 choices if you don't want to get an exception:

Val(numberString)
Double.TryParse

Bob
0
 
Corey ScheichDeveloperAuthor Commented:
So you can't use Ctype() when you don't know the type in advance?
0
 
Bob LearnedCommented:
No, I don't believe that you can.  At least I wouldn't know the workaround for the correct syntax.

Bob
0
 
LantraxCommented:
Has anyone figured this out?

I am have the same exact problem with the exact same functionality. I cna't find any solution.
0
 
Corey ScheichDeveloperAuthor Commented:
Lantrax,

To my knowledge you will  have to create Select case statement to handle the different type possibilities.  I think this is quite sad it would have been much more efficient to be able to use cType in this manner but it isn't possible to my knowledge.

Corey
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now