RE: Is it right strategy for control two similar instrument?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



As an alternative, I would create a base class and inherit from it. This
would allow you to share common functionality:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int id = 5;
machine src;
instrument1 ins1 = new instrument1(ref id);
instrument2 ins2 = new instrument2(ref id);

Console.WriteLine("Select instrument 1 or 2");
string input = Console.ReadLine();
if (input =="1")
{
src = ins1;
src.turn_on();
}
else if (input == "2")
{
src = ins2;
src.turn_on();
}
}
}
public class machine
{
private int id;

public machine(ref int _id)
{
id = _id;
}
abstract void turn_on();
}
public class instrument1 : machine
{
public instrument1(ref int id_) : base(id_)
{
Console.WriteLine(" instrument 1 is initilized");
}
public override void turn_on()
{
Console.WriteLine("turn on");
}
}
public class instrument2 : machine
{
public instrument2(ref int id_) : base(id_)
{
id = id_;
Console.WriteLine(" instrument 2 is initilized");
}
public override void turn_on()
{
Console.WriteLine("power on");
Console.WriteLine("I need more step -- plevel 1");
}
}
}

Now you can instantiate objects as either instrument1 or instrument2 but
refer to them as type machine.

--
Jeffrey Hornby
Hornby Consulting, Inc.



"lotus" wrote:

Hi all.

I want to control two different instrtument but have simliar
functionality.
Acually the fisrt one is controlled by using serial communication, and
the other is controlled by LAN communication.
The number of instruments will be extetned by 5 or more.
So If user select instrument (ex. by selection of combo box items), I
must do right procedure of contolling proper instrument.

I want to contol these instrument by same command. so I select
following strategy by using INTERFACE keyword of C#.

Is it right strategy? Is there more powerful method?

---lotus----

In the following code, Console.WriteLine is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int id = 5;
machine src;
instrument1 ins1 = new instrument1(ref id);
instrument2 ins2 = new instrument2(ref id);

Console.WriteLine("Select instrument 1 or 2");
string input = Console.ReadLine();
if (input =="1")
{
src = ins1;
src.turn_on();
}
else if (input == "2")
{
src = ins2;
src.turn_on();
}
}
}
public interface machine
{
void turn_on();
}
public class instrument1 : machine
{
int id = 3;
public instrument1(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 1 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLine(" instrument 2 is initilized");
}
void machine.turn_on()
{
Console.WriteLine("power on");
Console.WriteLine("I need more step -- plevel 1");
}
}
}

------------------ end of code ----------------


.



Relevant Pages