Make an alias for a static class?

  • Thread starter Frank K. Jensen
  • Start date
F

Frank K. Jensen

Hi guys,

I need to control 3 different measurement devices. They are all very
different and need to be controlled differently. However they all measure
the same thing, and will never be used at the same time, hence I would like
to be able to just call 'measure()' regardless of with device I use.

My idea is (and I am completely new to C#) to make three different static
classes (they don't have to be static, but I plan on using properties for
setting up the devices, so two instances of the same class will be a mess
(also, there is only one physical device pr. class)). These classes will be
called e.g. device1, device2 and device3, and as mentioned they will have
different properties and perhaps some methods for setting up the device.
However they will all have one method called 'measure()'. Setting up the
device will of cause be done by calling the proper class, but how can I
access the 'measure()' method without calling the individual classes (e.g.
device1.measure())? I would like to be able to just call 'measure()' or
'device.measure()' (i.e. without a number).

If I could make an alias for the class I could do something like this:

if (device2 selected) set device = device2
and then just call device.measure() which then would be an alias for
device2.measure()

Of cause I can make a separate method just for choosing which class to call
measure() from, and I will properly just do that, if you guys can't find a
better solution.

Any ideas/suggestions?

Best regards,
Frank
 
A

Arne Vajhøj

Hi guys,

I need to control 3 different measurement devices. They are all very
different and need to be controlled differently. However they all
measure the same thing, and will never be used at the same time, hence I
would like to be able to just call 'measure()' regardless of with device
I use.

My idea is (and I am completely new to C#) to make three different
static classes (they don't have to be static, but I plan on using
properties for setting up the devices, so two instances of the same
class will be a mess (also, there is only one physical device pr.
class)). These classes will be called e.g. device1, device2 and device3,
and as mentioned they will have different properties and perhaps some
methods for setting up the device. However they will all have one method
called 'measure()'. Setting up the device will of cause be done by
calling the proper class, but how can I access the 'measure()' method
without calling the individual classes (e.g. device1.measure())? I would
like to be able to just call 'measure()' or 'device.measure()' (i.e.
without a number).

If I could make an alias for the class I could do something like this:

if (device2 selected) set device = device2
and then just call device.measure() which then would be an alias for
device2.measure()

Of cause I can make a separate method just for choosing which class to
call measure() from, and I will properly just do that, if you guys can't
find a better solution.

3 non-static classes all implementing the same interface.

If you want to enforce the one instance strongly then use the
old GoF singleton pattern.

If slightly less strictness is needed you could use a DI
framework and instruct it always return the same instance.

Arne
 
F

Frank K. Jensen

"Arne Vajhøj" skrev i meddelelsen
3 non-static classes all implementing the same interface.

But then I will need two instances, right? One for the interface and one for
the class.
If I need two instances, I can not use it, as my setting (the properties)
will be in one instance (the class) whereas the 'measure()' method will in
another instance (the interface).
Or am I missing something here?
If you want to enforce the one instance strongly then use the
old GoF singleton pattern.

I have no idea what you are talking about :) but if it is legacy, I think I
will skip it.
If slightly less strictness is needed you could use a DI
framework and instruct it always return the same instance.

Again I have no idea what DI framework is (I am completely new at this :)),
but of cause I don't mind reading up on it. However, before I spend a lot of
time doing so, could you elaborate on "less strictness"?

The idea with a separate method for choosing with 'measure()' to call, is
starting to look like the easiest solution...

Best regards,
Frank
 
A

Arne Vajhøj

"Arne Vajhøj" skrev i meddelelsen


But then I will need two instances, right? One for the interface and one
for the class.

No.

You will have one instance of the class and the class
implments the interface.
If I need two instances, I can not use it, as my setting (the
properties) will be in one instance (the class) whereas the 'measure()'
method will in another instance (the interface).
Or am I missing something here?

I think so.

Have you read about interfaces in C#?
I have no idea what you are talking about :) but if it is legacy, I
think I will skip it.

It is one way to make sure you only have one instance.

I would not call it legacy.
Again I have no idea what DI framework is (I am completely new at this
:)), but of cause I don't mind reading up on it. However, before I
spend a lot of time doing so, could you elaborate on "less strictness"?

