473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert from string to Type?

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;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 16 '05 #1
4 17683
Try Convert.ChangeT ype

"Ken Varn" <nospam> wrote in message
news:ue******** ******@TK2MSFTN GP14.phx.gbl...
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;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #2
But you dont know the type? So dont you use a GetType() method?
"Ken Varn" <nospam> wrote in message
news:ue******** ******@TK2MSFTN GP14.phx.gbl...
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;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #3

"Ken Varn" wrote...
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;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}


It's not obvious what you're trying to accomplish. As what your method
returns only is of the type "object", you'll probably still need to cast it
anyway after the call.

As you still have to cast the *result*, you actually would'nt even have to
supply the numeric type. You could try something simple like:

static decimal MyParse(String str)
{
return decimal.Parse(s tr);
}

// Bjorn A

Nov 16 '05 #4
Simply:

object MyFunc(Type T, String Str)
{
return Convert.ChangeT ype(Str, T);
}

The Convert.ChangeT ype() method tries to retrieve an IConvertible interface
for the first argument and then calls the respective conversion method.

HTH,
Stefan

"Ken Varn" <nospam> wrote in message
news:ue******** ******@TK2MSFTN GP14.phx.gbl...
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;

o = // Want to convert Str to be of type T here.

return o; // Want to return an object of Type T here that has the
converted value.
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 16 '05 #5

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

Similar topics

4
25343
by: John Cotsell | last post by:
Basically I have a class called Dog and when using the objectdatasource I can use a string parameter to pass into the selectmethod. so I pass in a string = 'Dog' now once in the selectmethod I need to convert that string into and object of type Dog...I hope this makes sense. Do anyone know how to do this???? i guess I need a similar method to Eval in javascript. Please need urgent so could replies also go to johncotsell@hotmail.com ...
5
3434
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError: unsupported operand type(s) for /=: 'unicode' and 'int' I want to divide x by 10 and assign that value back to x.
3
7991
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object of FileStream type and the mode access. There is a function Convert.ChangeType(object value, Type conversionType), but I cant write for example: Convert.ChangeType(mystring, FileStream); CAN YOU HELP ME????? thanks
3
9893
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the 2 textboxes and place the value in the label. Unfortunately, when I try to convert.Int32(txtBox1.Text) I get cannot convert string to int error. Basically, Im doing this: int x; int y; x = (int) Convert.ToInt32(TextBox1.Text); y = (int)...
1
9911
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the data from "DataView" using "RowFilter" method. Here I m using like operator for searching, its work fine for string but if data type of "Data Column" is Date or Integer then its not working. Now I m decide I will convert data types of all column...
4
4816
by: Shaileshsharma | last post by:
Hi, I have a value in string type variable ,i want to convert the string type into integer.This is my sample code int s1; string s2; int s3; if (ds.Tables.Rows.Count > 0) { s1 = ds.Tables.Rows.Count;
5
3104
by: 9966 | last post by:
Hi, currently I'm having a problem on converting a string type array into a char type. An example is as follows: string fruits; vector <char> temp; The fruits array HAS to be a string type and temp vector HAS to be a char type. This can't be changed. So I've a problem on storing the element of fruits array into temp vector as follows: temp.push_back(fruits); <-----I know this is wrong. So can I know how to convert string array into...
2
11483
by: moondaddy | last post by:
How can I convert a string to a FontFamily? Suto code: String sFont = "Bold"; FontFamily ff = (FontFamily)sFont; Thanks. -- moondaddy@newsgroup.nospam
9
8245
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
2
1565
by: bushnellweb | last post by:
The answers i found sucked so maybe someone can shed some new light on the situation. I have a 2-dimensional array of string type string string1; string1 = integer string1 = string string1 = float string1 = integer
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9306
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...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
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
4548
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...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
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
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.