Making static class ToString() not possible, why?

Z

Zytan

"A static member 'function' cannot be marked as override, virtual or
abstract"

Is it possible to make a static class member function (which is also
static, obviously) that is an override to ToString()? Maybe it makes
no sense to do such a thing...

Zytan
 
T

Tom Porterfield

Zytan said:
"A static member 'function' cannot be marked as override, virtual or
abstract"

Is it possible to make a static class member function (which is also
static, obviously) that is an override to ToString()? Maybe it makes
no sense to do such a thing...

You could make a static ToString() method, but you can't override an
instance method with a static method. The end result is your class
would have at least two ToString() methods, the static one and the one
it inherits from object.
--
Tom Porterfield
MS-MVP Windows
http://support.teloep.org

Please post all follow-ups to the newsgroup only.
 
J

Jon Skeet [C# MVP]

Zytan said:
"A static member 'function' cannot be marked as override, virtual or
abstract"

Is it possible to make a static class member function (which is also
static, obviously) that is an override to ToString()? Maybe it makes
no sense to do such a thing...

No, it doesn't. "Override" involves polymorphism, and polymorphism
doesn't apply to static members. To convince yourself of this, try to
work out what situation you'd expect it to be invoked in.
 
Z

Zytan

You could make a static ToString() method, but you can't override an
instance method with a static method. The end result is your class
would have at least two ToString() methods, the static one and the one
it inherits from object.

Ah, right, of course. I think I cannot make a static ToString()
method. This failed, and this is why I ran into this problem, because
I wasn't thinking that there was an instance ToString() that already
exists.

Thanks, Tom

Zytan
 
Z

Zytan

No, it doesn't. "Override" involves polymorphism, and polymorphism
doesn't apply to static members. To convince yourself of this, try to
work out what situation you'd expect it to be invoked in.

Yes, of course, those are OOP things, and a static class is basically
just a unit of functions. I don't want to inherit anything. I just
want a ToString() method that is automatically invoked when I append
it to a string. But, for that, I want the inherited method, don't I?

This scenario came about from me converting a struct which overrode
ToString(), into a static class. Everything converted over fine,
except the ToString(), which is used only for debugging purposes, so
it's not a big issue. I am just curious about it.

Thanks,

Zytan
 
J

Jon Skeet [C# MVP]

Zytan said:
Yes, of course, those are OOP things, and a static class is basically
just a unit of functions. I don't want to inherit anything. I just
want a ToString() method that is automatically invoked when I append
it to a string. But, for that, I want the inherited method, don't I?

But what's the "it" here? You say you want it to be automatically
invoked when you append "it" to a string, but with a static class there
*isn't* anything to append. That's the problem.
This scenario came about from me converting a struct which overrode
ToString(), into a static class. Everything converted over fine,
except the ToString(), which is used only for debugging purposes, so
it's not a big issue. I am just curious about it.

Hope that's cleared things up for you.
 
Z

Zytan

But what's the "it" here? You say you want it to be automatically
invoked when you append "it" to a string, but with a static class there
*isn't* anything to append. That's the problem.
LOL!


Hope that's cleared things up for you.

Yes, you have! :)

Thanks, Jon.
 
Z

Zytan

Yes, of course, those are OOP things, and a static class is basically
But what's the "it" here? You say you want it to be automatically
invoked when you append "it" to a string, but with a static class there
*isn't* anything to append. That's the problem.

I hope I can possibly explain why I wanted to do such a crazy thing,
it's because there was only one instance of this struct. And it had a
ToString() to easily print out all of its data. So, I made a static
class out of it, instead, which forces only one 'copy' of it. But, I
still wanted a convenient function to print out all of its data. But,
yes, how can object.ToString be invoke on a static class? Thus, I
need to merely make my own function named something else othere than
ToString and use it, instead.

Zytan
 
P

Peter Duniho

[...]
I hope I can possibly explain why I wanted to do such a crazy thing,
it's because there was only one instance of this struct. And it had a
ToString() to easily print out all of its data. So, I made a static
class out of it, instead, which forces only one 'copy' of it.

Well, the usual solution to doing something like that is what seems to be
called around here "the Singleton pattern" (sorry...I'm still getting used
to seeing the word "pattern" applied to coding practices...I keep
wondering when computer science got merged with home economics class :) ).

The general idea is that you restrict the class to a single instance by
hiding the constructors and providing instead a function to get at "the"
instance (optionally creating the instance on-demand if necessary).

If you do something that, then you can easily provide your own ToString()
method that overrides the standard one.

Pete
 
Z

Zytan

ok, thanks, Pete, I don't need anything too complicated, it was just
for debugging, but thanks for the help. I have heard of singletons,
before.

Zytan
 

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