Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Design a Class

Design a Class

Scheduled Pinned Locked Moved C#
questiondesign
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rohit16db
    wrote on last edited by
    #1

    Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?

    D realJSOPR 2 Replies Last reply
    0
    • R Rohit16db

      Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Use an interface. Simple example below.

      public interface IMyInterface
      {
      void WriteMe();
      }
      public class A : IMyInterface
      {
      public void WriteMe()
      {
      Console.Write("A");
      }
      }
      public class B : IMyInterface
      {
      public void WriteMe()
      {
      Console.Write("B");
      }
      }
      public class C : IMyInterface
      {
      public void WriteMe()
      {
      Console.Write("C");
      }
      }
      public class D
      {
      public void WriteParameter(IMyInterface test)
      {
      test.WriteMe();
      }
      }

      Then you can do this

      D d = new D();
      d.WriteParameter(new A());
      d.WriteParameter(new B());
      // ...

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

      R 1 Reply Last reply
      0
      • R Rohit16db

        Can anyone suggest me how can I design a class of follwing kind. suppose i have 3 classes like Class A, Class B, Class C. All these classes have same method like returning collection of record of their own type. e.g. collection GetAllRecord() { returning collection of A; } Now I want to a common method in different class for calling above method e.g Class D { Getall(A) { A.GetAllRecord(); } } How could i design this method to take agument of different type A B C...?

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        If I understand what you want (and I probably don't), what you want to do is called oveloading.

        public class A
        {
        private List<a> list = new List<a>();
        public List<a> GetAllRecords() { get { return list; } }
        }
        public class B
        {
        private List<b> list = new List<b>();
        public List<b> GetAllRecords() { get { return list; } }
        }
        public class C
        {
        private List<c> list = new List<c>();
        public List<c> GetAllRecords() { get { return list; } }
        }

        public class D
        {
        private A a = new A();
        private B b = new B();
        private C c = new C();

        private List<a> aList;
        private List<b> bList;
        private List<c> cList;
        
        public D()
        {
        }
        
        private List<a> GetAllRecords(A a)
        {
            aList = a.GetAllRecords;
        }
        
        private List<b> GetAllRecords(B b)
        {
            bList = b.GetAllRecords;
        }
        
        private List<c> GetAllRecords(C c)
        {
            cList = c.GetAllRecords;
        }
        

        }

        [EDIT] Forgot to convert the pointy brackets...

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        modified on Saturday, October 11, 2008 5:08 AM

        1 Reply Last reply
        0
        • D DaveyM69

          Use an interface. Simple example below.

          public interface IMyInterface
          {
          void WriteMe();
          }
          public class A : IMyInterface
          {
          public void WriteMe()
          {
          Console.Write("A");
          }
          }
          public class B : IMyInterface
          {
          public void WriteMe()
          {
          Console.Write("B");
          }
          }
          public class C : IMyInterface
          {
          public void WriteMe()
          {
          Console.Write("C");
          }
          }
          public class D
          {
          public void WriteParameter(IMyInterface test)
          {
          test.WriteMe();
          }
          }

          Then you can do this

          D d = new D();
          d.WriteParameter(new A());
          d.WriteParameter(new B());
          // ...

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          R Offline
          R Offline
          Rohit16db
          wrote on last edited by
          #4

          Thanks a lot Dave this code worked for me.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups