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.

Recommended Answers

All 2 Replies

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

RightToLeft rtl = Parse<RightToLeft>(rightToLeftText);

RightToLeft rtl = Parse<RightToLeft>(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 argument and 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

commented: gr8 +1

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

thnx again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.