473,938 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance question

3 New Member
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!

Expand|Select|Wrap|Line Numbers
  1. public class Animal
  2.     {
  3.         public void Talk()
  4.         {
  5.             Console.WriteLine("Animal is talking");
  6.         }
  7.     }
  8.  
  9.     public class Cow:Animal
  10.     {
  11.         public void Moo()
  12.         {
  13.             Console.WriteLine("Moo...");
  14.         }
  15.     }
  16.  
  17.     public class Test
  18.     {
  19.         public void Run()
  20.         {
  21.             //If an animal doesn't know it's a cow when we initialize a program, can I make it moo()?
  22.             //or do I have to check its type such as this? It just seems awkward
  23.             Animal b;
  24.             if (GetType(b).ToString().Equals("Cow"))
  25.             {
  26.                 ((Cow)b).Moo();
  27.             }
  28.  
  29.         }
  30.     }
Aug 18 '09 #1
5 3370
GaryTexmo
1,501 Recognized Expert Top Contributor
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...

Expand|Select|Wrap|Line Numbers
  1. Cow myCow = new Cow();
  2. Animal someAnimal = myCow;
  3.  
  4. Cow animalAsCow = someAnimal as Cow;
  5. 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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Cow myCow = new Cow();
  12.             Pig myPig = new Pig();
  13.  
  14.             Console.WriteLine("Called from class");
  15.             Console.WriteLine("-----------------");
  16.             myCow.Talk();
  17.             myPig.Talk();
  18.  
  19.             Console.WriteLine();
  20.             Console.WriteLine("Called from interface");
  21.             Console.WriteLine("---------------------");
  22.             MakeTalk(myCow);
  23.             MakeTalk(myPig);
  24.         }
  25.  
  26.         static void MakeTalk(IAnimal someAnimal)
  27.         {
  28.             someAnimal.Talk();
  29.         }
  30.     }
  31.  
  32.     public interface IAnimal
  33.     {
  34.         void Talk();
  35.     }
  36.  
  37.     public class Cow : IAnimal
  38.     {
  39.         #region IAnimal Members
  40.  
  41.         public void Talk()
  42.         {
  43.             Console.WriteLine("Moo!");
  44.         }
  45.  
  46.         #endregion
  47.     }
  48.  
  49.     public class Pig : IAnimal
  50.     {
  51.         #region IAnimal Members
  52.  
  53.         public void Talk()
  54.         {
  55.             Console.WriteLine("Oink!");
  56.         }
  57.  
  58.         #endregion
  59.     }
  60. }
I hope that helps :)
Aug 18 '09 #2
shrekgal
3 New Member
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! :)
Aug 18 '09 #3
GaryTexmo
1,501 Recognized Expert Top Contributor
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 ;)
Aug 18 '09 #4
shrekgal
3 New Member
Thanks, GaryTexmo! You've been very helpful.
Aug 18 '09 #5
tlhintoq
3,525 Recognized Expert Specialist
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.
Aug 18 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3767
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2212
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
4
12827
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error below *should* happen, but my question to the community is *why* does it happen? Any answer will be appreciated, but a section and paragraph number from the C++ Standard would be especially appreciated.
8
7839
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
22
23409
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
45
6400
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
2112
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
5
2482
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1852
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
0
10125
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
9963
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
10649
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9852
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
8207
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
7377
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
6283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3495
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.