473,796 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Class Factory?

In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward , but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.

Mar 26 '07 #1
6 4222
Unfortunately, you can't do this. You will have to make your static
classes work on an instance level, and then have a factory which will return
instances of these classes (which implement the same interface or derive
from the same base).

You could make those instances singletons, if you want, which will give
you similar semantics to static methods on a class (but not completely).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"GroupReade r" <ne***********@ hotmail.comwrot e in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward , but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.

Mar 26 '07 #2
It doesn't make much sense. A "reference" always refers to an instance, so
it doesn't apply to a static class that will have no instances.

You could have your "factory" return a System.Type referring to the
appropriate static class, and then use Reflection on that Type to invoke the
static methods of the class, but this is not going to be very efficient. You
may wish to rethink the adequacy of the classes being static in this
scenario.
"GroupReade r" <ne***********@ hotmail.comwrot e in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward , but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.
Mar 26 '07 #3
MBR

You could use reflection to access one static class or the other (and I
suppose generate IL to clone them), but ick...
Why not make them instance-friendly?

If you must keep them static (like if you don't own the code), you could
also make an adapter that delgates the instace calls to the static classes,
and use a normal factory pattern to create the adaptors.

Or wait for MetaClass support from C# 4.7... :)

thanks,
m

"GroupReade r" <ne***********@ hotmail.comwrot e in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward , but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.


--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #4
Thanks guys, I think all of you were correct...

I ended up getting it to work using a slightly modified version of the
"classic singleton pattern."

I have a static class that returns either a Singleton Instance A or
Singleton Instance B.
Mar 28 '07 #5

"MBR" <no***@nospam.c omwrote in message
news:46******** *************** @free.teranews. com...
>
You could use reflection to access one static class or the other (and I
suppose generate IL to clone them), but ick...
Why not make them instance-friendly?

If you must keep them static (like if you don't own the code), you could
also make an adapter that delgates the instace calls to the static
classes,
Or create a struct with a bunch of delegate member variables (equivalent of
function pointers) which are a typesafe way to select a static method at
runtime. This is actually very similar to how virtual functions work
anyway, except that virtual function pass an extra "this" pointer that you
don't need or want.
and use a normal factory pattern to create the adaptors.

Or wait for MetaClass support from C# 4.7... :)

thanks,
m

"GroupReade r" <ne***********@ hotmail.comwrot e in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
>In my app, I have two very similar static classes. After long
thought, I've decided *yes - keep them static*.

- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.

- The interfaces (are interfaces allowed on static classes?) are
almost identical.

What's the best way in code to create some sort of "factory" that will
return a reference to Static Class A *OR* Static Class B, depending
on a switch.

(With instance classes, this is fairly straightforward , but I don't
know the best way to do it with static classes)

Are Interfaces allowed? How do you create a "factory" that returns a
reference to a static class, and not an instance of an object?

Thanks for the input.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 30 '07 #6
- Sometimes I will want to use Static Class A, and somtimes I will
want to use Static Class B, depending on the situation.
You may consider a generic static class:

public static class Foo<Unspecified >
{
...
}
- The interfaces (are interfaces allowed on static classes?)
No. A static class can inherit only from System.Object.

public static class Bar : System.Object // this will compile
{
...
}

public interface IFunkyInterface
{
...
}

public static class Foo : IFunkyInterface // this won't compile
{
...
}

Jun 5 '07 #7

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

Similar topics

3
1695
by: Aaron Prillaman | last post by:
I'm trying to set up a class factory for loading classes from a database. Each table will be loaded into it's own class with a common base. Just thought I'd try a simple sample first, but I'm having trouble getting it to link. Here's the code and output: class c1{ public: virtual void fn()=0; };
0
1083
by: Simon Elliott | last post by:
I have a class factory which maps various parameters onto static creator functions: typedef TbarAutoPtr (*TcreateBar)(const std::string& barName); typedef std::map <std::string, TcreateBar> TbarMap; TbarMap barMap_; barMap_ = TbarDerived1::CreateInstance;
15
3345
by: ad | last post by:
We can define a static member in a class. But can we define a static class, so that all members are static?
15
64132
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
2
15726
by: Sakke | last post by:
Hello! We have written a GCryptoSvr.dll COM server in C++. Inside that resides WebClient COM component. WebClient CLSID is {8DC27D48-F94C-434B-A509-C3E1A3E75B9E}. When we are using that WebClient COM component from C++ code it works just fine. However when we try to use that same WebClient in the same machine with following C# code: using GCRYPTOSVRLib; WEBClient WC = new WEBClient();
1
9450
by: jimmyfo | last post by:
Hi, I recently wrote an ASP.Net web application in VS2005 and published (using VS2005 Publish feature) it to a relatively clean machine with ASP.Net 2.0 and MDAC 2.8 installed on it. However, when I try to create my SQL connection in the code-behind, I get the following error. I tried to register the DLL using regsvr32 but that errored out saying, "dllregisterserver entry point was not found". Any ideas? Retrieving the COM class factory...
9
46777
by: =?Utf-8?B?QmFkaXM=?= | last post by:
Hi, I'm using automation and it's working fine when I run it from my local machine. but if I try to access it from another machine it's giving me this Error: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.
6
1637
by: =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?= | last post by:
Morning, I've got an ASP.NET 2.0 Web Application. Behind it, I've a statically-scoped facade/class factory as the business layer, running atomic functions back and forth from my by DAL. The DAL returns DataSet objects to the facade, which loads the data into a memento object to pass to the ASP.NET interface. For instance, I've got the method Profile ProfileSystem.GetProfile(Guid
4
1708
by: Mike P | last post by:
What is the difference between a helper class and a provider class? I have seen plenty of helper classes as static classes with only static methods, but I have also recently seen a provider class that is also a static class with static methods. What should be the difference between them? *** Sent via Developersdex http://www.developersdex.com ***
0
9673
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
9525
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
10221
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
10169
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
10003
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
9050
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...
1
7546
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
5440
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...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.