We help IT Professionals succeed at work.

cType function not dynamic

Corey Scheich
on
Medium Priority
779 Views
Last Modified: 2008-03-03
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?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you using Option Strict On?

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

Bob

Corey ScheichDeveloper

Author

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.
Corey ScheichDeveloper

Author

Commented:
(I should add) Even when the String could be cast to a double
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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
Corey ScheichDeveloper

Author

Commented:
So you can't use Ctype() when you don't know the type in advance?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
No, I don't believe that you can.  At least I wouldn't know the workaround for the correct syntax.

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Has anyone figured this out?

I am have the same exact problem with the exact same functionality. I cna't find any solution.
Corey ScheichDeveloper

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.