473,672 Members | 2,826 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 3216
"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
15373
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
7851
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
3333
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
10486
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
8503
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
8945
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
8844
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
8642
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
7472
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2836
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
2092
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1836
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.