Inheritance question | Newbie | | Join Date: Aug 2009
Posts: 3
| |
Hello,
I am a C# newbie. I want to create a barcode reader engine library, with base class that supports basic functions, but there are many brands of barcode and there are some brands that have specific functions. I don't know ahead of time which brand runs the program. How do I call a specific function of that brand without it getting awkward?
Do I have to check its type and then cast it such as the example below?
Thanks in advance! - public class Animal
-
{
-
public void Talk()
-
{
-
Console.WriteLine("Animal is talking");
-
}
-
}
-
-
public class Cow:Animal
-
{
-
public void Moo()
-
{
-
Console.WriteLine("Moo...");
-
}
-
}
-
-
public class Test
-
{
-
public void Run()
-
{
-
//If an animal doesn't know it's a cow when we initialize a program, can I make it moo()?
-
//or do I have to check its type such as this? It just seems awkward
-
Animal b;
-
if (GetType(b).ToString().Equals("Cow"))
-
{
-
((Cow)b).Moo();
-
}
-
-
}
-
}
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 211
| | | re: Inheritance question
As I understand it, if you're dealing with an object of the base type, you can only access things defined for that type. Any object that inherits the base can have extra stuff on it, but if you're looking at the base type, you're stuck with it.
So in your case, only cows can moo, but you don't have to check the type (per se). It's a roundabout example, but hopefully it makes sense... - Cow myCow = new Cow();
-
Animal someAnimal = myCow;
-
-
Cow animalAsCow = someAnimal as Cow;
-
if (animalAsCow != null) animalAsCow.Moo();
Another option would be to use an interface, or an abstract class, for your base class which defines a method (such as talk). Then each class that inherits the base will implement that method, which means any object can call it, so long as it belongs to that family. - using System;
-
using System.Collections.Generic;
-
using System.Text;
-
-
namespace ConsoleApplication1
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Cow myCow = new Cow();
-
Pig myPig = new Pig();
-
-
Console.WriteLine("Called from class");
-
Console.WriteLine("-----------------");
-
myCow.Talk();
-
myPig.Talk();
-
-
Console.WriteLine();
-
Console.WriteLine("Called from interface");
-
Console.WriteLine("---------------------");
-
MakeTalk(myCow);
-
MakeTalk(myPig);
-
}
-
-
static void MakeTalk(IAnimal someAnimal)
-
{
-
someAnimal.Talk();
-
}
-
}
-
-
public interface IAnimal
-
{
-
void Talk();
-
}
-
-
public class Cow : IAnimal
-
{
-
#region IAnimal Members
-
-
public void Talk()
-
{
-
Console.WriteLine("Moo!");
-
}
-
-
#endregion
-
}
-
-
public class Pig : IAnimal
-
{
-
#region IAnimal Members
-
-
public void Talk()
-
{
-
Console.WriteLine("Oink!");
-
}
-
-
#endregion
-
}
-
}
I hope that helps :)
| | Newbie | | Join Date: Aug 2009
Posts: 3
| | | re: Inheritance question
Hi GaryTexmo,
Thanks for your reply. I understand that interface can be used when we have two different objects with the same functionalities. However, I am just wondering how to tackle when one Animal has an extra functionality compared to other animal? For example, all barcodes can do Scan(), but barcode with brand ABC and XYZ can do MoreThanScan()? If I have a Form with a button called 'Run MoreThanScan' but that Form only has an object of type BarCode (which is the parent class), how do I call MoreThanScan()? Do I have to check if _myBarCode's type is either ABC or XYZ first?
Sorry if this sounds confusing! :)
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 211
| | | re: Inheritance question
As mentioned in the first section of my post, yes you'll have to do a check on the type, or a cast. Sorry... unless there's another way (which there could be, but it seems unlikely in this case), you can only methods that belong to, or are inherited from, the object of the type you currently have.
And I know, this does all get confusing, hopefully what I said there makes sense ;)
| | Newbie | | Join Date: Aug 2009
Posts: 3
| | | re: Inheritance question
Thanks, GaryTexmo! You've been very helpful.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: Inheritance question
Barcode readers really come in three connection flavors:
Serial port
USB port
Wireless
And two reading technologies:
Laser reflective
Camera
Many of the USB port readers imitate a serial port. Once the driver is installed you will notice you have a new "USB serial port" of COM5 or whatever number is next available.
Many of the wireless/bluetooth do the same thing.
The serial port type are nice because serial is simple and they will all share a lot of common code.
All scanners will need some type of configuration before you can use them. They need to be initialized to act the way you want them to act.
Do you want them constantly reading, or only when their trigger is pulled?
Do you want them only reading CODE39, or only POSTCODE, or everything they are capable of?
Do you want them polling every 1 second, or every 200mSec?
Do you want a beep tone on a successful read?
Every scanner will be different for their communication protocol. One brand will send a different command for setting each parameter. Another will require you to send a single configuration string where each letter in the string represents a feature setting. Another will expect a series of bytes not characters.
If the device is serial or pretends to be serial then you also need to configure that port for baud, parity, stop bits and so on.
Every scanner will return its value differently. If your barcode is "123456789" once scanner might return that as clear text. One might send a string like "0Cb4 123456789" to say it was a CODE39 read of that number. One might send the entire packet in hex bytes.
For every scanner you want to support you will need to get the maker's SDK so you know what to send, how to send it and what to expect in return. For scanners that are just being serial port, you will probably need the maker's DLL's to communicate with the scanner.
You will need to create methods for each model, inside each brand of scanner. [SARCASM] It's lots of fun! [/SARCASM]
Then of course there are the RFID scanners, magstripe scanners, HID scanners, and barcode scanners.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|