Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Nested Classes

Posted on 2009-03-31
10
Medium Priority
?
780 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 86

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 86

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 86

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
 

Author Comment

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

That is not nesting ?
0
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

721 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