Static Interface/Abstract/Virtual

T

Tamir Khason

I have some classes that basicly do the same things but different way.
There are no default constructors in those classes each constructor should
recieve same value

So

Fooclass:MyBasicClass or Fooclass:IBasicClass

and I want to be able to call form program something like this:

MyBasicClass foo = new MyBasicClass(value);
or
IBasicInterface foo = new IBasicInterface(value);

Interfaces,Abstracts or Virtuals does not support constructors with
parameters

So trying:

MyBasicClass foo = MyBasicClass.GetInstance(value);
or
IBasicInterface foo = IBasicInterface.GetInstance(value);

and in each class something like
if(this=null)
return new Fooclass(value);
else
return this;

But I can not create static methods neither in interface, abstract or
virtual

So how to implement such architecture???

TNX
 
G

Guest

Well, how do you determine which object you need when you create them?
Usually what some people will do is create a "Class factory" which when
passed certain parameters determines which class you actually need and
returns it to your Interface type or base type.

Thanks,
Justin Etheredge
 
N

Nicholas Paldino [.NET/C# MVP]

Tamir,

Basically, you would want a class factory. You would pass the factory
the type, and it would return the interface or abstract base class.

When constructing the class, you would use reflection. You would get
the constructor from the Type for the implementation, and then call it,
passing the parameter. Since you can't define the contract through an
interface, you have to encapsulate the contract through a class factory.

Hope this helps.
 
T

Tamir Khason

Basicly, only one object will not return exception in my certain case... It
parameter based. If the object can not work with parameter transferred it'll
thow exception.
 
T

Tamir Khason

It seemed I can not use reflection here, due all of those objects are in the
same class with caller

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nicholas Paldino said:
Tamir,

Basically, you would want a class factory. You would pass the factory
the type, and it would return the interface or abstract base class.

When constructing the class, you would use reflection. You would get
the constructor from the Type for the implementation, and then call it,
passing the parameter. Since you can't define the contract through an
interface, you have to encapsulate the contract through a class factory.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
I have some classes that basicly do the same things but different way.
There are no default constructors in those classes each constructor
should recieve same value

So

Fooclass:MyBasicClass or Fooclass:IBasicClass

and I want to be able to call form program something like this:

MyBasicClass foo = new MyBasicClass(value);
or
IBasicInterface foo = new IBasicInterface(value);

Interfaces,Abstracts or Virtuals does not support constructors with
parameters

So trying:

MyBasicClass foo = MyBasicClass.GetInstance(value);
or
IBasicInterface foo = IBasicInterface.GetInstance(value);

and in each class something like
if(this=null)
return new Fooclass(value);
else
return this;

But I can not create static methods neither in interface, abstract or
virtual

So how to implement such architecture???

TNX
 
N

Nicholas Paldino [.NET/C# MVP]

Tamir,

What do you mean all of the objects are in the same class with the
caller? That shouldn't mean that you can't use reflection, you just aren't
using it properly. Can you show what you mean? It would help point out how
to use reflection here.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
It seemed I can not use reflection here, due all of those objects are in
the same class with caller

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nicholas Paldino said:
Tamir,

Basically, you would want a class factory. You would pass the factory
the type, and it would return the interface or abstract base class.

When constructing the class, you would use reflection. You would get
the constructor from the Type for the implementation, and then call it,
passing the parameter. Since you can't define the contract through an
interface, you have to encapsulate the contract through a class factory.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
I have some classes that basicly do the same things but different way.
There are no default constructors in those classes each constructor
should recieve same value

So

Fooclass:MyBasicClass or Fooclass:IBasicClass

and I want to be able to call form program something like this:

MyBasicClass foo = new MyBasicClass(value);
or
IBasicInterface foo = new IBasicInterface(value);

Interfaces,Abstracts or Virtuals does not support constructors with
parameters

So trying:

MyBasicClass foo = MyBasicClass.GetInstance(value);
or
IBasicInterface foo = IBasicInterface.GetInstance(value);

and in each class something like
if(this=null)
return new Fooclass(value);
else
return this;

But I can not create static methods neither in interface, abstract or
virtual

So how to implement such architecture???

TNX
 
G

Guest

Well then you need to try each type. It isn't that Interfaces or Abstract
types don't take constructors with parameters, they don't have constructors
at all, they are Abstract. It wouldn't make any sense to implement an
interface or abstract type anyways. Since they have no implementation what
would you do with it? Just calling an Interface with a constructor and
passing a value won't call every class that implements that Interface, that
just isn't the way it works. You're going to need to go through each type and
try to call them. Or make a procedure that calls them with your variable and
then pass each type individually into it.

In my opinion you need to re-evaluate your design. If you have several
objects that all accept the same value, and only one will work with that then
perhaps the value itself needs to be an object and call each type that it can
work on to see which one it works with. Or if you want to just call one
function that calls many functions then maybe you can look into using a
delegate. But at some point, each of these types is going to have to be
individually instantiated using its concrete class, since there really is no
other way to do it.

Maybe create all the types into an array whose type is your interface type
and then iterate through the array (or collection) and perform the same
operation on each item in the array/collection. It is hard to give
suggestions without knowing more about your architecture.

Thanks,
Justin Etheredge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top