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

Rectangle.FromString()

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
Nov 17 '05 #1
15 7609
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew McLellan" <an****@cix.co.uk> wrote in message
news:e$***************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #3
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
Nov 17 '05 #4
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew McLellan" <an****@cix.co.uk> wrote in message
news:e$***************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #5
Nicholas Paldino [.NET/C# MVP] wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #6
Working out what that implementation actually means is another thing!
Nov 17 '05 #7
Andrew McLellan wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #8
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
Nov 17 '05 #9
Andrew McLellan wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #10
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


Nov 17 '05 #11
Andrew McLellan wrote:
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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #12
Andrew McLellan <an****@cix.co.uk> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
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
Nov 17 '05 #14
Oliver Sturm <ol****@sturmnet.org> wrote:
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.


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);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #15
Jon Skeet [C# MVP] wrote:
Does it not?
Would I post that sample code if it didn't work for me?

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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #16

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

Similar topics

3
by: tlviewer | last post by:
hello, The script below is a prototype for loading playing card images from the bitmap resource in cards.dll (WinXP) I like the idea of not keeping copies of the images as files. I'm able...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
6
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
3
by: | last post by:
I am having a hard time understanding the logic behind the Rectangle object. My problem has to do with the way the rectangle treats the "Width" property. For example, take the following rectangle...
5
by: DazedAndConfused | last post by:
I have a rectangle around text that I want to fill in with color. I do not know the height of the rectangle until I actually go through and draw out the text. Is there a way of filling in the...
4
by: RobinS | last post by:
I am drawing a rectangle on a picture that has already been drawn on the graphics area (a user control). It works something like this: //in the MouseDown event m_isDragging = true; m_oldX =...
1
by: bharathv6 | last post by:
i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk PIL supports the use of...
1
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.