954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

datatypr convert string to point

i find this code in a forum

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        string pointText = "11, 19",
        rightToLeftText = "Inherit";
        Point point = Parse<Point>(pointText);
        RightToLeft rtl = Parse<RightToLeft>(rightToLeftText);
    }

    static T Parse<T>(string text)
    {
// might need ConvertFromString
        // (rather than Invariant)

        return (T)TypeDescriptor.GetConverter(typeof(T))
            .ConvertFromInvariantString(text);
    }

}


this code is working, but i dont understand what is "

RightToLeft rtl = Parse<RightToLeft>(rightToLeftText);


and what it does?
also i dont understand

return (T)TypeDescriptor.GetConverter(typeof(T))
            .ConvertFromInvariantString(text);)



? what type of format it is , and how it works

plz anyone explain me, i will be grateful.


ALSO my actual problem is i write a object.location as a string in file and read it from file and trying to convert in drawing.point. but my string write as "{X=11,Y=19}"
not like ""11, 19", as code, how can i convert this string to point.

thanx.

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

this code is working, but i dont understand what is "

RightToLeft rtl = Parse(rightToLeftText);

RightToLeft rtl = Parse(rightToLeftText); and what it does?


That's for dealing with languages (strings in C#) which are read from right to left. You can read more about RightToLeft property from MSDN.

also i dont understand return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(text);) ? what type of format it is , and how it works

If you look at function's signature static T Parse<T>(string text) you'll see that it's a static function named Parse which takes one string argumentand one type argument (T) and function's return type is the same (i.e. of type T). So, it's a generic function which converts a string (representing something of type T) to a value of type T. For example

int a = Parse<int>("12");
double b = Parse<double>("1.2");


Here's a quick&dirty solution for point conversion

// Get your point to a string variable
string z = "{X=11,Y=19}";

// Strip unneeded chars
Point p = Parse<Point>(z.ToUpper().Replace(@"{X=", "").Replace(@"Y=", "").Replace(@"}", ""));


With regular expression or some other approach you may get a better solution, but simple Replace works in this case.

HTH

Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
 

Thanks a lot teame64.
i m really gr8ful to u.
u r real genius.

thnx again.

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You