473,624 Members | 2,615 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 2399
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 ConsoleApplicat ion1
{
abstract class Class1
{
public abstract static int val
{
get;
}

public static void test1()
{
Console.WriteLi ne(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...@nn owslpianmk.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
5909
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 Count { get { return 0; }
17
2342
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
2
1588
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 advance public abstract class Segment {
14
7082
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 define some derived classes wich inherit the function and override the variable, but it doesn't work. Testcase:
14
33257
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 ComponentA { static string s_name = "I am the root class."; public string Name { get {return s_name;} } }
6
1940
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 same class, one of them by string, the other by int. The weird thing is, if I seal one of them, I can't override the other. Here is the code.
4
2075
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 class as totally static and do this? If so then how please? (I dont want to create an instance of the derived class just to query a
9
5041
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 ToString()? Maybe it makes no sense to do such a thing... Zytan
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
1390
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 Identical and walking ,communication and eatng are common but non-identical)here is my code: using System; using System.Collections.Generic; using System.Text; namespace Project1
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1489
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.