473,772 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it right strategy for control two similar instrument?

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.WriteLi ne is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
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.WriteLi ne("Select instrument 1 or 2");
string input = Console.ReadLin e();
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.WriteLi ne(" instrument 1 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLi ne(" instrument 2 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("power on");
Console.WriteLi ne("I need more step -- plevel 1");
}
}
}

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

Apr 24 '06 #1
3 1432
As an alternative, I would create a base class and inherit from it. This
would allow you to share common functionality:

namespace ConsoleApplicat ion1
{
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.WriteLi ne("Select instrument 1 or 2");
string input = Console.ReadLin e();
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.WriteLi ne(" instrument 1 is initilized");
}
public override void turn_on()
{
Console.WriteLi ne("turn on");
}
}
public class instrument2 : machine
{
public instrument2(ref int id_) : base(id_)
{
id = id_;
Console.WriteLi ne(" instrument 2 is initilized");
}
public override void turn_on()
{
Console.WriteLi ne("power on");
Console.WriteLi ne("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.WriteLi ne is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
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.WriteLi ne("Select instrument 1 or 2");
string input = Console.ReadLin e();
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.WriteLi ne(" instrument 1 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLi ne(" instrument 2 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("power on");
Console.WriteLi ne("I need more step -- plevel 1");
}
}
}

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

Apr 24 '06 #2
The use of an interface is good but since the ID stuff is common you should
probably have an abstract base class as well or instead just to hold that.

There is no reason to use "ref" anywhere.

Use a Dictionary<stri ng,machine> to look up the machine from the id rather
than a switch [then it is just dict[input].turn_on()].

"lotus" <lo*******@gmai l.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
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.WriteLi ne is acaully substitution of
sending command to instrument.
-----------Code start-------------------------------
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
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.WriteLi ne("Select instrument 1 or 2");
string input = Console.ReadLin e();
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.WriteLi ne(" instrument 1 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("turn on");
}
}
public class instrument2 : machine
{
int id = 5;
public instrument2(ref int id_)
{
id = id_;
Console.WriteLi ne(" instrument 2 is initilized");
}
void machine.turn_on ()
{
Console.WriteLi ne("power on");
Console.WriteLi ne("I need more step -- plevel 1");
}
}
}

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

Apr 24 '06 #3
Thank you, Jeffrey and Nick

Actually, I didn't know about abstract and generics.
But from your recommendation. I could know about power of that.

Generic functionality of .Net 2.0 is really fantastic to me.

-lotus-

Apr 24 '06 #4

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

Similar topics

4
1989
by: Claudio Jolowicz | last post by:
I am trying to find a solution to the following design problem (code at the bottom): We are implementing a trader agent that can trade with other traders on an electronical trading platform. To make the trader more extensible, we have defined a strategy interface and implemented this interface for different trading strategies. The problem relates to how to connect the trader and the strategy. The problem is tricky because the strategy...
4
1664
by: Ney André de Mello Zunino | last post by:
Hello. Given a sorted collection of strings, what would a good (the best?) strategy be to allow fast access to an item, based on a search substring which should match the beginning of the searched item? The actual goal is to implement a functionality similar to that found in help indices, where one can locate an item by gradually typing its initial characters. I expect that some kind of tree structure be present in the solution, but I...
4
2676
by: Kathy | last post by:
I have a form to generate a report. On the form are several textboxes and a "Generate" button. I would like to have the button grayed (not enabled), unless the data in each of the dependant textboxes is valid. What is the proper procedure for doing this? It seems the Validating and Validated events for each control only fire when it loses focus. I would like the button to be enabled as soon as the data is valid (before leaving the...
7
1390
by: Adine | last post by:
Hi, I need to develop a program to control electronic instruments. We have so many different instruments but we can group them. Each group has some common functions. When user choose an instrument, the other instruments may not be needed at all. So I was thinking if I want to write one executable file and put all instruments functions in it, I will have a huge file and user just needs 2% of that. So I thought that could be better to have...
2
1272
by: Mike D | last post by:
I have a windows control in an aspx page. It receives data from some serial hardware on the client. How do I get this data to an aspx page so that I can post it to the server? I will need to post an instrument id to the server and return some data to the usercontrol. How is this done?? Thanks Mike
5
8665
by: itsme321 | last post by:
Hi I am from science background and learnt little bit of programming in C,Matlab,C#. My task in hand is to interface an instrument through RS 232. I am using the serialport class but not able to communicate with the instrument . So kindly help me. Its completely educational purpose (no profit making) so reply soon..... awaitng expert reply.....................
5
1334
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 with Apache 2.2 on LInux. I have been building a web site, and now a client wants that same site, only he wants all the graphics substituted with his own. There could potentially be more clients who want the same site but with custom graphics. What makes more sense? 1. Maintain a single code base, only have "if/else" blocks when it comes to "<img src ...>" tags?
7
1957
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize, StringBuilder etc.. }
0
1268
by: yanzinmeister | last post by:
Hello everyone, i'm new user of python, in fact, i'm using Control Desk from dsPACE. I just want to change the programm a bit to fit to my application. I draw xyplot using virsual instrument xyplot in Control Desk, and i want to delect any curses if it's needed. But in Control Desk the xyplot is time signal, i can not simply delect it. The visual instrument is set up from python programming.
0
9621
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
9454
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
10106
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
10039
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
8937
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
7461
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
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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...
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.