473,407 Members | 2,629 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,407 software developers and data experts.

Opposite of "ToString"

Once you call "ToString()" for an object, how can you then convert the
string back to the original object (assuming you know its type of course)?
Also, why does "ToString()" produce one string and
"TypeConverter.ConvertToString()" produce yet another (e.g., for a
"System.Drawing.Size" object, the former method yields, say, "Width=5,
Height=40" and the latter yields "5, 40"). Thanks.
Jul 1 '06 #1
4 4853
Because all the classes in .NET framework are inherited from System.Object
class.
http://msdn2.microsoft.com/en-us/lib...ct(d=ide).aspx

chanmm

"Larry Smith" <no_spam@_nospam.com> wrote in message
news:eK**************@TK2MSFTNGP05.phx.gbl...
Once you call "ToString()" for an object, how can you then convert the
string back to the original object (assuming you know its type of course)?
Also, why does "ToString()" produce one string and
"TypeConverter.ConvertToString()" produce yet another (e.g., for a
"System.Drawing.Size" object, the former method yields, say, "Width=5,
Height=40" and the latter yields "5, 40"). Thanks.

Jul 1 '06 #2
There is no generic way to convert from a ToString back to an object (for
some objects there are parse methods suc h as DateTime.Parse). If you have a
simple class "Foo" that you call ToString() on it will return
"YourNameSpace.Foo" which won't be very helpful in recreating the object as
it does not have all of the data required in order to recreate the object.
Serialization will work in both directions as it includes all of the
necesary information to recreate the object.

TypeConverter is different by design, it is there to allow for other
mechanisms to be put in place (like your example). It is also a more generic
than a ToString() as it can work with other types
http://msdn2.microsoft.com/en-us/lib...converter.aspx
explains a bit mroe and gives some examples.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Larry Smith" <no_spam@_nospam.com> wrote in message
news:eK**************@TK2MSFTNGP05.phx.gbl...
Once you call "ToString()" for an object, how can you then convert the
string back to the original object (assuming you know its type of course)?
Also, why does "ToString()" produce one string and
"TypeConverter.ConvertToString()" produce yet another (e.g., for a
"System.Drawing.Size" object, the former method yields, say, "Width=5,
Height=40" and the latter yields "5, 40"). Thanks.

Jul 1 '06 #3
Larry,

It should be noted that ToString is not required to output the
information which will allow you to reconstitute an instance from the
results.

The documentation only states:

Return Value
A String that represents the current Object.

There are no guarantees that it can actually be used to create a
specific object.
The reason that a TypeConverter produces a different string is because
its function is to convert one type to another, (strings included), with no
loss of information. From the documentation:

Provides a unified way of converting types of values to other types, as well
as for accessing standard values and subproperties.

This is different from a representation of the object.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Larry Smith" <no_spam@_nospam.comwrote in message
news:eK**************@TK2MSFTNGP05.phx.gbl...
Once you call "ToString()" for an object, how can you then convert the
string back to the original object (assuming you know its type of course)?
Also, why does "ToString()" produce one string and
"TypeConverter.ConvertToString()" produce yet another (e.g., for a
"System.Drawing.Size" object, the former method yields, say, "Width=5,
Height=40" and the latter yields "5, 40"). Thanks.

Jul 2 '06 #4
I don't think there is automatic way u can use. the only way i know is to use parse.
Jul 4 '06 #5

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

Similar topics

2
by: Olaf Baeyens | last post by:
A question: (C#, C++) If I use iValue.ToString("000000000000",CultureInfo::CurrentUICulture)); The I get a string with a fixed lengt if format: "000000542663" But since I want the leading "0"...
6
by: trint | last post by:
Here is the vb.net version of what I need to do in c#: myString = drSQL.Item("columnName").ToString() Thanks, Trint
1
by: ad | last post by:
Hi, What is the difference between (string)dr and dr.ToString();
1
by: Steve Norman | last post by:
Hi All, I am trying to display an int as it' sHex representation, and looking on MSDN it appears this can be done with .ToString("X"). This does work, but the example on MSDN...
5
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in...
0
by: Norman Yuan | last post by:
I posted this issue a while ago. After moveing the ASP.NET 1.1 app to a brand new server, the same problem still there. System: Winows2003 server std. .NET 1.1 App: Invoicing system Problem:...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
1
by: Marc Gravell | last post by:
Just been debugging a daylight savings glitch... something that interested me: (in a BST locale at 5:14 local time, 4:14 GMT) DateTime.Now.ToString("R") >"Mon, 26 Mar 2007 05:14:34 GMT" ...
5
by: Bob | last post by:
Hello Folks, I am bring backa data reader dr from the database/ So I have a number of fields such as dr To put this in a table I use "<td>" + dr.ToString() + "</td>" But I want to...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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,...

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.