DI = Dependency Injection

If you are just starting with C#, then do not start with DI now.

It is less strict in the sense that if developers remember to
use the DI framework to get the instance, then there will
only be one instance, but it is still possible for a
developer top use new to construct an extra instance.

Arne
 
F

Frank K. Jensen

"Arne Vajhøj" skrev i meddelelsen
No.

You will have one instance of the class and the class
implments the interface.


I think so.

Have you read about interfaces in C#?

Yes, I have :) but being new to C#, there is a good chance that I have
misunderstood some of it.
Here is what I have tried:

interface Interface1
{
void DeclaredInTheInterface();
}

class Class1 : Interface1
{
public int x { get; set; }

public void DeclaredInTheInterface()
{
Console.WriteLine("This was declared in the interface, but defined
in the class " + x);
}

public void OnlyDeclaredInTheClass()
{
Console.WriteLine("This was only defined in the class " + x);
}
}

class Program
{
static void Main(string[] args)
{
Interface1 Itf; // Itf is declared as Interface1
but the methods have no content before it is also instaciated as a class
Itf = new Class1();
Class1 Cls1 = new Class1();

//Itf.x = 1; // will not work as the property
x is not known in the interface
Cls1.x = 2;

Itf.DeclaredInTheInterface(); // will use the definition of
DeclaredInTheInterface() from Class1
//Itf.OnlyDeclaredInTheClass(); // will not work as
OnlyDeclaredInTheClass() is not known in the interface

Cls1.DeclaredInTheInterface();
Cls1.OnlyDeclaredInTheClass();

Console.ReadLine();

}
}

What should I do to call 'DeclaredInTheInterface()' and have it use the
method from Class1 (there are other classes that used the same interface
with different methods for 'DeclaredInTheInterface()'), with a general name
(i.e. not the name used to set the properties) and without making another
instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the method but
this time from Class2.

Can this be done with interfaces and if so, how?
It is one way to make sure you only have one instance.

I would not call it legacy.


DI = Dependency Injection

If you are just starting with C#, then do not start with DI now.

It is less strict in the sense that if developers remember to
use the DI framework to get the instance, then there will
only be one instance, but it is still possible for a
developer top use new to construct an extra instance.

Will it do what I have descibed above?

~Frank
 
A

Arne Vajhøj

"Arne Vajhøj" skrev i meddelelsen
I think so.

Have you read about interfaces in C#?

Yes, I have :) but being new to C#, there is a good chance that I have
misunderstood some of it.
Here is what I have tried:

interface Interface1
{
void DeclaredInTheInterface();
}

