Solved

Nested Classes

Posted on 2009-03-31
10
769 Views
Last Modified: 2013-11-25
Trying to nest some classes in my app.  If I try to nest class2 inside class1, shouldnt the following work:

'************
'Class 1 General Section
Private MyClass2 as new Class2

Public Property Let InnerClass(ByVal clsNewValue as Class2)
     MyClass2 = clsNewValue
End Property
Public Property Get IinnerClass() as Class2
     InnerClass = Class2
End Property

Public Sub Command1_Click
     Class1.InnerClass.FromString = NewVal    'but this does not evoke the FromSting property
End Sub
'
'*******************
'This does work ...
Public InnerClass as new Class2
Public Sub Command1_Click
     Class1.InnerClass.FromString = NewVal    'now this does evoke the FromSting property
End Sub
0
Comment
Question by:sidwelle
  • 6
  • 4
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24031110
Worked for me...

I changed:

    Public Property Get IinnerClass() as Class2   ' <----- there is an extra "i" in there!
         InnerClass = Class2
    End Property

To this:

    Public Property Get InnerClass() as Class2
         InnerClass = Class2
    End Property

Then used this code:

    Private Sub Command1_Click()
        Dim c1 As New Class1
        c1.InnerClass.FromString = "Hello"
    End Sub

0
 

Author Comment

by:sidwelle
ID: 24032830
Did you actually create a property "FromString" and stop your code to see if it got there ?
FYI, this is VB6

I get:  
>Run-time error '91':
>Object variable or With block variable not set.

See my code below.


'Form ...

Private Sub Command1_Click()

    Dim c1 As New Class1

    c1.InnerClass.FromString = "Hello"

End Sub
 

'Class1 ...

Private MyClass2 As New Class2

  

Public Property Let InnerClass(ByVal clsNewValue As Class2)

    MyClass2 = clsNewValue

End Property

Public Property Get InnerClass() As Class2

    InnerClass = MyClass2

End Property
 

'Class2 ...

Private MyString As String
 

Public Property Get FromString() As String

    FromString = MyString

End Property
 

Public Property Let FromString(ByVal sNewValue As String)

    MyString = sNewValue

    MsgBox "MyString: '" & MyString & "'" 

End Property

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24032918
Yeah...I created a FromString prop in my Class2.

I tested it in Excel.  If it works there it should definitely work in VB6.  I would have to fire up my very old desktop to test it with VB6.  Let me know if you still can't figure it out. =\

0
 

Author Comment

by:sidwelle
ID: 24033067
No, it does not work  for me in VB6.  Is this what the book means when it states that VB6 does not support "Inheritance"
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24033226
Well...there actually is NOT any Inheritance being used in your example!

What your code is doing is demonstration COMPOSITION.  Class1 simply contains an Instance of Class2.  One class is not inheriting anything from the other.

Also, this is not what a nested class is either.  A nested class (also called an inner class) is one where the DEFINITION of a class exists INSIDE that of another.  This is not possible in VB6.

The book is correct that VB6 does not support TRUE inheritance.  It can support a rudimentary inheritance via the use of INTERFACES though.

If you want TRUE OOP then move on up to VB.Net as it is fully compliant and on par with other languages such as C++/C#/Java etc...

I'll test it on my VB6 machine later today sometime.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sidwelle
ID: 24038101
>'Class1 ...
>Private MyClass2 As New Class2

That is not nesting ?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24039743
Definitions vary...but in general, a nested class is one that is DEFINED within another (often called an Inner class).

Your example is only DECLARING an instance of Class2 within Class1 (not defining what Class2 "is").  So Class1 is said to be composed of all of its members...including the instance of Class2.

*** A Nested/Inner Class is NOT possible in VB6 ***

It is possible in VB.Net though.  Here is an example of what that might look like:

    Public Class Class1
 
        ...

        Public Class Class2

            ...

        End Class

    End Class

The definition of Class2 is completely contained within Class1 making it an inner/nested class.

Look at it this way.  What if we had several classes such as Customer, Order and Item for an online store.

The Customer class would obviously have data related to a person such as name, address, telephone number, etc.
The Item class would have data about something that can be bought such as name, description, price, dimensions, etc.

Now the Order class could contain an Order number, an instance of a Customer, and a List containing Items ordered:

    ' VB.Net (for explanation purposes)
    Public Class Order

        Public OrderNumber As String
        Public Cust As Customer
        Public Items As New List(Of Item)

    End Class

The Order class is COMPOSED of those items within it...but the items within it are not being defined there...thus they are not "nested".
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24040414
So VB6 is a little pickier on the syntax than Excel.

The stickler was the use of "Set" keyword to return the Class2 instance in the InnerClass property:

    Public Property Get InnerClass() As Class2
          Set InnerClass = MyClass2
    End Property

Here is the full code I tested with:

' ----------

' Form1

' ----------

Private Sub Command1_Click()

    Dim c1 As New Class1

    c1.InnerClass.FromString = "Hello World!"

    MsgBox c1.InnerClass.FromString, vbInformation, "c1.InnerClass.FromString"

End Sub
 

' ----------

' Class1

' ----------

Private MyClass2 As New Class2
 

Public Property Set InnerClass(ByVal clsNewValue As Class2)

     Set MyClass2 = clsNewValue

End Property
 

Public Property Get InnerClass() As Class2

      Set InnerClass = MyClass2

End Property
 

' ----------

' Class2

' ----------

Private MyString As String

 

Public Property Get FromString() As String

    FromString = MyString

End Property

 

Public Property Let FromString(ByVal sNewValue As String)

    MyString = sNewValue

End Property

Open in new window

0
 

Author Closing Comment

by:sidwelle
ID: 31565482
Thanks for the help !

Do you see any advantage or disadvantage to using:
Public InnerClass as Class2
Opposed to Get and Set ?

In our VB.Net class we were told that MS retired the "Set" command.
Maybe thats why you code worked in Excel ?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24043129
Your first question:

    Do you see any advantage or disadvantage to using:
        Public InnerClass as Class2
        Opposed to Get and Set ?

This goes to the HEART of good OOP practices and I hope that by the end of your class you'll see why.  Making "MyClass2" private and allowing access to it via the Get/Set properties only is known as ENCAPSULATION.  The advantage here is that you can CONTROL what happens when the value is accessed or retrieved.  You can increment a counter or raise an event every time it is accessed.  In the "setter", you could enforce validation, making it impossible to set a member to something that is considered an "invalid state" for your purposes.  For instance, you could keep a certain member within a specific range, or not allow changes when a flag is toggled.  This is hard to see for many people because often times the example is exactly what you had: just a blank get/set that doesn't really do anything.  It is easy to see why people would say, "Why bother?"  Realize that you can put CODE into the getter/setters and call other methods/functions to make decisions...thus allowing you to determine what action to take.  =)

Your second question:

    In our VB.Net class we were told that MS retired the "Set" command.
    Maybe thats why you code worked in Excel ?

My code worked in Excel because it is a "looser" (less type safe) variant of VB6.  This is just one of the many differences between the MSOffice versions of VB and VB6.

The deprecation of "set" in VB.Net has more to do with making the language more consistent with other fully OOP compliant languages such as Java/C++/C#.  In those languages you simply make the assignment:

    someObject = someOtherObject ' <-- most modern languages don't have a "set" keyword!

In classic VB6, the "set" keyword is to specifically assign one object to another:
http://msdn.microsoft.com/en-us/library/aa266283(VS.60).aspx

    "Assigns an object reference to a variable or property."

I have no idea why the original VB6 developers chose to differentiate object assignments like that...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now