Inheritance??

masj78

Freshman
Joined
Aug 8, 2003
Messages
31
Location
Harrogate, UK
I am new to VB.NET and unsure of the way this works. I have a form with a variety of controls and to keep things nice and neat I have written some sub routine methods in another class vb file for the form to perform. The class with the sub routine methods in has references to controls on the form. I assumed if you create a link as I have done below and the methods are public then I could reference the methods from the form class. However when I do it this way I get a null reference error and if I add new to the linking instances it gives me a stack overflow error. My code is as follows:
Visual Basic:
Public Class Form1

Public objSomeMethods As SomeMethods

objSomeMethods.method1()
objSomeMethods.method2()


End Class


Public Class SomeMethods

Public objForm1 As Form1


public Sub method1()

objForm1.buttonaction


End Sub

Public Sub method2()

objForm1.buttonaction

End sub


End Class

Can someone explain the proper way of going about this as I am a bit confused.
Many Thanks!
 
Last edited by a moderator:
When you create an instence of the class you will need to pass the instance of the form either in the constructor or through a property/method.

Right now the objForm1 knows nothing about the current form1
 
Do you know the best way to do this? I know how to do it in Java

Controller cObj = new Controller(this);
Model mObj = new Model(this, cObj);

Above in Java with 2 class instances Controller and Model; to link the model to controller you pass in an instance parameter of the controller into the model object.
 
You create instances of the objects you use by:
Visual Basic:
Public Class Form1

'Give an Instance of this class to the SomeMethods
Public objSomeMethods As SomeMethods = New SomeMethods(Me)

objSomeMethods.method1()
objSomeMethods.method2()

End Class


Public Class SomeMethods
Public objForm1 As Form1

'Get a Reference to Form1 on creation
Public Sub New(ByRef AForm1 As Form1)
If AForm1 Is Nothing Then _
      Throw New ArgumentNullException("You must pass a valid Form1 Object")
objForm1 = AForm1
End Sub

Public Sub method1()

objForm1.buttonaction

End Sub

Public Sub method2()

objForm1.buttonaction

End Sub
End Class
The second method you could use is:
Visual Basic:
Public Class Form1

'Give an Instance of this class to the SomeMethods
Public objSomeMethods As SomeMethods = New SomeMethods()
SomeMethods.objForm1 = Me

objSomeMethods.method1()
objSomeMethods.method2()

End Class


Public Class SomeMethods
Public objForm1 As Form1

Public Sub method1()

objForm1.buttonaction

End Sub

Public Sub method2()

objForm1.buttonaction

End Sub
End Class
The first method is probably better because you can make sure the programmer passes a Form1 to prevent errors
 
Last edited:
Cheers for the help, but still having problems. Both methods look logical. Passing reference in a method or constructor, but still gives a null system reference error. Damn! This is frustrating.

In the first method, the line its complaining about is:

Visual Basic:
Public objSomeMethods As SomeMethods = New SomeMethods(Me)

In the second method, the line below is giving a syntax error as needing a declaration:

Visual Basic:
SomeMethods.objForm1 = Me


Hopefully we will get there somehow!
 
Try moving the New command to the New for Form1:
Visual Basic:
Public objSomeMethods As SomeMethods
Public Sub new()
'....
objSomeMethods = New SomeMethods(Me)
End Sub
The error shouldn't be occuring, make sure the class is actually loaded before the line is called
 
Back
Top