Implement ToString on a method?

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();
 
J

Jon Skeet [C# MVP]

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?
 
W

Wayne

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.
 
J

Jon Skeet [C# MVP]

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.
 
W

Wayne

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.
 
B

Bill Butler

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
 
B

Bill Butler

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
 
O

Oliver Sturm

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
 

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