473,503 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does ConvertFromString work?

I have two strings, strType and strValue. I am looking for a single,
generic function to create an object of type strType with a value of
strValue. I think I'm looking to use TypeConverter.ConvertFromString.
Perhaps I'm wrong. Using ConvertFromString (or any other function that'd
work), how do I acomplish this? Examples:

strType strValue
Integer 12
Boolean True
System.Drawing.Color #B5C7DE
System.Web.UI.WebControls.Orientation Vertical

Thanks!
Sean

Apr 6 '06 #1
5 1627
I'm confused. You started out by saying that you "have two strings." That
implies that both strings are strings, and are therefore of the same type.
But then you say that you want to create an object of the first type with a
value of the second type. But since they are the same type, and they are
both strings, what exactly is the problem?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Sean Sampey" <ss*****@yahoo.com> wrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
I have two strings, strType and strValue. I am looking for a single,
generic function to create an object of type strType with a value of
strValue. I think I'm looking to use TypeConverter.ConvertFromString.
Perhaps I'm wrong. Using ConvertFromString (or any other function that'd
work), how do I acomplish this? Examples:

strType strValue
Integer 12
Boolean True
System.Drawing.Color #B5C7DE
System.Web.UI.WebControls.Orientation Vertical

Thanks!
Sean

Apr 6 '06 #2
Wait a second. I've been puzzling over your post, and I have a theory. Is it
possible that what you are asking is something along the lines of the
following?

Given data that is of a certain type and a certain value, you want to create
2 strings from the data, one of which holds the string representation of the
type, and the other of which holds the string representation of the value.

Is that what you're asking?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Sean Sampey" <ss*****@yahoo.com> wrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
I have two strings, strType and strValue. I am looking for a single,
generic function to create an object of type strType with a value of
strValue. I think I'm looking to use TypeConverter.ConvertFromString.
Perhaps I'm wrong. Using ConvertFromString (or any other function that'd
work), how do I acomplish this? Examples:

strType strValue
Integer 12
Boolean True
System.Drawing.Color #B5C7DE
System.Web.UI.WebControls.Orientation Vertical

Thanks!
Sean

Apr 6 '06 #3
Sorry for the unclear writing. What I am doing is serializing and
deserializing an object. To deserialize, I have the type of the new
object as a string (strType) and value it will hold as a string
(strValue). Using these two strings, I'm trying to create a new object
of type(strType) and value (strValue). For example, if strType were
"boolean" and strValue were "true", I would create a Boolean object
which equals True.

Kevin Spencer wrote:
Wait a second. I've been puzzling over your post, and I have a theory. Is it
possible that what you are asking is something along the lines of the
following?

Given data that is of a certain type and a certain value, you want to create
2 strings from the data, one of which holds the string representation of the
type, and the other of which holds the string representation of the value.

Is that what you're asking?


Apr 6 '06 #4
Hi Sean,

If your type is not a complex type, such as a class, you can use something
like the following:

string strType = "System.Int32";
string strValue = "567";
object o;
Type t = Type.GetType(strType);
TypeConverter tc = TypeDescriptor.GetConverter(t);
if (strValue.GetType().Equals(t)) o = strValue;
else o = tc.ConvertFromString(strValue);

Otherwise, you will have to deserialize it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Sean Sampey" <ss*****@yahoo.com> wrote in message
news:Ob**************@TK2MSFTNGP05.phx.gbl...
Sorry for the unclear writing. What I am doing is serializing and
deserializing an object. To deserialize, I have the type of the new object
as a string (strType) and value it will hold as a string (strValue). Using
these two strings, I'm trying to create a new object of type(strType) and
value (strValue). For example, if strType were "boolean" and strValue were
"true", I would create a Boolean object which equals True.

Kevin Spencer wrote:
Wait a second. I've been puzzling over your post, and I have a theory. Is
it possible that what you are asking is something along the lines of the
following?

Given data that is of a certain type and a certain value, you want to
create 2 strings from the data, one of which holds the string
representation of the type, and the other of which holds the string
representation of the value.

Is that what you're asking?

Apr 6 '06 #5
Yep, that did the trick for me. Thanks.

Kevin Spencer wrote:
Hi Sean,

If your type is not a complex type, such as a class, you can use something
like the following:

string strType = "System.Int32";
string strValue = "567";
object o;
Type t = Type.GetType(strType);
TypeConverter tc = TypeDescriptor.GetConverter(t);
if (strValue.GetType().Equals(t)) o = strValue;
else o = tc.ConvertFromString(strValue);

Otherwise, you will have to deserialize it.


Apr 6 '06 #6

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

Similar topics

7
4838
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
14
4814
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
5948
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
3450
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
0
7282
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
7342
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...
1
6998
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...
0
7464
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
5586
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
4680
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...
0
3171
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...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
391
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...

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.