6 Replies - 192 Views - Last Post: 30 November 2018 - 11:39 AM Rate Topic: -----

#1 NAK91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 18

Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 08:41 AM

I'm wondering how I would go about using an abstract base class to contain a list of its inherited classes. If I use an animal base class analogy, I would like to create a class called AnimalTypes which acts as a container of classes of AnimalTypes.

If I want to instantiate an object of type Lion, I would like to be able to type:

Dim Simba As New AnimalTypes.Lion


How do I obtain this hierarchical structure? If I have a hundred classes that inherit from the AnimalTypes class, but I don't necessarily remember what I've named a particular class, I would like to be able to utilize intellisense to list those for me. I would appreciate a quick example.

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract Base Class/Inherited Class Hierarchy

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 08:50 AM

Check inheriting class, and interfaces would be where I'd start if I was you. Post what you have so far
Was This Post Helpful? 0
  • +
  • -

#3 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 08:58 AM

Sounds like you want a Namespace.

Link Explaining Namespace, it is in c#, which is why I put an example of how to use namespaces in VB under it.

https://docs.microso...ide/namespaces/

Namespace AnimalTypes

Public Class Lion


End Class

Public Class Tiger


End Class

End Namespace

New AnimalTypes.Lion
New AnimalTypes.Tiger

Was This Post Helpful? 1
  • +
  • -

#4 NAK91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-March 18

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 09:03 AM

View Postmaceysoftware, on 30 November 2018 - 08:58 AM, said:

Sounds like you want a Namespace.


Thank you. I think this is what I'm looking for. I had been reading into abstract base classes, inheritance, and interfaces, but hadn't yet looked into namespaces.
Was This Post Helpful? 0
  • +
  • -

#5 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 09:10 AM

Your welcome, For reference you don't have to have all your class in one Namespace block, you can spread them over multiple class files (just remember to wrap all the classes in a namespace block with the same Type) and it will merge them together in the intellisense.

Class 1
Namespace AnimalTypes

Public Class Lion


End Class

End Namespace

Class 2
Namespace AnimalTypes
Public Class Tiger


End Class
End Namespace


Always remember to click the little green '+' if someone has been helpful.

This post has been edited by maceysoftware: 30 November 2018 - 09:11 AM

Was This Post Helpful? 1
  • +
  • -

#6 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 10:25 AM

You don't have to have a separate namespace, if you don't want to, but I personally sometimes do so.

You could have them all in the one namespace, and have one class inherit the other class like this. So you could derive from another... this is just a POC

Namespace AnimalKingdom
    Public Class Animals
        Public Enum Dogs
            Doberman
            Rottweiler
        End Enum
    End Class
    Public Class AnimalType
        Inherits Animals
        Public Shared Function SelectedAnimal(ByVal Choice As String) As Boolean
            If Choice = Dogs.Rottweiler.ToString Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class
    Public Interface AnimalInterface

    End Interface
End Namespace


Call it like so ::

        If AnimalKingdom.AnimalType.SelectedAnimal("Rottweiler") = True Then
            Console.WriteLine("Rottweiler is part of the dog specie: " & True)
        End If



Outputs fine ::
Rottweiler is part of the dog specie: True


Hope it helps
Was This Post Helpful? 1
  • +
  • -

#7 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 671
  • Joined: 01-October 08

Re: Abstract Base Class/Inherited Class Hierarchy

Posted 30 November 2018 - 11:39 AM

Not really clear what you want to accomplish, but here is an enum and some classes

Public Enum AnimalType
    unknown
    Lion
    Tiger
    Bear
    Fish
    Cow
End Enum

Public MustInherit Class AnimalBase
    Public ReadOnly TypeOfAnimal As AnimalType
    Public Property NumOfLegs As Integer
    Public Property HasWings As Boolean

    Public Sub New(KindOfAnimal As AnimalType, Legs As Integer, Winged As Boolean)
        Me.TypeOfAnimal = KindOfAnimal
        Me.NumOfLegs = Legs
        Me.HasWings = Winged
    End Sub

    Public Shared Function InheritedBy() As List(Of System.Type)
        'Manually add inherited classes
        Dim rv As New List(Of System.Type)
        rv.Add(GetType(Lion))
        rv.Add(GetType(Tiger))
        'check
        For idx As Integer = rv.Count - 1 To 0 Step -1
            If rv(idx).BaseType IsNot GetType(AnimalBase) Then
                rv.RemoveAt(idx)
            End If
        Next
        Return rv
    End Function

    Public Shared Function Create(WhichAnimal As AnimalType) As AnimalBase
        Select Case WhichAnimal
            Case AnimalType.Lion
                Return New Lion
            Case AnimalType.Tiger
                Return New Tiger

            Case Else
                Return Nothing
        End Select
    End Function

    Public Shared Function Which(SomeBaseClass As AnimalBase) As AnimalBase
        Select Case SomeBaseClass.TypeOfAnimal
            Case AnimalType.Lion
                Return DirectCast(SomeBaseClass, Lion)
            Case AnimalType.Tiger
                Return DirectCast(SomeBaseClass, Tiger)

            Case Else
                Return Nothing
        End Select
    End Function
End Class

Public Class Lion : Inherits AnimalBase
    Public Sub New()
        MyBase.New(AnimalType.Lion, 4, False)
    End Sub

    Public LionsRoar As Boolean = True
End Class

Public Class Tiger : Inherits AnimalBase
    Public Sub New()
        MyBase.New(AnimalType.Tiger, 4, False)
    End Sub

    Public TigersHaveClaws As Boolean = True
End Class


The base class has a function that returns a list of types, but this is dependent on you having added all of the inherited classes manually.

There is some other code that might help.

Here is some code to test with
        Dim foo As Lion = DirectCast(AnimalBase.Create(AnimalType.Lion), Lion)
        Dim typ As System.Type = foo.GetType
        Dim bar As New Tiger

        Dim someanimal As AnimalBase = AnimalBase.Which(foo)

        someanimal = AnimalBase.Which(bar)

        Dim typsOFBase As List(Of System.Type) = AnimalBase.InheritedBy



The real answer might be to do some documentation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1