Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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:

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!

Edited by Robby
  • Moderators
Posted

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

Visit...Bassic Software
Posted

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.

Posted (edited)

You create instances of the objects you use by:

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:

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

Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

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:

 

Public objSomeMethods As SomeMethods = New SomeMethods(Me)

 

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

 

SomeMethods.objForm1 = Me

 

 

Hopefully we will get there somehow!

Posted

Try moving the New command to the New for Form1:

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...