[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Runtime Type Casting .. need help

Posted on 2004-09-28
10
Medium Priority
?
299 Views
Last Modified: 2010-04-23
I have some code that looks like this:

               Dim mytype As Type = Type.GetType(table.Columns(map.DataColumnName).DataType.ToString())
               datarow.Item(map.DataColumnName) = CType(row(IntegerIndex(map.TextColumnIndex)), mytype)

But that doesn't work. It says "mytype" is not defined. I think the problem has to do with the fact that the CType method only works with design time values.

I need to cast at runtime this object: row(IntegerIndex(map.TextColumnIndex))
to a type that I will only know at runtime. The type that I want to cast it to is the underlying datatype of the strongly typed datatable.

Can someone help me? This code without the CType is blowing up when

row(IntegerIndex(map.TextColumnIndex)) = "0"

and the datatable column type = "System.Boolean"

It seems this should be possible, and quite frankly, I am amazed that I am stuck on this.
0
Comment
Question by:gregasm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12174991
CType(row(IntegerIndex(map.TextColumnIndex)), mytype)

This is not the way to use CTYPE(), the second parameter is the name of the type, this garantee the compiler the type of object you are trying to convert can be converted.

If you are having problem with some types you have to use a "select case"

Although is weird you are getting an error with this:

row(IntegerIndex(map.TextColumnIndex)) = "0"

and the datatable column type = "System.Boolean"

"0" is false and non zero is true, now if for some reason you assign "s", you will get an error.

But the compiler try to make the conversion to the new type if posible. otherwise you will get an error.

I don't recommend you to leave the compiler make the conversions, since you can get unexpected results in some types.
0
 
LVL 18

Assisted Solution

by:DotNetLover_Baan
DotNetLover_Baan earned 400 total points
ID: 12175298
Did you you try this ?
CType(row(IntegerIndex(map.TextColumnIndex)), GetType(mytype))

-Baan
0
 
LVL 8

Author Comment

by:gregasm
ID: 12175318
Hi wguerram,

The select case approach is what I ahve been working with.

can you throw out a skeleton of the select case for me to have a look at?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:gregasm
ID: 12175810
Yes, I think I tried that. The compiler doesn't like that expression though because it doesn't know the value at design time. Therefore, it kind of defeats one of the purposes of strong typing.

CType(row(IntegerIndex(map.TextColumnIndex)), GetType(mytype))

What is mytype? It's not known until runtime...

i have a feeling tomorrow i'm gonan write something like this:

select case table.Columns(map.DataColumnName).DataType.ToString()

case "System.Boolean"

.....

case "System.String"

case "System.Double"

case "This.Approach.Sucks"

case else

end select
0
 
LVL 10

Assisted Solution

by:123654789987
123654789987 earned 800 total points
ID: 12178047
U need not use Ctype for this. It is working well without it

Dim mytype As Type = Type.GetType(table.Columns(map.DataColumnName).DataType.ToString())
               datarow.Item(map.DataColumnName) =mytype


For the System.Boolean problem

Loop through the column and convert the 0, 1 values

u can use CBool to convert it to boolean
0
 
LVL 8

Accepted Solution

by:
wguerram earned 800 total points
ID: 12179551
Here is a list of conversion functions

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrptypeconversion.asp

----------------------------
You already have it, now just use the proper function to make the conversion.

select case table.Columns(map.DataColumnName).DataType.ToString()

case "System.Boolean"

datarow.Item(map.DataColumnName)  = CBoll(row(IntegerIndex(map.TextColumnIndex)))

case "System.String"

case "System.Double"

case "This.Approach.Sucks"
         'There is no an equivalent function for this...

case else
    datarow.Item(map.DataColumnName)  = row(IntegerIndex(map.TextColumnIndex))
end select
0
 
LVL 8

Author Comment

by:gregasm
ID: 12181548
Oops. I meant to accept the answer from wguerram. But the points are distributed as I wanted them at least. Thanks guys.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12181776
You can change it

http://www.experts-exchange.com/help.jsp#hi17

Post a question with zero points and add the link to the question.
0
 
LVL 8

Author Comment

by:gregasm
ID: 12182159
Alrightie.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question