class Class1 : Interface1
{
public int x { get; set; }

public void DeclaredInTheInterface()
{
Console.WriteLine("This was declared in the interface, but defined in
the class " + x);
}

public void OnlyDeclaredInTheClass()
{
Console.WriteLine("This was only defined in the class " + x);
}
}

class Program
{
static void Main(string[] args)
{
Interface1 Itf; // Itf is declared as Interface1 but the methods have no
content before it is also instaciated as a class
Itf = new Class1();
Class1 Cls1 = new Class1();

//Itf.x = 1; // will not work as the property x is not known in the
interface
Cls1.x = 2;

Itf.DeclaredInTheInterface(); // will use the definition of
DeclaredInTheInterface() from Class1
//Itf.OnlyDeclaredInTheClass(); // will not work as
OnlyDeclaredInTheClass() is not known in the interface

Cls1.DeclaredInTheInterface();
Cls1.OnlyDeclaredInTheClass();

Console.ReadLine();

}
}

What should I do to call 'DeclaredInTheInterface()' and have it use the
method from Class1 (there are other classes that used the same interface
with different methods for 'DeclaredInTheInterface()'), with a general
name (i.e. not the name used to set the properties) and without making
another instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

"Arne Vajhøj" skrev i meddelelsen


Will it do what I have descibed above?

A DI framework will allow you say:

ISomething o = somedithing.Get<ISomething>();

and somedithing will give you a SomethingA or a SomethingB
depending on configuration.

And you can configure somedithing to give you the same
instance every time.

Arne
 
F

Frank K. Jensen

"Arne Vajhøj" skrev i meddelelsen
"Arne Vajhøj" skrev i meddelelsen

What should I do to call 'DeclaredInTheInterface()' and have it use the
method from Class1 (there are other classes that used the same interface
with different methods for 'DeclaredInTheInterface()'), with a general
name (i.e. not the name used to set the properties) and without making
another instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from
Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Thanks for the example, but the call to the method is not generic as I have
to call Test(a) and Test(b).
However it got me thinking, and I tried this:

-----
// Itf is declared as an interface
Interface1 Itf;

Class1 Cls1 = new Class1();
Class2 Cls2 = new Class2();

// Here is the magic :)
Itf = Cls1;
// Itf will use DeclaredInTheInterface() from Class1
Itf.DeclaredInTheInterface();

// will not work as it is unknown for the interface
//Itf.OnlyDeclaredInTheClass();

Itf = Cls2;
// Itf will now use DeclaredInTheInterface() from Class2
Itf.DeclaredInTheInterface();

// I can not accedently make new instance (and reset the properties) if I
use the same name. Which is good, if it is true...
//Class1 Cls1 = new Class1();
 
A

Arne Vajhøj

"Arne Vajhøj" skrev i meddelelsen
"Arne Vajhøj" skrev i meddelelsen

What should I do to call 'DeclaredInTheInterface()' and have it use the
method from Class1 (there are other classes that used the same interface
with different methods for 'DeclaredInTheInterface()'), with a general
name (i.e. not the name used to set the properties) and without making
another instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from > Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Thanks for the example, but the call to the method is not generic as I
have to call Test(a) and Test(b).

You can not make a call without making a call.

The point is that Test just uses the interface.

However it got me thinking, and I tried this:

-----
// Itf is declared as an interface
Interface1 Itf;

Class1 Cls1 = new Class1();
Class2 Cls2 = new Class2();

// Here is the magic :)
Itf = Cls1;
// Itf will use DeclaredInTheInterface() from Class1
Itf.DeclaredInTheInterface();

// will not work as it is unknown for the interface
//Itf.OnlyDeclaredInTheClass();

Itf = Cls2;
// Itf will now use DeclaredInTheInterface() from Class2
Itf.DeclaredInTheInterface();

// I can not accedently make new instance (and reset the properties) if
I use the same name. Which is good, if it is true...
//Class1 Cls1 = new Class1();

You can do it simpler:

Interface1 Itf;
Itf = new Class1();
Itf.DeclaredInTheInterface();
Itf = new Class2();
Itf.DeclaredInTheInterface();

It is actually the same point as in my code except that
my code shifted to interface in the call - and in these two
code fragments it is done in assignment.

Class1 Cls1 = new Class1();

is still possible.

This is what singleton/DI has to solve.

Arne
 
F

Frank K. Jensen

"Arne Vajhøj" skrev i meddelelsen
"Arne Vajhøj" skrev i meddelelsen
On 11/11/2011 10:35 AM, Frank K. Jensen wrote:
"Arne Vajhøj" skrev i meddelelsen

What should I do to call 'DeclaredInTheInterface()' and have it use
the
method from Class1 (there are other classes that used the same
interface
with different methods for 'DeclaredInTheInterface()'), with a
general
name (i.e. not the name used to set the properties) and without
making
another instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from >
Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the
method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Thanks for the example, but the call to the method is not generic as I
have to call Test(a) and Test(b).

You can not make a call without making a call.

I know :) but Test(a) and Test(b) are two different calls, which is what I
am trying to avoid. I just want to call Test().
The point is that Test just uses the interface.



You can do it simpler:

Interface1 Itf;
Itf = new Class1();
Itf.DeclaredInTheInterface();
Itf = new Class2();
Itf.DeclaredInTheInterface();

It is actually the same point as in my code except that
my code shifted to interface in the call - and in these two
code fragments it is done in assignment.

Class1 Cls1 = new Class1();
is still possible.

But then Itf and Cls1 will be two different instances, and the properties
set in Cls1 will not be used, when I call methods through Itf, right?
In my code above, methods called through the interface will be from the same
instance as Cls1 (or Cls2 when I set Itf=Cls2).

