|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
how do you implement association between objectsBut 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??!!!! "DougE" <Do***@discussions.microsoft.com> schrieb You should have mentioned where you get the error.> 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: Show quoteHide quote > Imports System CarA is a member of the Form. If you want to access CarA, you need a > > 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 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 Here's the same with Label1.> > 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 Armin Thank you, Armin, passing the reference seems to have worked. Thanks again.
Show quoteHide quote > Armin > > > 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 toprogram 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 If these are the two variable you want to be visible to Class1, you> Public CarB As New Class2 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 This needs changed to the below if you want to pass the reference to> floor As Integer) 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 This is where you need to pass the references to CarA and CarB. With> System.EventArgs) Handles Button1.Click > Simulation.update_rideList(True, 3) > End Sub 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) On a side note, I find the "change" variable pointless as you don't> If (change) Then > Label1.BackColor = Label1.BackColor.Green > End If > End Sub 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 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. > 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??!!!! >
Type inference
Read very large file in bytearray and upload to MSSQL VB calling DLL only with _stdcall? Getting a subset of DataRows from a DataTable Check if libraries or program exist Web Server Read/Write Problem with ToolStripTextBox Insert smaller images into one Image object Edit And Continue doesn't work :-( Form - controls is placed different on Vista than in XP |
|||||||||||||||||||||||