Home All Groups Group Topic Archive Search About

how do you implement association between objects

Author
19 Nov 2007 3:53 PM
DougE
I am at my wits end.  VB .net is supposed to be an object oriented language. 
But I have tried hack after hack, and I can not get past either "Reference to
a non-shared member requires an object reference.", or "Name <object> is not
declared." or a stack overflow exception.  This is simple object oriented
association.  I need for objects generated by Form1, Class1, and Class2 to
talk to each other back and forth.  Here is how it starts:

Imports System

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Simulation As New Class1
    Public CarA As New Class2
    Public CarB As New Class2

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Simulation.update_rideList(True, 3)
    End Sub

End Class

'Then the other classes:

Public Class Class1

        Public Sub New()
            MyBase.New()
        End Sub

        Public Overridable Sub update_rideList(ByVal car As Boolean, ByVal
floor As Integer)

            If (car) Then
                CarA.rideList.Add(floor)
                CarA.test_change(True)
            Else
                CarB.rideList.Add(floor)
            End If
        End Sub

End Class

'and

Public Class Class2

        Public Sub New()
            MyBase.New()
        End Sub

        Public rideList As New ArrayList
        Public Sub test_change(ByVal change As Boolean)
            If (change) Then
                Label1.BackColor = Label1.BackColor.Green
            End If

        End Sub
End Class


Please Help??!!!!

Author
19 Nov 2007 4:43 PM
Armin Zingler
"DougE" <Do***@discussions.microsoft.com> schrieb
> I am at my wits end.  VB .net is supposed to be an object oriented
> language. But I have tried hack after hack, and I can not get past
> either "Reference to a non-shared member requires an object
> reference.", or "Name <object> is not declared." or a stack overflow
> exception.  This is simple object oriented association.  I need for
> objects generated by Form1, Class1, and Class2 to talk to each other
> back and forth.  Here is how it starts:


You should have mentioned where you get the error.


Show quoteHide quote
> Imports System
>
> Public Class Form1
>    Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>   Public Simulation As New Class1
>    Public CarA As New Class2
>    Public CarB As New Class2
>
>    Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>        Simulation.update_rideList(True, 3)
>    End Sub
>
> End Class
>
> 'Then the other classes:
>
> Public Class Class1
>
>        Public Sub New()
>            MyBase.New()
>        End Sub
>
>        Public Overridable Sub update_rideList(ByVal car As Boolean,
> ByVal floor As Integer)
>
>            If (car) Then
>                CarA.rideList.Add(floor)
>                CarA.test_change(True)
>            Else
>                CarB.rideList.Add(floor)
>            End If
>        End Sub
>
> End Class


CarA is a member of the Form. If you want to access CarA, you need a
reference to the Form that contains CarA. If you had 5 instances of the
Form, you would have 5 x CarA, so you must say ThisForm.CarA or
ThatForm.CarA. Or maybe no single Form instance exists. Which CarA would
Class1 refer to?

Solution: Pass the reference to the Form into sub update_rideList:

   Public Overridable Sub update_rideList( _
      ByVal car As Boolean, ByVal floor As Integer, _
      ByVal TheForm As Form1)

      If (car) Then
         TheForm.CarA.rideList.Add(floor)
         TheForm.CarA.test_change(True)
      Else
         TheForm.CarB.rideList.Add(floor)
      End If
   End Sub


Call:

    Simulation.update_rideList(True, 3, Me)


Show quoteHide quote
> 'and
>
> Public Class Class2
>
>        Public Sub New()
>            MyBase.New()
>        End Sub
>
>        Public rideList As New ArrayList
>        Public Sub test_change(ByVal change As Boolean)
>            If (change) Then
>                Label1.BackColor = Label1.BackColor.Green
>            End If
>
>        End Sub
> End Class

Here's the same with Label1.



Armin
Author
20 Nov 2007 3:40 AM
DougE
Thank you, Armin, passing the reference seems to have worked.  Thanks again.

