Inheritance Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • z71mdridin

    #1

    Inheritance Question

    I have three classes:

    Public MustInherit Class Animal
    Public MustOverride Function speak() As String
    End Class
    Public Class dog
    Inherits Animal
    Public Overrides Function speak() As String
    Return "Bark"
    End Function
    End Class
    Public Class cat
    Inherits Animal
    Public Overrides Function speak() As String
    Return "Meow"
    End Function
    End Class

    I want to create a front-end and allow the user to choose dog or cat
    from a drop down and then call the appropriate method. Is this
    possible to do without using a case statement? I would like to create
    an instance of animal and then cast it to whatever the user has
    chosen. Is this possible in vb.net?

    Dim testanimal As Animal
    CType(testanima l, Combobox1.Selec tedValue)
    testanimal.spea k()
  • Tom Shelton

    #2
    Re: Inheritance Question

    On 2008-01-17, z71mdridin <z71mdridin@gma il.comwrote:
    I have three classes:
    >
    Public MustInherit Class Animal
    Public MustOverride Function speak() As String
    End Class
    Public Class dog
    Inherits Animal
    Public Overrides Function speak() As String
    Return "Bark"
    End Function
    End Class
    Public Class cat
    Inherits Animal
    Public Overrides Function speak() As String
    Return "Meow"
    End Function
    End Class
    >
    I want to create a front-end and allow the user to choose dog or cat
    from a drop down and then call the appropriate method. Is this
    possible to do without using a case statement? I would like to create
    an instance of animal and then cast it to whatever the user has
    You can't create an instance of animal, it is abstract...
    chosen. Is this possible in vb.net?
    >
    Dim testanimal As Animal
    CType(testanima l, Combobox1.Selec tedValue)
    testanimal.spea k()
    ComboBox1.Items .Add (New Dog())
    ComboBox1.ITems .Add (New Cat())

    ....

    Dim TestAnimal As Animal = CType (ComboBox1.Sele ctedValue, Animal)
    TestAnimal.Spea k()


    --
    Tom Shelton

    Comment

    • Armin Zingler

      #3
      Re: Inheritance Question

      <jonathan.white hurst@gmail.com schrieb
      What I would really want to do is store the animal types in a
      database and then at run-time display those types in a list box and
      allow the user to choose. I would like to be able to add new animal
      types( chicken, cow, etc) and only add the new class to the class
      library and then add a new record to the database without having to
      make any modifications to the front end code. I am getting the
      feeling this is not possible though, as I will always need to
      ComboBox.Add(Ne w ComboItem(GetTy pe("cow"))) for each new animal
      subclass I add. Am I correct that this is not possible? Thanks for
      the help!
      No, it is possible. Before, you've described a different situation.
      That's why I manually used "..GetType. ." for each class available in the
      example.

      You can load the type names from the database and add them to a Combobox
      in a loop without knowing how many there will be. You will have to plan
      which information about the type you want to store in the database. A
      type name can contain a namespace, and to be exact, the information
      about the assembly containing the class could also be stored. I assume
      it's the same assembly.

      For example, if you loaded the type names into an array called
      'TypeNames':

      With Me.ComboBox1.It ems
      For Each TypeName As String In TypeNames
      .Add(New ComboItem( _
      Type.GetType("W indowsApplicati on1.Form1+" & TypeName) _
      ))
      Next
      End With



      However, as you're using Reflection anyway, you might retrieve the
      available types from the executing assembly. This way you could get rid
      of the redundant storage in the database. Have a look at the method
      Assembly.GetTyp es. In addition, you can add your own attributes
      to the class if something from the database is missing.


      Armin

      Comment

      • Spam Catcher

        #4
        Re: Inheritance Question

        z71mdridin <z71mdridin@gma il.comwrote in news:d6facd9d-48ec-41b5-b654-
        087b73f760f4@s1 2g2000prg.googl egroups.com:
        I want to create a front-end and allow the user to choose dog or cat
        from a drop down and then call the appropriate method. Is this
        possible to do without using a case statement? I would like to create
        an instance of animal and then cast it to whatever the user has
        chosen. Is this possible in vb.net?
        Yes this is possible - you can use reflection to iterate through all types
        in your project. If the type inherits from Animal (your base type), you add
        it to the list.

        It's actually quite easy to do:

        For Each AssemblyType As Type In
        System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes
        'Check if base type inherits from Animal
        If AssemblyType.Ba seType Is GetType(Animal) Then
        'Load into combo Box here
        End If
        Next


        If you have multiple level inheritance, i.e. Animal -Mammal -Primates
        -Monkey, you'll need to write a recursive search to check the entire
        inheritance hierachy.
        --
        spamhoneypot@ro gers.com (Do not e-mail)

        Comment

        • Bill McCarthy

          #5
          Re: Inheritance Question


          "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
          news:Xns9A29D9F 39225usenethone ypotrogers@127. 0.0.1...
          z71mdridin <z71mdridin@gma il.comwrote in news:d6facd9d-48ec-41b5-b654-
          087b73f760f4@s1 2g2000prg.googl egroups.com:
          >
          >I want to create a front-end and allow the user to choose dog or cat
          >from a drop down and then call the appropriate method. Is this
          >possible to do without using a case statement? I would like to create
          >an instance of animal and then cast it to whatever the user has
          >chosen. Is this possible in vb.net?
          >
          Yes this is possible - you can use reflection to iterate through all types
          in your project. If the type inherits from Animal (your base type), you
          add
          it to the list.
          >
          It's actually quite easy to do:
          >
          For Each AssemblyType As Type In
          System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes
          'Check if base type inherits from Animal
          If AssemblyType.Ba seType Is GetType(Animal) Then
          'Load into combo Box here
          End If
          Next
          >
          >
          If you have multiple level inheritance, i.e. Animal -Mammal -Primates
          -Monkey, you'll need to write a recursive search to check the entire
          inheritance hierachy.

          It might be simpler to use Type.IsSubClass Of instead ;)





















          Comment

          • Spam Catcher

            #6
            Re: Inheritance Question

            "Bill McCarthy" <Bill@NOSPAM.co mwrote in news:FB95EB48-0B52-4248-886A-
            45B37EB01C6E@mi crosoft.com:
            It might be simpler to use Type.IsSubClass Of instead ;)
            I knew there must be a function to check the inheritance chain :-p

            --
            spamhoneypot@ro gers.com (Do not e-mail)

            Comment

            • z71mdridin

              #7
              Re: Inheritance Question

              Thanks for all the help so far. I am still getting an error though. I
              created a ComboItem class so that I could pass in name and value pair
              to the combox. However when retrieving from the combobox I get An
              unhandled exception of type 'System.Invalid CastException' occurred.
              Anyone have any ideas on what I am doing wrong?

              Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
              System.EventArg s) Handles MyBase.Load
              For Each AssemblyType As Type In
              System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes()
              'Check if base type inherits from Animal
              If AssemblyType.Ba seType Is GetType(Animal) Then
              'Load into combo Box here
              ComboBox1.Items .Add(New ComboItem(Assem blyType.Name,
              AssemblyType))
              End If
              Next
              End Sub

              Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
              System.Object, ByVal e As System.EventArg s) Handles
              ComboBox1.Selec tedIndexChanged
              ' Dim Item = CType(Me.ComboB ox1.SelectedIte m, ComboItem)
              'Dim Animal = DirectCast(Acti vator.CreateIns tance(Item.Type ),
              Animal)
              ' MsgBox(Animal.s peak())
              Dim ComboItem = CType(ComboBox1 .SelectedItem,
              ComboItem).Item Data
              Dim TestAnimal = CType(ComboItem , Animal)
              TestAnimal.spea k()
              End Sub

              Public Class ComboItem
              ' Declare the variable the property uses.
              Private strItemData As Type
              Private strItem As String
              Public Property ItemData() As Type
              Get
              Return strItemData
              End Get
              Set(ByVal ItemData As Type)
              strItemData = ItemData
              End Set
              End Property

              Public Property Item() As String
              Get
              Return strItem
              End Get
              Set(ByVal Item As String)
              strItem = Item
              End Set
              End Property
              Public Overrides Function ToString() As String
              Return strItem
              End Function
              Public Sub New()
              End Sub
              Public Sub New(ByVal Item As String, ByVal ItemData As Type)
              strItem = Item
              strItemData = ItemData
              End Sub
              End Class


              On Jan 18, 1:20 am, Spam Catcher <spamhoney...@r ogers.comwrote:
              z71mdridin <z71mdri...@gma il.comwrote in news:d6facd9d-48ec-41b5-b654-
              087b73f76...@s1 2g2000prg.googl egroups.com:
              >
              I want to create a front-end and allow the user to choose dog or cat
              from a drop down and then call the appropriate method. Is this
              possible to do without using a case statement? I would like to create
              an instance of animal and then cast it to whatever the user has
              chosen. Is this possible in vb.net?
              >
              Yes this is possible - you can use reflection to iterate through all types
              in your project. If the type inherits from Animal (your base type), you add
              it to the list.
              >
              It's actually quite easy to do:
              >
              For Each AssemblyType As Type In
              System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes
              'Check if base type inherits from Animal
              If AssemblyType.Ba seType Is GetType(Animal) Then
              'Load into combo Box here
              End If
              Next
              >
              If you have multiple level inheritance, i.e. Animal -Mammal -Primates
              -Monkey, you'll need to write a recursive search to check the entire
              inheritance hierachy.
              --
              spamhoney...@ro gers.com (Do not e-mail)

              Comment

              • jonathan.whitehurst@gmail.com

                #8
                Re: Inheritance Question

                Nevermind, I figured it out. I was using an old ComboItem class.
                Thanks for all your help!

                On Jan 18, 10:54 am, z71mdridin <z71mdri...@gma il.comwrote:
                Thanks for all the help so far. I am still getting an error though. I
                created a ComboItem class so that I could pass in name and value pair
                to the combox. However when retrieving from the combobox I get An
                unhandled exception of type 'System.Invalid CastException' occurred.
                Anyone have any ideas on what I am doing wrong?
                >
                Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
                System.EventArg s) Handles MyBase.Load
                For Each AssemblyType As Type In
                System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes()
                'Check if base type inherits from Animal
                If AssemblyType.Ba seType Is GetType(Animal) Then
                'Load into combo Box here
                ComboBox1.Items .Add(New ComboItem(Assem blyType.Name,
                AssemblyType))
                End If
                Next
                End Sub
                >
                Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As
                System.Object, ByVal e As System.EventArg s) Handles
                ComboBox1.Selec tedIndexChanged
                ' Dim Item = CType(Me.ComboB ox1.SelectedIte m, ComboItem)
                'Dim Animal = DirectCast(Acti vator.CreateIns tance(Item.Type ),
                Animal)
                ' MsgBox(Animal.s peak())
                Dim ComboItem = CType(ComboBox1 .SelectedItem,
                ComboItem).Item Data
                Dim TestAnimal = CType(ComboItem , Animal)
                TestAnimal.spea k()
                End Sub
                >
                Public Class ComboItem
                ' Declare the variable the property uses.
                Private strItemData As Type
                Private strItem As String
                Public Property ItemData() As Type
                Get
                Return strItemData
                End Get
                Set(ByVal ItemData As Type)
                strItemData = ItemData
                End Set
                End Property
                >
                Public Property Item() As String
                Get
                Return strItem
                End Get
                Set(ByVal Item As String)
                strItem = Item
                End Set
                End Property
                Public Overrides Function ToString() As String
                Return strItem
                End Function
                Public Sub New()
                End Sub
                Public Sub New(ByVal Item As String, ByVal ItemData As Type)
                strItem = Item
                strItemData = ItemData
                End Sub
                End Class
                >
                On Jan 18, 1:20 am, Spam Catcher <spamhoney...@r ogers.comwrote:
                >
                z71mdridin <z71mdri...@gma il.comwrote in news:d6facd9d-48ec-41b5-b654-
                087b73f76...@s1 2g2000prg.googl egroups.com:
                >
                I want to create a front-end and allow the user to choose dog or cat
                from a drop down and then call the appropriate method. Is this
                possible to do without using a case statement? I would like to create
                an instance of animal and then cast it to whatever the user has
                chosen. Is this possible in vb.net?
                >
                Yes this is possible - you can use reflection to iterate through all types
                in your project. If the type inherits from Animal (your base type), you add
                it to the list.
                >
                It's actually quite easy to do:
                >
                For Each AssemblyType As Type In
                System.Reflecti on.Assembly.Get ExecutingAssemb ly.GetTypes
                'Check if base type inherits from Animal
                If AssemblyType.Ba seType Is GetType(Animal) Then
                'Load into combo Box here
                End If
                Next
                >
                If you have multiple level inheritance, i.e. Animal -Mammal -Primates
                -Monkey, you'll need to write a recursive search to check the entire
                inheritance hierachy.
                --
                spamhoney...@ro gers.com (Do not e-mail)

                Comment

                Working...