views:

50

answers:

2

Hi,

I have a Class FileDoc

Public Class FileDoc
Inherits BaseClass

Public Sub DeleteDoc()

dim catId as integer = Cat_ID

End Sub

    a bunch of properties...
End Class

And I have another Class...

Public Class BaseClass

Private _Cat_ID As Integer
Public Property Cat_ID() As Integer
    Get
        Return _Cat_ID
    End Get
    Set(ByVal value As Integer)
        _Cat_ID = value
        AssignAllInfo()
    End Set
End Property

Private _Docs As List(Of FileDoc)
Public Property Docs() As List(Of FileDoc)
    Get
        Return _Docs
    End Get
    Set(ByVal value As List(Of FileDoc))
        _Docs = value
    End Set
End Property

My question is, since FileDoc comes from the BaseClass, how can I access values from the BaseClass when I'm coding in the FileDoc Class. Like my example in sub DeleteDoc(), I'm trying to access the Cat_ID of the Base Class which this FileDoc belongs to.

Adding an inheritance doesn't transfer the values to the class, only the properties.

Thx in advance

A: 

Inheritance doesn't do that. Keep in mind that you are working with Classes which is a kind of template for your object. You're mixing that up with instances which is an instantiated version of your class that contains data.

For example, when you create an instance of FileDoc, you are getting a blank object that contains fields from both BaseClass and FileDoc. FileDoc has no idea if there are other instances of BaseClass that contain data (nor should it). Think about it this way - if you had multiple BaseClass instances and then you instantiated a FileDoc class, which BaseClass instance should it use to populate data?

There are two ways to go about what you want to do. First, you can imagine your BaseClass to be abstract (not sure how to do this in VB). So you will not instantiate any instances of BaseClass. BaseClass solely exists to provide a base from which other classes can inherit. So, what you will do is instantiate FileDoc and then populate it with the data you need.

Your other option is have a BaseClass, but not inherit from it. Instead you can think of FileDoc as a wrapper that accepts BaseClass as a parameter. So this means that FileDoc has a private member that is of type BaseClass. This way you don't need to inherit, but you have access to data from BaseClass.

Choose the method that is appropriate for your situation.

Vivin Paliath
So, I'm supposed to send an instance of BaseClass to FileDoc?How do I do that?
Iggy
Haven't really done OOP in VB, but you could maintain a property that is of type BaseClass? `Private baseClassInstance As BaseClass`.
Vivin Paliath
I added a Cat_ID property in the FileDoc and I set it when I instantiate an instance of FileDoc... thx for the help
Iggy
If you're going to have the same common values for a lot of `FileDoc` instances, you should have a single instance of `BaseClass` and pass that in to your `FileDoc` instances.
Vivin Paliath
A: 

I have a list(of FileDoc)... I have many FileDocs which have the same properties as the BaseClass... I don't wanna instantiate many FileDocs and in each FileDoc save the same values for the BaseClass... A FileDoc shouldn't exist without the BaseClass. I see what you mean and I guess I'm not doing things right...

Iggy