Show quoteHide quote
> Armin
>
>
Author
19 Nov 2007 4:48 PM
rowe_newsgroups
> I am at my wits end.  VB .net is supposed to be an object oriented language.

It is an object orientated language, you're just confused on how to
program using it. The errors, with the exception of the stack
overflow, tell you exactly the problem. You are trying to use a
variable unknown to a Class. There are at least two ways of fixing
this:

>     Public CarA As New Class2
>     Public CarB As New Class2

If these are the two variable you want to be visible to Class1, you
must either pass a reference to them to the instance of Class1 or make
Class1 a subclass to Form1. Making Class1 a subclass would give it
access to these two variables, but also could break your class
structure. For that reason I'll assume you are going to use the first
method (passing the reference to the class).

>         Public Overridable Sub update_rideList(ByVal car As Boolean, ByVal
> floor As Integer)

This needs changed to the below if you want to pass the reference to
your class variables:

///////////
Public Overridable Sub update_ridelist(car As Boolean, floor As
Integer, carA as Class2, carB as Class2)
///////////

If you want to be able to access the CarA and CarB objects.

>     Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         Simulation.update_rideList(True, 3)
>     End Sub

This is where you need to pass the references to CarA and CarB. With
the above change you would call the method like this:

///////////
Simulation.update_ridelist(True, 3, CarA, CarB)
///////////

Then the CarA and CarB variables should be changed properly.

>         Public Sub test_change(ByVal change As Boolean)
>             If (change) Then
>                 Label1.BackColor = Label1.BackColor.Green
>             End If
>         End Sub

On a side note, I find the "change" variable pointless as you don't
have an else clause. Basically, if you want the change call the
method, otherwise don't. Adding the bool check just adds overhead.

Thanks,

Seth Rowe
Author
20 Nov 2007 3:39 AM
DougE
Thank you Seth, passing the reference to the Form1 seems to have fixed it.

Incidentally, this is just a test case.

Thanks again.

Show quoteHide quote
>
> On a side note, I find the "change" variable pointless as you don't
> have an else clause. Basically, if you want the change call the
> method, otherwise don't. Adding the bool check just adds overhead.
>
Author
19 Nov 2007 4:59 PM
Family Tree Mike
It's unlikely that Label1 (presumably from Form1) is exposed publicly to
Class2.  Even if it is, you need to let the instance of Class2 (either carA
or CarB) to have a reference to the creating form instance.


Show quoteHide quote
"DougE" wrote:

> I am at my wits end.  VB .net is supposed to be an object oriented language. 
> But I have tried hack after hack, and I can not get past either "Reference to
> a non-shared member requires an object reference.", or "Name <object> is not
> declared." or a stack overflow exception.  This is simple object oriented
> association.  I need for objects generated by Form1, Class1, and Class2 to
> talk to each other back and forth.  Here is how it starts:
>
> Imports System
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>    Public Simulation As New Class1
>     Public CarA As New Class2
>     Public CarB As New Class2
>
>     Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         Simulation.update_rideList(True, 3)
>     End Sub
>
> End Class
>
>  'Then the other classes:
>
> Public Class Class1
>
>         Public Sub New()
>             MyBase.New()
>         End Sub
>
>         Public Overridable Sub update_rideList(ByVal car As Boolean, ByVal
> floor As Integer)
>
>             If (car) Then
>                 CarA.rideList.Add(floor)
>                 CarA.test_change(True)
>             Else
>                 CarB.rideList.Add(floor)
>             End If
>         End Sub
>
> End Class
>
> 'and
>
> Public Class Class2
>
>         Public Sub New()
>             MyBase.New()
>         End Sub
>
>         Public rideList As New ArrayList
>         Public Sub test_change(ByVal change As Boolean)
>             If (change) Then
>                 Label1.BackColor = Label1.BackColor.Green
>             End If
>
>         End Sub
> End Class
>
>
> Please Help??!!!!
>