473,774 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ChangeT ype 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 14349
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*****@discus sions.microsoft .com> wrote in message
news:71******** *************** ***********@mic rosoft.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.ChangeT ype 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.ChangeT ype(value, <xxxx>.GetType( ));

HTH
Stoitcho Goutsev (100) [C# MVP]

"Demorsy" <De*****@discus sions.microsoft .com> wrote in message
news:28******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message
news:28******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message
news:28******** *************** ***********@mic rosoft.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******** *****@TK2MSFTNG P15.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*****@discus sions.microsoft .com> wrote in message
news:28******** *************** ***********@mic rosoft.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******** *****@TK2MSFTNG P15.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*****@discus sions.microsoft .com> wrote in message
news:28******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP15.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

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

Similar topics

7
6299
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 passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
7
3541
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 necessary changes on the functions and recreated all my types and functions. It seemd to be all right, but the newly created functions won't work anymore.
51
4565
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 there something wrong in there? -------------------------------------------------------------------- Types A type is a definition for a sequence of storage bits. It gives the meaning of the data stored in memory. If we say that the object a is an
4
17684
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 to do a generic cast or conversion on the type? Here is sort of what I want to do: object MyFunc(Type T, String Str) { object o;
0
1408
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, I'm running into a bit of a problem with a specific field type in the database. It seems that fields of type TEXT (a kind of binary way of storing large amounts of text) generate the error "Unknown Target Conversion Type" when I try to bind...
13
12392
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
6656
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" has type "unknown" DETAIL: Proceeding with relation creation anyway. CREATE VIEW Or is this warning just noise in this case?
4
2113
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
7627
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
10267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.