Rectangle.FromString()

  • Thread starter Thread starter Andrew McLellan
  • Start date Start date
A

Andrew McLellan

I'm still finding my way round C# and am not sure what is possible and not
possible. Is there any way to do the equivalent of

Rectangle r = new Rectangle();
r.FromString(anotherRectangle.ToString());

and assuming there isn't, would the panel recommend I create my own
Rectangle class with a FromString method or override the ToString to give
something a bit more parsable, or what?

Thanks,

Andrew
 
Andrew,

You have no choice but to create your own structure, since you can't
derive from structures in .NET.

Of course, this means that you couldn't use your rectangle structure
where the regular Rectangle structure is used.

I am curious why you want to do this. You ask for something more
parsable, and yet, the methods to persist to a string and get the values
from that very same string are already there. What are you trying to do?

Hope this helps.
 
Nicholas said:
You have no choice but to create your own structure, since you can't derive from structures in .NET.

Of course, this means that you couldn't use your rectangle structure where the regular Rectangle structure is used.

I think it would be better to implement this function outside of the
Rectangle structure (obviously) and also without a new Rectangle. I'm
assuming here, of course, that the functionality in question is restricted
to fairly simple stuff that can be done from the outside, like the string
conversion you want. In that case I think it would be a better approach to
have a RectangleStringConverter or something than to introduce your own
non-interchangable Rectangle replacement.


Oliver Sturm
 
I guess I'm after the method that 'gets the values from
that very same string' - ie, I'm completely missing something obvious about
persistence.
Given a string of the form {X=100,Y=100,Width=600,Height=300} how do I get a
Rectangle with X of 100, Y of 100, etc?
You have no choice but to create your own structure, since you can't
derive from structures in .NET.

Well that saves me from trying that one then! Thanks.

Andrew
 
Andrew,

I just realized that there is not a FromString method on the Rectangle
structure.

What you want to use to convert this is the RectangleConverter structure
in the System.Drawing namespace. You can use the ConvertFromString method
on the RectangleConverter to convert the string representation to a
Rectangle.
 
Nicholas said:
What you want to use to convert this is the RectangleConverter structure
in the System.Drawing namespace. You can use the ConvertFromString method
on the RectangleConverter to convert the string representation to a
Rectangle.

Good call, I had forgotten there was such a thing :-) That thing is
apparently implemented just the way I was suggesting!


Oliver Sturm
 
Andrew said:
Working out what that implementation actually means is another thing!

Try code like this:

Rectangle r1 = new Rectangle(10,10,10,10);
Console.WriteLine("r1: " + r1);
RectangleConverter converter = new RectangleConverter();
string s = converter.ConvertToString(r1);
Console.WriteLine("s: " + s);
Rectangle r2 = (Rectangle) converter.ConvertFromString(s);
Console.WriteLine("r2: " + r2);


Oliver Sturm
 
Thanks, but this throws an exception (which is the bit I'm getting confused
over):

Rectangle r1 = new Rectangle(10,10,10,10);
Console.WriteLine("r1: " + r1);
RectangleConverter converter = new RectangleConverter();
string s1 = converter.ConvertToString(r1);
Console.WriteLine("s1: " + s1);
string s2 = r1.ToString();
Console.WriteLine("s2: " + s2);
Rectangle r2 = (Rectangle) converter.ConvertFromString(s1);
Console.WriteLine("r2: " + r2);
Rectangle r3 = (Rectangle) converter.ConvertFromString(s2);
Console.WriteLine("r3: " + r3);

Andrew
 
Andrew said:
Thanks, but this throws an exception (which is the bit I'm getting
confused over):

<snip>

Well, it doesn't throw an exception for me, and if you're not going to
tell us what the exception says or on which line it's thrown, we're
unlikely to be able to help you with it.


Oliver Sturm
 
Does it not? Are you using .Net2? I'm on 1.1.

It throws

An unhandled exception of type 'System.Exception' occurred in system.dll
Additional information: {X=10 is not a valid value for Int32.

because Converter.ConvertToString() and Rectangle.ToString() don't return
the same string.



Andrew
 
Andrew said:
Does it not?

Would I post that sample code if it didn't work for me?
Are you using .Net2? I'm on 1.1.

Yes I am. But from the MS docs that shouldn't make a difference here.
It throws

An unhandled exception of type 'System.Exception' occurred in system.dll
Additional information: {X=10 is not a valid value for Int32.

because Converter.ConvertToString() and Rectangle.ToString() don't return
the same string.

Of course they don't, nobody said that. Didn't you test the sample in its
entirety? You must use the corresponding methods of the converter to
convert in both directions, otherwise it won't work. Try it just like my
sample shows and it'll work.


Oliver Sturm
 
Andrew McLellan said:
Thanks, but this throws an exception (which is the bit I'm getting confused
over):

Rectangle r1 = new Rectangle(10,10,10,10);
Console.WriteLine("r1: " + r1);
RectangleConverter converter = new RectangleConverter();
string s1 = converter.ConvertToString(r1);
Console.WriteLine("s1: " + s1);
string s2 = r1.ToString();
Console.WriteLine("s2: " + s2);
Rectangle r2 = (Rectangle) converter.ConvertFromString(s1);
Console.WriteLine("r2: " + r2);
Rectangle r3 = (Rectangle) converter.ConvertFromString(s2);
Console.WriteLine("r3: " + r3);

It would help if you'd said what exception was being thrown,
preferrably with a short but complete program to demonstrate the
problem. Anyway, it looks like RectangleConverter can only convert back
to a rectangle when it's given a string which is produced by a
RectangleConverter in the first place - it doesn't cope with the
results of Rectangle.ToString itself. Is that a major problem for you?
 
I did test your sample - my sample was different :-) !

OK, I know where I am with this, thanks very much to you and Nick for your
help.

Andrew
 
Oliver Sturm said:
Would I post that sample code if it didn't work for me?


Yes I am. But from the MS docs that shouldn't make a difference here.

Odd. It doesn't work for me on either .NET 1.1 or .NET 2.0:

using System;
using System.Drawing;

public class Test
{

static void Main()
{
try
{
Rectangle r1 = new Rectangle(10,10,10,10);
Console.WriteLine("r1: " + r1);
RectangleConverter converter = new RectangleConverter();
string s1 = converter.ConvertToString(r1);
Console.WriteLine("s1: " + s1);
string s2 = r1.ToString();
Console.WriteLine("s2: " + s2);
Rectangle r2 = (Rectangle) converter.ConvertFromString(s1);
Console.WriteLine("r2: " + r2);
Rectangle r3 = (Rectangle) converter.ConvertFromString(s2);
Console.WriteLine("r3: " + r3);
}
catch (Exception e)
{
Console.WriteLine (e);
}
}
}
 
Odd. It doesn't work for me on either .NET 1.1 or .NET 2.0:

Sorry guys - when I looked at the post where Andrew posted the sample code
he was using himself, I didn't notice that is was different from the block
I had previously posted myself. It looked too similar :-) That code, where
ToString() is used instead of the converter method, doesn't work for me
either (and I wouldn't assume it should).


Oliver Sturm
 
Back
Top