~Frank
 
A

Arne Vajhøj

"Arne Vajhøj" skrev i meddelelsen
"Arne Vajhøj" skrev i meddelelsen

On 11/11/2011 10:35 AM, Frank K. Jensen wrote:
"Arne Vajhøj" skrev i meddelelsen

What should I do to call 'DeclaredInTheInterface()' and have it use >> > the
method from Class1 (there are other classes that used the same
interface
with different methods for 'DeclaredInTheInterface()'), with a >>
general
name (i.e. not the name used to set the properties) and without
making
another instance of Class1 (which will not use the same properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from >
Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the
method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Thanks for the example, but the call to the method is not generic as I
have to call Test(a) and Test(b).

You can not make a call without making a call.

I know :) but Test(a) and Test(b) are two different calls, which is
what I am trying to avoid. I just want to call Test().

You want to make one call, but you need to have that call
work with two different implementations.

That is easier to show working by using two calls in the test.
But then Itf and Cls1 will be two different instances, and the
properties set in Cls1 will not be used, when I call methods through
Itf, right?
In my code above, methods called through the interface will be from the
same instance as Cls1 (or Cls2 when I set Itf=Cls2).

Interfaces is what enables you two treat two implementations the
same way.

It has nothing two do with ensuring only one instance of the
implementation.

That requires different solutions (discussed previously).

And there is no difference in that regard between your code
and my code.

Arne
 
F

Frank K. Jensen

"Arne Vajhøj" skrev i meddelelsen
"Arne Vajhøj" skrev i meddelelsen
On 11/12/2011 10:09 AM, Frank K. Jensen wrote:
"Arne Vajhøj" skrev i meddelelsen

On 11/11/2011 10:35 AM, Frank K. Jensen wrote:
"Arne Vajhøj" skrev i meddelelsen

What should I do to call 'DeclaredInTheInterface()' and have it
use >> > the
method from Class1 (there are other classes that used the same
interface
with different methods for 'DeclaredInTheInterface()'), with a >>
general
name (i.e. not the name used to set the properties) and without
making
another instance of Class1 (which will not use the same
properties)?

I.e.
I only want one instance of Class1.
I want to use Cls1.x to set the properties of this instance.
I want to use Itf.DeclaredInTheInterface() to call the method from

Class1.

At a later time, I want one instance of Class2.
I want to use Cls2.x to set the properties of this instance.
But I still want to use Itf.DeclaredInTheInterface() to call the
method
but this time from Class2.

Can this be done with interfaces and if so, how?

Example:

using System;

namespace E
{
public interface ISomething
{
void Tell();
}
public class SomethingA : ISomething
{
public void Tell()
{
Console.WriteLine("I am an A");
}
}
public class SomethingB : ISomething
{
public void Tell()
{
Console.WriteLine("I am a B");
}
}
public class Program
{
// Test does only know about the interface
public static void Test(ISomething o)
{
o.Tell();
}
public static void Main(string[] args)
{
SomethingA a = new SomethingA();
// here we could call SomethingA specific members
Test(a);
SomethingB b = new SomethingB();
// here we could call SomethingA specific members
Test(b);
Console.ReadKey();
}
}
}

Thanks for the example, but the call to the method is not generic as
I
have to call Test(a) and Test(b).

You can not make a call without making a call.

I know :) but Test(a) and Test(b) are two different calls, which is
what I am trying to avoid. I just want to call Test().

You want to make one call, but you need to have that call
work with two different implementations.

That is easier to show working by using two calls in the test.
But then Itf and Cls1 will be two different instances, and the
properties set in Cls1 will not be used, when I call methods through
Itf, right?
In my code above, methods called through the interface will be from the
same instance as Cls1 (or Cls2 when I set Itf=Cls2).

Interfaces is what enables you two treat two implementations the
same way.

It has nothing two do with ensuring only one instance of the
implementation.

That requires different solutions (discussed previously).

And there is no difference in that regard between your code
and my code.

Thanks for all your help. I am not sure I understand interfaces 100% but
then again I am new at C#.
If it is not "wrong" what I am doing, I thing I'll just do it the way I
wrote, as it seems simple, and I think I understand it :)

~Frank
 

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