473,657 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does ConvertFromStri ng 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.C onvertFromStrin g.
Perhaps I'm wrong. Using ConvertFromStri ng (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.W ebControls.Orie ntation Vertical

Thanks!
Sean

Apr 6 '06 #1
5 1641
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******** ******@TK2MSFTN GP04.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.C onvertFromStrin g.
Perhaps I'm wrong. Using ConvertFromStri ng (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.W ebControls.Orie ntation 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******** ******@TK2MSFTN GP04.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.C onvertFromStrin g.
Perhaps I'm wrong. Using ConvertFromStri ng (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.W ebControls.Orie ntation 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.Int 32";
string strValue = "567";
object o;
Type t = Type.GetType(st rType);
TypeConverter tc = TypeDescriptor. GetConverter(t) ;
if (strValue.GetTy pe().Equals(t)) o = strValue;
else o = tc.ConvertFromS tring(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******** ******@TK2MSFTN GP05.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.Int 32";
string strValue = "567";
object o;
Type t = Type.GetType(st rType);
TypeConverter tc = TypeDescriptor. GetConverter(t) ;
if (strValue.GetTy pe().Equals(t)) o = strValue;
else o = tc.ConvertFromS tring(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
4845
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 InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does not exist... What can I do about this and way does it not work in win 98?
14
4840
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 can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
89
6035
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
14
3471
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="" id="eventImage0" class="eventImage"><img src="" id="eventImage1" class="eventImage"></div>
0
8310
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
8826
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
8732
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
8503
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
8605
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
6166
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
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
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...
2
1615
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.