473,765 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

public abstract static ...

Hi,
I was just wondering, why is not possible to make a member of a class BOTH
abstract and static? MSDN says just that's in an error, not why this is so.
In a nutshell, I need this:
I have an abstract base class called EntityBase that all my other business
objects are derived from. I want to be able to load and save them in a
generic manner. I use a static method called EntityBase.GetB yID(int id) and a
member method called EntityBase.Save (). I want to be able to force every
derived class to implement both of these methods, with this limitation I
can't though. Is there any workaround? Is there any reason for this
limitation in the first place?
Thanks!
Mar 23 '06 #1
5 3220
"pali" <pa**@discussio ns.microsoft.co m> a écrit dans le message de news:
62************* *************** **...icrosof t.com...

| I was just wondering, why is not possible to make a member of a class BOTH
| abstract and static? MSDN says just that's in an error, not why this is
so.
| In a nutshell, I need this:
| I have an abstract base class called EntityBase that all my other business
| objects are derived from. I want to be able to load and save them in a
| generic manner. I use a static method called EntityBase.GetB yID(int id)
and a
| member method called EntityBase.Save (). I want to be able to force every
| derived class to implement both of these methods, with this limitation I
| can't though. Is there any workaround? Is there any reason for this
| limitation in the first place?

Neither constructors or static members are allowed to be virtual in C#; you
can only declare virtual (and abstract) instance methods. That is just one
of the few limitations of the language.

Are you sure you want your method to be accessible without having to
instantiate the class it is included in ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 23 '06 #2
Yes, i know this is the limitation, I'm asking why? Is that a by-design choice?

I don't think it's meaningful to instantiate an instance of an object only
to be able to read/delete it from the store.
My typical class would look like this:
public class Customer : EntityBase
{
public static Customer GetByID(int id) {}
public void Save() {}
public static void Delete(int id) {}
}
In this case i don't see a reason to instantiate an object if I want to get
it from the database by id anyway. Same for the delete, I don't want to have
to get an entity from the database just to be able to delete it.
I want to put the signatures of those methods in the parent abstract class,
so I can get/save/delete them in a generic way...

"Joanna Carter [TeamB]" wrote:
"pali" <pa**@discussio ns.microsoft.co m> a écrit dans le message de news:
62************* *************** **...icrosof t.com...

| I was just wondering, why is not possible to make a member of a class BOTH
| abstract and static? MSDN says just that's in an error, not why this is
so.
| In a nutshell, I need this:
| I have an abstract base class called EntityBase that all my other business
| objects are derived from. I want to be able to load and save them in a
| generic manner. I use a static method called EntityBase.GetB yID(int id)
and a
| member method called EntityBase.Save (). I want to be able to force every
| derived class to implement both of these methods, with this limitation I
| can't though. Is there any workaround? Is there any reason for this
| limitation in the first place?

Neither constructors or static members are allowed to be virtual in C#; you
can only declare virtual (and abstract) instance methods. That is just one
of the few limitations of the language.

Are you sure you want your method to be accessible without having to
instantiate the class it is included in ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 23 '06 #3
"pali" <pa**@discussio ns.microsoft.co m> a écrit dans le message de news:
50************* *************** **...icrosof t.com...

| Yes, i know this is the limitation, I'm asking why? Is that a by-design
choice?

I guess so, Delphi has virtual static methods.

| I don't think it's meaningful to instantiate an instance of an object only
| to be able to read/delete it from the store.
| My typical class would look like this:
| public class Customer : EntityBase
| {
| public static Customer GetByID(int id) {}
| public void Save() {}
| public static void Delete(int id) {}
| }
| In this case i don't see a reason to instantiate an object if I want to
get
| it from the database by id anyway. Same for the delete, I don't want to
have
| to get an entity from the database just to be able to delete it.
| I want to put the signatures of those methods in the parent abstract
class,
| so I can get/save/delete them in a generic way...

