masj78 Posted September 12, 2003 Posted September 12, 2003 (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 September 12, 2003 by Robby Quote
Moderators Robby Posted September 12, 2003 Moderators Posted September 12, 2003 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 Quote Visit...Bassic Software
masj78 Posted September 12, 2003 Author Posted September 12, 2003 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. Quote
AndreRyan Posted September 14, 2003 Posted September 14, 2003 (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 September 14, 2003 by AndreRyan Quote .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?
masj78 Posted September 17, 2003 Author Posted September 17, 2003 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! Quote
AndreRyan Posted September 19, 2003 Posted September 19, 2003 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 Quote .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?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.