473,414 Members | 1,626 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,414 software developers and data experts.

Making static class ToString() not possible, why?

"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

Apr 19 '07 #1
9 5027
Zytan wrote:
"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.
Apr 19 '07 #2
Zytan <zy**********@gmail.comwrote:
"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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #3
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

Apr 19 '07 #4
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

Apr 19 '07 #5
Zytan <zy**********@gmail.comwrote:
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?
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 19 '07 #6
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!
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.
Yes, you have! :)

Thanks, Jon.

Apr 19 '07 #7
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.
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

Apr 20 '07 #8
On Thu, 19 Apr 2007 17:07:25 -0700, Zytan <zy**********@gmail.comwrote:
[...]
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
Apr 20 '07 #9
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

Apr 23 '07 #10

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

Similar topics

15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
3
by: Amy L. | last post by:
I have a method below called "ResolveHostname" which is called from the ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient class where I am calling dnsClient.Lookup( Hostname)...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
4
by: shooter | last post by:
How does one make a function in a base class slect the appopriate version of an overriden static variable. Code-splination follows: public class letter { public static int PostCodeDigits =...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
2
by: Ognjen Bezanov | last post by:
Hello! Just to ask, is it possible to make a static dictionary in python. So that the keys in the dictionary cannot be removed, changed or new ones added, but the value pairs can. Is this...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.