473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 4192
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.com

"GroupReader" <ne***********@hotmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.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.
"GroupReader" <ne***********@hotmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.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

"GroupReader" <ne***********@hotmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.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.comwrote 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

"GroupReader" <ne***********@hotmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.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
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...
0
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>...
15
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
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
2
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...
1
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...
9
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...
6
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.