Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class w/Class

Status
Not open for further replies.

Skie

Programmer
Jun 21, 2004
475
US
There's probably something simple that I'm missing, but I was to make a class with a class(es). I'd like to be able to use my class so that I can dot (.) into subsequent classes. But, I want the subsequent classes to still be able to use the functions, subs, and variables from the original class. I also want to be able to call functions and subs in subsequent classes.

This is dummied up, but it's a simplified example of what I'm trying to do. Actual code is a few hundred lines.
Code:
Sub DoStuff()
    Dim Blah As New Class1
    Blah.SetVar
    Blah.Class2.ParseVar
    Blah.Class2.NewVar
End Sub

Public Class Class1
    Public Function SetVar() As String
        SetVar = "abcdef"
    End Function

    Public Sub SetThisValue()
        Dim ThisValue As String
        ThisValue = Class2.NewVar
    End Sub

    Public Class Class2
        Public Sub ParseVar()
            Dim aArray
            aArray = Split(SetVar)
        End Sub

        Public Function NewVar() As String
            NewVar = "123456"
        End Function
    End Class
End Class

With this format, I get an error "Reference to a non-shared member requires an object reference." I tried sharing the functions, which gets rid of the error, but still doesn't seem to work. I also had my the top level class dim an the secondary class as new, and the secondary class dim the top level class. All I really want to do is to organize my class so that I don't have class1.50+ItemsToChooseFrom. Instead I have a class with say 15 items.. and some of those items have sub-items.

Thanks for any help.
 
That is because your instance of Class1 (Blah) does not contain an instance of Class2 - or rather that the routines in Class2 are not declared as Shared.

You want to either declare the methods as Shared or upon creation of the instance of Class1 create and instance of Class2 within the class1 instance that you can access publicly.

Hope that helps.
 
Maybe I am a bit out of touch with newer practices (I must admit I am mostly self taught through books and web searches but am comfortable in VB.Net 2005 and 2008 now), but I would not create a setup like this. If I am wrong, someone please let me know so I can learn. [smile]

I would create two separate classes (in separate .vb files). I would then have the one class create an instance of the second class...and use the second class ONLY through the instantiated instance located in the first class. I know a bit confusing trying to describe it...here's a code example that I really did use at one time for a game (scaled down to esential parts as it is MUCH longer).

Code:
'First Class (Base Class That the Next Class Will Call)
Public Class Mainworld

#Region "Private Variables"

    Private _Name As String = String.Empty
    [COLOR=red]Private _Size As String = String.Empty[/color]

#End Region

#Region "Public Properties"

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    [COLOR=red]Public Property Size() As String
        Get
            Return _Size
        End Get
        Set(ByVal value As String)
            _Size = value
        End Set
    End Property[/color]

#End Region

End Class

'-----------------------------------------

'Second Class (This Class Instantiates The Base Class)

Public Class Character

#Region "Private Variables"

    Private _Name As String = String.Empty
    [COLOR=red]Private _Homeworld As Mainworld = Nothing[/color]

#End Region

#Region "Public Properties"

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    [COLOR=red]Public Property Homeworld() As Mainworld
        Get
            Return _Homeworld
        End Get
        Set(ByVal value As Mainworld)
            _Homeworld = value
        End Set
    End Property[/color]

#End Region

End Class

Then throughout my code, I would use something like this.

Code:
Dim chtr as New Character
' Some Code To Fill Character With Lots Of Info

'To view a property of the Homeworld - which is an instance of the base Mainworld class
Me.txtHomeworldSize.Text = chtr.Homeworld.Size

This method allows for full intellisense when you are referencing the base class instantiated in the other classes.

If I am way out of the ballpark here, someone please let me know. I hope this helps and if not please let me know, so I can try to describe it better....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
That is certainly one way to do it.

Placing the class within another class is done specifically to hide (or prevent if it is a private class) another developer from directly using it. At least that's the way I figure it.
 
Brovik,

Hadn't thought of that one....As my personal experience is working by myself or with at most two other developers, I never had to even think of that scenario. Thanks.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
I had issues with the sharing. It resolved my errors, but nothing would work. After some thought, I decided I really just need a private "common" class that I can load to both classes. This is a simplified version of what I created:

Code:
Option Explicit On
Public Class Earth
#Region "Private Variables"
    Private cCommon As New EnM
#End Region

#Region "Public Variables"
    Public cMoon As New Moon
#End Region

#Region "Public Properties"
    Public ReadOnly Property HasLife() As Boolean
        Get
            Return cCommon.HasLife
        End Get
    End Property
#End Region

#Region "Private Methods"
    Private Sub AddLife()
        cCommon.HasLife = True
    End Sub
#End Region

#Region "Public Methods"
    Public Sub New()
        cCommon.HasLife = False
        cCommon.BuildEarth()
        Addlife()
        cMoon.SendCheese(1)
    End Sub
#End Region

    Public Class Moon
#Region "Private Variables"
        Private cCommon As New EnM
#End Region

#Region "Public Methods"
        Public Sub SendCheese(ByVal value As Integer)
            cCommon.Cheese = value
        End Sub
#End Region
    End Class

    Private Class EnM
#Region "Private Variables"
        Private iCheese As Integer = 0
        Private isLife As Boolean = False
#End Region

#Region "Public Properties"
        Public Property Cheese() As Integer
            Get
                Return iCheese
            End Get
            Set(ByVal value As Integer)
                iCheese = value
            End Set
        End Property

        Public Property HasLife() As Boolean
            Get
                Return islife
            End Get
            Set(ByVal value As Boolean)
                isLife = value
            End Set
        End Property
#End Region

#Region "Public Methods"
        Public Sub BuildEarth()
            '*Do some stuff here
        End Sub
#End Region
    End Class
End Class

It seems to work. I think one potential problem I have is that when I set a value in EnM, the value isn't universal. Or rather, it's possible to have a different value in cCommon.HasLife in Earth than in cCommon.HasLife Earth.Moon.
 
I fixed my second issue by sharing cCommon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top