is there a bug in RectangleConverter

T

Tony Johansson

Hi!

When I run the code below I get runtime error saying
10,10,100,200 is not a valid value for Int32.
But I mean this method ConvertFromString can take a string and convert that
to a rectangle that's what the docs say
So is there a bug or have I missunderstood this ConvertFromString ?

protected override void OnPaint(PaintEventArgs e)
{
string rectInput = "10,10,100,200";
Graphics g = e.Graphics;
RectangleConverter converter = new RectangleConverter();
Rectangle outerRect = (Rectangle)
converter.ConvertFromString(rectInput);
Rectangle innerRect = new Rectangle(outerRect.X + 2, outerRect.Y +
2, outerRect.Width - 4, outerRect.Height - 4);

Region reg = new Region(innerRect);

g.DrawRectangle(Pens.Black, outerRect);
g.FillRegion(Brushes.AliceBlue, reg);
}

//Tony
//Tony
 
M

Martin Honnen

Tony said:
Hi!

When I run the code below I get runtime error saying
10,10,100,200 is not a valid value for Int32.
But I mean this method ConvertFromString can take a string and convert that
to a rectangle that's what the docs say
So is there a bug or have I missunderstood this ConvertFromString ?

protected override void OnPaint(PaintEventArgs e)
{
string rectInput = "10,10,100,200";
Graphics g = e.Graphics;
RectangleConverter converter = new RectangleConverter();
Rectangle outerRect = (Rectangle)
converter.ConvertFromString(rectInput);

It might depend on the current CultureInfo how that string is parsed
respectively what format is expected. Does using
ConvertFromInvariantString give you the result you want?
 
T

Tony Johansson

Martin Honnen said:
It might depend on the current CultureInfo how that string is parsed
respectively what format is expected. Does using
ConvertFromInvariantString give you the result you want?

If I use ConvertFromInvariantString it works but I mean a rectangle always
look the same no matter what culture we use.

//Tony
 
T

Tony Johansson

Martin Honnen said:
It might depend on the current CultureInfo how that string is parsed
respectively what format is expected. Does using
ConvertFromInvariantString give you the result you want?

If I use ConvertFromInvariantString it works but I mean a rectangle always
look the same no matter what culture we use.

//Tony
 
P

Peter Duniho

Tony said:
If I use ConvertFromInvariantString it works but I mean a rectangle always
look the same no matter what culture we use.

Ah, but that's where you're wrong.

A rectangle doesn't "look" like _anything_ until you render it somehow.
Typically, you render it graphically, which produces something that
has the geometric shape of the rectangle you're rendering.

Even there, there will be differences depending on whether you render it
with a blank pen or a red one, with a 1 pixel pen or a 10 pixel one,
etc. You could even describe those differences as "cultural".

But in the case of the string, you're dealing with a rectangle that has
been rendered textually. And that rendering depends very much on the
current culture. How things look as text is one of the most significant
things culture settings on the computer give us.

The rectangle your string "rectInput" attempts to describe can be stored
in a string in a variety of ways:

"10,10,100,200"
"10, 10, 100, 200"
"10.10.100.200"
"{10,10,100,200}"
"10;10;100;200"
etc.

(…and all of the above examples assume a specific order of the location
and size of the rectangle, while even that is not guaranteed; I could
just as easily store a rectangle as text by writing the size first, then
the location, or any other arrangement).

The RectangleConverter class can only successfully "un-render" a string
describing a rectangle if it knows what format was used to create the
string in the first place. And it does that by looking at the current
culture, or using the invariant culture if you call the
ConvertFromInvariantString() method instead.

As it happens, the string you're using matches the format expected by
the invariant culture. So it works when you use the
ConvertFromInvariantString() method. You could also get it to work by
determining what your current culture is and figuring out what format
that culture expects (though, if you're doing this for serialization
purposes, the invariant culture is actually a more appropriate choice
anyway).

Don't confuse the rectangle — that is, the abstract idea of a four-sided
polygon that has 90-degree angles at each corner — with the various ways
such a rectangle can be represented. And don't forget that text is just
as valid a representation as anything else, and text _does_ depend very
much on culture.

Pete
 
M

Martin Honnen

If I use ConvertFromInvariantString it works but I mean a rectangle always
look the same no matter what culture we use.

I am not very familiar with the RectangeConverter class and those
methods but as both ConvertFromString and ConvertFromInvariantString
exist and as ConvertFromString has an overload
http://msdn.microsoft.com/en-us/library/2kf8exy6(v=VS.90).aspx
taking a CultureInfo it looks as if ConvertFromString is culture dependant.
 
P

Patrice

If I use ConvertFromInvariantString it works but I mean a rectangle always
look the same no matter what culture we use.

"10,10,100,200" is just a string. Here in France , is used as the decimal
point so ; is used to separate items. Likely the same in your country...

So the culture does matter...
 
T

Tony Johansson

As it happens, the string you're using matches the format expected by the
invariant culture. So it works when you use the
ConvertFromInvariantString() method. You could also get it to work by
determining what your current culture is and figuring out what format that
culture expects (though, if you're doing this for serialization purposes,
the invariant culture is actually a more appropriate choice anyway).

Hi!


My current culture is swedish and this culture use ; as list separator and
not ,(comma) so I just changed from , to ; and now it works.
myRectangle.Location = (Point)pointConverter.ConvertFromString("20; 20");
myRectangle.Size = (Size)sizeConverter.ConvertFromString("200; 400");

I didn't thought about the list separator was culture dependent.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top