Using ToString

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

When I call .ToString in order to return the class property values, is there
any way to return the values discretely? I want to return some values as
integers and some as strings. Is there any way to pull these out one at a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc. When I try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?
 
Earl comcast net> said:
When I call .ToString in order to return the class property values, is there
any way to return the values discretely? I want to return some values as
integers and some as strings. Is there any way to pull these out one at a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc. When I try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?

I'm not sure what you're asking. You have a ListBox populated with
instances of your Grid class, and you want to get those instances out?
Have you tried something like:

[VB] Dim myGrid As Grid = DirectCast(ListBox1.Items(0), Grid)

[C#] private Grid myGrid = (Grid) ListBox1.Items[0];

You shouldn't have to parse an output string to get your original
values -- eek!

Hope this helps!
Jeremy
 
If I understand what you are doing , you are relying on the ToString to be
the visible entry in the ListBox. In your classes simply override the
ToString with your own implementation. Then you control what you see.

Lloyd Sheen
Jeremy Todd said:
Earl comcast net> said:
When I call .ToString in order to return the class property values, is there
any way to return the values discretely? I want to return some values as
integers and some as strings. Is there any way to pull these out one at a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc. When I try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?

I'm not sure what you're asking. You have a ListBox populated with
instances of your Grid class, and you want to get those instances out?
Have you tried something like:

[VB] Dim myGrid As Grid = DirectCast(ListBox1.Items(0), Grid)

[C#] private Grid myGrid = (Grid) ListBox1.Items[0];

You shouldn't have to parse an output string to get your original
values -- eek!

Hope this helps!
Jeremy
 
* "Earl said:
When I call .ToString in order to return the class property values, is there
any way to return the values discretely? I want to return some values as
integers and some as strings. Is there any way to pull these out one at a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc. When I try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?

Create a class which inherits from your item class and override the
'ToString' method. In your custom implementation, return the property
values concatenated to a single string.
 
Thanks, but that's really what I've already got and would like to pull those
out discretely. The concatenation is what I'm trying to avoid.
 
Thanks for the idea Lloyd. I think what you are suggesting is a subclass for
each propery value that I wish to return?

Lloyd Sheen said:
If I understand what you are doing , you are relying on the ToString to be
the visible entry in the ListBox. In your classes simply override the
ToString with your own implementation. Then you control what you see.

Lloyd Sheen
at
a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc. When
I
try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?

I'm not sure what you're asking. You have a ListBox populated with
instances of your Grid class, and you want to get those instances out?
Have you tried something like:

[VB] Dim myGrid As Grid = DirectCast(ListBox1.Items(0), Grid)

[C#] private Grid myGrid = (Grid) ListBox1.Items[0];

You shouldn't have to parse an output string to get your original
values -- eek!

Hope this helps!
Jeremy
 
I am now lost as to what you want to do. The ToString was to put a readable
value in the listbox. To get the actual object, it should be the
selecteditem of the listbox.

Lloyd

Earl comcast net> said:
Thanks for the idea Lloyd. I think what you are suggesting is a subclass for
each propery value that I wish to return?

If I understand what you are doing , you are relying on the ToString to be
the visible entry in the ListBox. In your classes simply override the
ToString with your own implementation. Then you control what you see.

Lloyd Sheen
values
When
I
try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and then
splitting. What am I missing here?

I'm not sure what you're asking. You have a ListBox populated with
instances of your Grid class, and you want to get those instances out?
Have you tried something like:

[VB] Dim myGrid As Grid = DirectCast(ListBox1.Items(0), Grid)

[C#] private Grid myGrid = (Grid) ListBox1.Items[0];

You shouldn't have to parse an output string to get your original
values -- eek!

Hope this helps!
Jeremy
 
No problem putting the readable value in the listbox, I want to discretely
get at the the other property values of the class simultaneously.

Lloyd Sheen said:
I am now lost as to what you want to do. The ToString was to put a readable
value in the listbox. To get the actual object, it should be the
selecteditem of the listbox.

Lloyd

Earl comcast net> said:
Thanks for the idea Lloyd. I think what you are suggesting is a subclass for
each propery value that I wish to return?
to
values,
is
there
any way to return the values discretely? I want to return some
values
as
integers and some as strings. Is there any way to pull these out
one
at
a
time?

For example, I have a listbox that I populate with instances of my Grid
class. The class properties include GridName, GridID number, etc.
When
I
try
to retrieve the values, I see no other way of getting to the individual
values without returning all of the values concatenated together and
then
splitting. What am I missing here?

I'm not sure what you're asking. You have a ListBox populated with
instances of your Grid class, and you want to get those instances out?
Have you tried something like:

[VB] Dim myGrid As Grid = DirectCast(ListBox1.Items(0), Grid)

[C#] private Grid myGrid = (Grid) ListBox1.Items[0];

You shouldn't have to parse an output string to get your original
values -- eek!

Hope this helps!
Jeremy
 
Can you give us a code sample?

I'm guessing here but do you have something like this:

class Foo
{
int bar;
string baz;

public override string ToString()
{
return baz + bar.ToString();
}

// SEE BELOW
}

If so the reason you can't get to the data members is that they are private
by default. You could read them either with a function:

public int GetBar()
{
return bar;
}

or a property:

public int Bar
{
get { return bar; }
}

(These code snippets would be inserted at the marked position above).

If I'm barking up the wrong tree then apologies.

Stu


Earl comcast net> said:
Thanks, but that's really what I've already got and would like to pull those
out discretely. The concatenation is what I'm trying to avoid.

at
I
 
Back
Top