1

I'm relatively new to OOP. Let's say I have three different objects/classes defined, like:

Public Class Part
    Private m_Name As String = ""
    Public Property Name() As String
        Get
            Return m_Name
        End Get
    End Property

    something...
End Class

Public Class Assembly 
    Private m_Name As String = ""
    Public Property Name() As String
        Get
            Return m_Name
        End Get
    End Property

    something...
End Class

Public Class Drawing
    Private m_Name As String = ""
    Public Property Name() As String
        Get
            Return m_Name
        End Get
    End Property

    something...
End Class

Now I have a list of all instances of those objects in another class, like:

Public Class Main
    Private m_ListOfObjects As List(Of Objects)
    Private Sub run()
       something...
    End Sub
End Class

Imagine a code which adds each instance of those three classes (Part, Assembly and Drawing) to this m_ListOfObjects. Now I have a loop which loops trough the whole list, like:

Dim Obj As Object
For Each Obj In m_ListObObjects
   Debug.Print(Obj.Name)
End For

My questions is now: IntelliSense is not able to show me if the property Name exists for the Object Obj. Well that's clear because Visual Studio does not know that I will use the classes Part, Assembly and Drawing for. How can I solve that issue?

It feels like a hacky way when I use something like this:

CType(Obj, Part).Name 

Then Visual Studio knows that I refer to the Part Class, but what can I do if I loop through different objects in a list and refer to a property each class will have? Or maybe my approach is completely off the track of OOP? Do I miss the opportunity to implement an interface?

1 Answer 1

5

Your named objects either need

Inherits abstractClass

or

Implements interfaceclass.name

Make whichever one you pick and give it a Name method with no implementation.

Then main needs either

Private m_ListOfObjects As List(Of abstractClass)

or

Private m_ListOfObjects As List(Of interfaceclass)

and suddenly calling Name works. This is polymorphism. It's a big part of OOP.

The reason it didn't work before is because vb.net doesn't support duck typing. Therefor you can't just discover that each object has a Name property as you call it. At compile time you have to make some kind of promise that it will be there every time. You don't make that promise if it's only a list of type Object. Sure Name is there, waiting, this time, but vb.net want's you to promise it'll be there. That way it can complain as soon as it sees that promise broken. It doesn't have to wait for your call. It can complain as soon as you say you want to put some nameless thing in the list.

3
  • Thank you. If I have variables that each class will need. I would make a base class/abstract class which declares this variables, like name and which will be inherited by the three object classes? Specific variables which only occurs in some of those three objects would be implemented with an interface or what's the strategy here?
    – SturmGhost
    Mar 8, 2023 at 22:33
  • Funny you should mention the word strategy. Mar 8, 2023 at 22:44
  • @SturmGhost: When you want to reuse something (property, method) across multiple classes in a way that you can iterate over all of its usages, you need to declare it in a reusable component (abstract class, interface) and then have your classes implement the component. If you can then guarantee that your objects polymorphically adhere to this component, the compiler is satisfied that it will reliably be able to access the thing (method, property) that you wanted to reusably access.
    – Flater
    Mar 9, 2023 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.