Implement ToString on a method?

  • Thread starter Thread starter Wayne
  • Start date Start date
W

Wayne

I have class, and one of its methods is to return the number of items
based on a criteria.

The user can parse that through, but is it possible to append the
ToString() function to a method?

For instance I would call the procedure thusly:
int myvar = p.Count("foo");
But how would I implement it such that:
string myvar = p.Count("Foo").ToString();
 
Wayne said:
I have class, and one of its methods is to return the number of items
based on a criteria.

The user can parse that through, but is it possible to append the
ToString() function to a method?

For instance I would call the procedure thusly:
int myvar = p.Count("foo");
But how would I implement it such that:
string myvar = p.Count("Foo").ToString();

You can do that already - it ends up calling ToString() on the integer.

What do you want to do that you can't do already?
 
Jon said:
You can do that already - it ends up calling ToString() on the integer.

What do you want to do that you can't do already?

The situation that brought this up, was me using MessageBox.Show() which
only takes a string for the message parameter.

I can assign the return to a var then use the var's ToString() to pass
that to MessageBox, but I was wondering of it was possible.
 
Wayne said:
The situation that brought this up, was me using MessageBox.Show() which
only takes a string for the message parameter.

I can assign the return to a var then use the var's ToString() to pass
that to MessageBox, but I was wondering of it was possible.

Just call ToString() on the return value of the method, just as you did
in your previous post.
 
Jon said:
Just call ToString() on the return value of the method, just as you did
in your previous post.

That works now????

Before I was recieving an error "Count does not contain a definintion
for ToString"

I have not changed any of my code.

This is wierd.
 
Wayne said:
Jon Skeet [C# MVP] wrote:
Just call ToString() on the return value of the method, just as you did
in your previous post.

That works now????

Before I was recieving an error "Count does not contain a definintion for
ToString"

I have not changed any of my code.

This is wierd.

The most likely explanation is that you had a Type in your call to
ToString()
Tostring(), toString(), ToSting()?

Hard to tell after the fact.

Bill
 
The most likely explanation is that you had a Type in your call to
ToString()
Tostring(), toString(), ToSting()?

Oops.....That was supposed to read " had a Typo in your call ".

How ironic :^)

Bill
 
Bill said:
Oops.....That was supposed to read " had a Typo in your call ".

How ironic :^)

Allways proofread carefully in case you something out. :-)


Oliver Sturm
 
Back
Top