Then how about factoring out the retrieval behaviour, which I would say
doesn't belong in the Customer class and use an Object Persistence Framework
instead ?

If you are using .NET 2.0, you can have methods on it like :

public class ObjectStore
{
public objectT RetrieveObject< objectT>(int Id)
{
...
}

...
}

Although I would also argue that most times you could retrieve a browsing
list for a criteria and then choose one object. That way you don't need to
know the ID outside of the OPF.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 23 '06 #4
Thanks,
i will try this template approach, looks nice :)

I've tried NHibernate and Gentle.NET as OPFs so far, both of them have quite
some limitations though, so I'll rather stick with my own "framework" and
code-generation templates...

Thanks!
"Joanna Carter [TeamB]" wrote:
"pali" <pa**@discussio ns.microsoft.co m> a écrit dans le message de news:
50************* *************** **...icrosof t.com...

| Yes, i know this is the limitation, I'm asking why? Is that a by-design
choice?

I guess so, Delphi has virtual static methods.

| I don't think it's meaningful to instantiate an instance of an object only
| to be able to read/delete it from the store.
| My typical class would look like this:
| public class Customer : EntityBase
| {
| public static Customer GetByID(int id) {}
| public void Save() {}
| public static void Delete(int id) {}
| }
| In this case i don't see a reason to instantiate an object if I want to
get
| it from the database by id anyway. Same for the delete, I don't want to
have
| to get an entity from the database just to be able to delete it.
| I want to put the signatures of those methods in the parent abstract
class,
| so I can get/save/delete them in a generic way...

Then how about factoring out the retrieval behaviour, which I would say
doesn't belong in the Customer class and use an Object Persistence Framework
instead ?

If you are using .NET 2.0, you can have methods on it like :

public class ObjectStore
{
public objectT RetrieveObject< objectT>(int Id)
{
...
}

...
}

Although I would also argue that most times you could retrieve a browsing
list for a criteria and then choose one object. That way you don't need to
know the ID outside of the OPF.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 23 '06 #5

"pali" <pa**@discussio ns.microsoft.co m> wrote in message
news:50******** *************** ***********@mic rosoft.com...
Yes, i know this is the limitation, I'm asking why? Is that a by-design
choice?

I don't think it's meaningful to instantiate an instance of an object only
to be able to read/delete it from the store.
My typical class would look like this:
public class Customer : EntityBase
{
public static Customer GetByID(int id) {}
public void Save() {}
public static void Delete(int id) {}
}
In this case i don't see a reason to instantiate an object if I want to
get
it from the database by id anyway. Same for the delete, I don't want to
have
to get an entity from the database just to be able to delete it.
I want to put the signatures of those methods in the parent abstract
class,
so I can get/save/delete them in a generic way...


How do you think that you would call an abstract static method?

EntityBase.Dele te(32); // how can it know that you mean Customer?

Customer.Delete (32); // why would it need to be abstract in EntityBase?

If you mean that you should be able to call base static methods through
derived scope as you can in C++ then I agree with you but that doesn't seem
to be what you are asking.

Mar 27 '06 #6

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

Similar topics

3
15378
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an abstracct superclass, with an abstract method such that all subclasses that implement this method make that method static. basically, the abstract class declares an abstract method that should be static for all implementations (for example a factory...
1
7863
by: jkstexas2001 | last post by:
Could someone be of assistance and show me the problem with the following code? I have researched Metalink regarding ORA-29531, and the most common cause of this error is the failure to declare the method "public" and "static". I have done both here and still have the same error: Java Code:::::::::::::::::::::::::::::::::::::::::::::: CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "TimeZoneFormatter" AS import java.util.TimeZone;
33
3351
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
2
10491
by: Mads Lee Jensen | last post by:
Hello.. Was wondering shouldn't an abstract static method cause a E_STRICT ? this is my test. error_reporting(E_STRICT); abstract class Test_Abstract { protected abstract static function testStatic(); }
0
9568
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
9399
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
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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
7379
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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
5276
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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

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.