473,416 Members | 1,840 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,416 software developers and data experts.

How to do type conversion to unknown type?

I want to convert a string (or object) to a primitive type. But I don't know
the type to convert to at run time.

For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";

If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.

I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.

Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?

-------
Thanks
Demorsy
Nov 17 '05 #1
11 14223
Demorsy,

Any method that converts one type to another and doesn't spellout the the
type in its name (ChangeType vs. ToInt32) would return Object.

However the actuall type of the object in memory would be the type you
converted to.

I don't see any possible situation where the situatuation you describe could
happen, though.

Maybe I miss something but if you have:
int unknownType = 0;
And a string:
string str = "123";
You have to put *int* infront of unknownType otherwise you cannot compile.
Constructions like:
??? unknowType; are not acceptable in C#.

Unlike javascript for example where you decalre variables like:
var unknowType;
where you don't specify the type are not allowed in C#.

In all .NET languages If you don't want to (or can't) specify the type you
declare the variable of type Object: in this case you shouldn't have
problems using ChangeType method. But that also means you that don't want to
use anything more from the type than the Object class interface provides.

Stoitcho Goutsev (100) [C# MVP]

"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...I want to convert a string (or object) to a primitive type. But I don't
know
the type to convert to at run time.

For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";

If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.

I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.

Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?

-------
Thanks
Demorsy

Nov 17 '05 #2
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy
Nov 17 '05 #3
Demorsy,

I think I see now what your problem is. Unfortunately, AFAIK there is no
conversion that can help you with this. As I said all generic data
conversion methods that could exist would have return type Object.

When you populate the data table you need to create and add a DataRow. Even
if you have typed DataRow it inherits from the DataRow class untyped indexer
is still available.

I believe you can do.

myDataRow["some column name"] = Convert.ChangeType(value, <xxxx>.GetType());

HTH
Stoitcho Goutsev (100) [C# MVP]

"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy

Nov 17 '05 #4
Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code

--
Jonathan Allen
"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy

Nov 17 '05 #5
Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code

--
Jonathan Allen
"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy

Nov 17 '05 #6
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]

"Jonathan Allen" <x@x.x> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code

--
Jonathan Allen
"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy


Nov 17 '05 #7
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]

"Jonathan Allen" <x@x.x> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
Just build a case structure around the type.

if getType == String
// soem code
else if getType == Int32
// other code

--
Jonathan Allen
"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I'm not the one that create the instance of the unknownType.
Actually I'm receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy


Nov 17 '05 #8
My apologies, I missed that part.

--
Jonathan Allen
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]


Nov 17 '05 #9
My apologies, I missed that part.

--
Jonathan Allen
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]


Nov 17 '05 #10
Hi Demorsy,

I think the other guys' suggestion are reasonable. Currently for type
convertion we can not make it purely dynamically since the compiler need to
get the exact type info for compiling the code into underlying convert
instrunction. For your scenario, I'm afraid we're limited to using some
switch or if...else... block to do the convertion.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no rights.)
"Jonathan Allen" wrote:
My apologies, I missed that part.

--
Jonathan Allen
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]


Nov 17 '05 #11
Hi Demorsy,

I think the other guys' suggestion are reasonable. Currently for type
convertion we can not make it purely dynamically since the compiler need to
get the exact type info for compiling the code into underlying convert
instrunction. For your scenario, I'm afraid we're limited to using some
switch or if...else... block to do the convertion.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no rights.)
"Jonathan Allen" wrote:
My apologies, I missed that part.

--
Jonathan Allen
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
The question was:

"Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?"
--
Stoitcho Goutsev (100) [C# MVP]


Nov 17 '05 #12

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

Similar topics

7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
7
by: Együd Csaba | last post by:
Hi, I've a problem with some of my stored procs. My config is: RH7.1, Postgres 7.3.2 I had converted a few fields of a few tables from one type to another and after this I made all the...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
0
by: David P. Donahue | last post by:
I'm using C# to create an ASP .NET website backed by a MySQL database. One of the things I do often on the site is populate a DataList by binding it to a DataSet pulled from the database. However,...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
by: Ed L. | last post by:
On 7.4.6, is there any problem with defining one column of a view to be a string literal? For example ... $ psql -c "create view fooview as select 'bar' as footype" WARNING: column "footype"...
4
by: ranjeet.gupta | last post by:
Dear All Please check the below code: UINT8 MsgLength = 0; MsgLength = strlen((char *)msg); if ( MsgLength == 0 || MsgLength 64) {
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.