Jay1b Posted October 29, 2004 Posted October 29, 2004 Hello Could someone pls point me in the right direction with this: If i create a class within one form, how can i use the same instance of that class from within another form? Thanks. Quote
stustarz Posted October 29, 2004 Posted October 29, 2004 Firstly ensure the class is a public class, then simply create an object set to the instance of that class: Dim objNewClass As PROJECTNAME.CLASSNAME You can now access the functions etc from the class using the new object Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Jay1b Posted October 29, 2004 Author Posted October 29, 2004 Thanks. Call me stupid (i do enough), but here is an example of what i want to do. The Class: Public Class ClsPrintArgs Inherits EventArgs Public serial As Integer Public printer As String Public Sub New(ByVal serial As Integer, ByVal printer As String) Me.serial = serial Me.printer = printer End Sub End Class How the class is called. Public printarg As New ClsPrintArgs(0, "") Using your example Dim clstest As Label_Printer.ClsPrintArgs MsgBox(clstext.serial) Unfortunately it doesnt like the msgbox's contents. Could you tell me what i am doing wrong pls? Thanks again. Quote
stustarz Posted October 29, 2004 Posted October 29, 2004 Could this be a spelling error? : Dim clstest As Label_Printer.ClsPrintArgs 'Should this read: MsgBox(clstest.serial) ?? MsgBox(clstext.serial) Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
stustarz Posted October 29, 2004 Posted October 29, 2004 Oh! and make the public variables in your class Public Shared Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Jay1b Posted October 29, 2004 Author Posted October 29, 2004 Thanks.........How small do i feel now? :) Errr......still getting an error though: 'Object reference not set to an instance of an object' Surely as i can have several references to the same object, this wouldnt be able to determine between them. Quote
stustarz Posted October 29, 2004 Posted October 29, 2004 Sorry you may not have seen my latest update: About making the variables declared in your class Public Shared, i have just run the code and everything is fine :) Stu Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Jay1b Posted October 29, 2004 Author Posted October 29, 2004 Nope, i didnt see it :) Thank you very much for your help. I assume shared means that all the instances of this class access the same variables? What if i had two instances of a class, with different variables? How could i do that? Thanks again. Quote
Joe Mamma Posted October 29, 2004 Posted October 29, 2004 yeah, you wouldnt want them shared Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Jay1b Posted October 29, 2004 Author Posted October 29, 2004 In which case, how would you do it? Quote
Administrators PlausiblyDamp Posted October 29, 2004 Administrators Posted October 29, 2004 You would just need to declarte an instance of the class before using it Dim clstest As new Label_Printer.ClsPrintArgs MessageBox.Show(clstest.serial.ToString()) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
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.