473,499 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Why can't a static member be abstract or an override?

Owen... Hopefully this is a succinct answer to your question Why can't a
static method be abstract or virtual (overridden)?:

It is an error to declare a static method abstract or virtual.

Declaring a static method abstract prohibits instantiation of the
enclosing class. Since static methods are part of the class itself they
can be invoked without creating an instance of the class, so there is no
logic in prohibiting instantiation of the enclosing class. Declaring a
static method virtual is an error since it would result in runtime
ambiguity. If the static virtual method was overridden in more than one
subclass, the runtime would not be able to determine which
implementation to invoke.

http://www.geocities.com/jeff_louie/OOP/oop40.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
6 2385
I've read this topic, but is still don't know what to do.
I would like my code to be as following, but gets an error due to
subject. Is there any way arround this problem, I would really like to
keep the methode test1 in the super class1, since it's quit complex, but
need the methode to use the value val depending on the subclass class2.

namespace ConsoleApplication1
{
abstract class Class1
{
public abstract static int val
{
get;
}

public static void test1()
{
Console.WriteLine(val);
}
}

class Class2 : Class1
{
public static override int val
{
get { return 10; }
}
}

class Program
{
static void Main(string[] args)
{
Class2.test1();
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #2
It would be better to show what you REALLY want rather than something that
probably resembles it.
Jun 27 '08 #3
On Fri, 16 May 2008 01:27:31 -0700, Orla Sorensen <cs****@orlaus.dkwrote:
I've read this topic, but is still don't know what to do.
I would like my code to be as following, but gets an error due to
subject. Is there any way arround this problem, I would really like to
keep the methode test1 in the super class1, since it's quit complex, but
need the methode to use the value val depending on the subclass class2.
It simply makes no sense for a static member to be virtual.

In the code example you've provided, the most obvious solution I see is to
make the method "test1()" take a parameter, then provide a different
"call_test1()" method in each class that calls "test1()" with the
appropriate value. That way, each class has its own public version of the
method appropriate to the value in the class, but they both can use the
common implementation in the base class.

There are other approaches that might also work, but as Peter M. notes,
without a more clear higher-level description of what you're really trying
to do, it's hard to provide suggestions.

Pete
Jun 27 '08 #4
Peter Duniho wrote:
On Fri, 16 May 2008 13:33:41 -0700, Barry Kelly <ba***********@gmail.com>
wrote:
Peter Duniho wrote:
It simply makes no sense for a static member to be virtual.
Actually, it can make sense,

No, not in C# it doesn't.
Sure, but I wasn't limiting my point to C#.

It's like an instance of the Sapir-Whorf hypothesis. If C#/Java/C++ are
the only kinds of languages you're[1] familiar with, then a "virtual
static members are nonsensical" may seem like a true statement in
general for object-oriented languages.

[1] I don't mean you specifically, but a generalized third party who may
be reading this and get the wrong idea.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #5
On Fri, 16 May 2008 15:38:56 -0700, Barry Kelly <ba***********@gmail.com>
wrote:
>It simply makes no sense for a static member to be virtual.

Actually, it can make sense,

No, not in C# it doesn't.

Sure, but I wasn't limiting my point to C#.
Why not? I was. This is a C# newsgroup.

There are other languages in which a type identifier is itself treatable
as an object. As an example, in Objective-C, there are class messages and
instance messages, and you can override either. I've been told that
Smalltalk is similar. No doubt lots of other languages are similar, in
that the type identifier is the type itself, rather than being a way to
use the type.

But this is C#. We're talking about C#, where the type identifier is a
different entity than the type itself. Saying "what could be" doesn't
help anyone trying to write C# code.

At the very least, if you're going to diverge from the topic, I think you
should be more explicit about your comments. Someone reading your post
might be misled into thinking that virtual static members "actually...can
make sense" even in C#.

Pete
Jun 27 '08 #6
On May 16, 4:30 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 16 May 2008 15:38:56 -0700, Barry Kelly <barry.j.ke...@gmail.com>
wrote:
It simply makes no sense for a static member to be virtual.
Actually, it can make sense,
No, not in C# it doesn't.
Sure, but I wasn't limiting my point to C#.

Why not? I was. This is a C# newsgroup.

There are other languages in which a type identifier is itself treatable
as an object. As an example, in Objective-C, there are class messages and
instance messages, and you can override either. I've been told that
Smalltalk is similar. No doubt lots of other languages are similar, in
that the type identifier is the type itself, rather than being a way to
use the type.
You can even do it on .NET with Delphi, where "class methods" can be
overridden (including constructors). That's a handy feature, and it's
a shame Anders left it out of C#.

Jesse
Jun 27 '08 #7

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

Similar topics

5
5900
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
17
2328
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
2
1582
by: Polo | last post by:
Hi I have a abstract class and some other than inherite from it I woul like to return a bitmap (defined in resource) from the each subclass (a bitmap that is global (static)) Thank's in...
14
7063
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
14
33223
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...
6
1927
by: Ole Nielsby | last post by:
I'm having a strange problem with sealing virtual indexers. Looks like a compiler error to me - or have I overlooked some obscure statement in the specs? I have two virtual indexers in the...
4
2065
by: Chukkalove | last post by:
Hi I have an abstract class that contains 100% static methods and variables. One of the member variables "string DatabaseName" needs to be overridden in derived classes. Am I able to keep my...
9
5030
by: Zytan | last post by:
"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...
0
124
by: Alexander Mueller | last post by:
Owen Blacker schrieb: Something that is static just exists once. If you could override or derive it, you would create it for a second time. See it like this:
2
1375
by: alireza6485 | last post by:
Hi, Can you please help me with my assignment.I coded it and it runs I just need someone to help me a bit. The behaviour of monekys and human beings are Similar (Breeding and breathing are...
0
7128
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
7006
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
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
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...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4597
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...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.