Burning Posted January 13, 2004 Posted January 13, 2004 I'm trying to find a way to pass Variables from Form to Form.... So I do this: Dim frmPetZoom As New frmPetZoom("CUSTOMER", strCustCode) frmPetZoom.ShowDialog(Me) '-- This is Modal '-- Get Chosen Value Back txtCustNo.Text = strCustCode frmPetZoom looks like this: Public Sub New(ByVal strCategory As String, ByRef strReturnVar As String) MyBase.New() . . . Now how do I Change the Reference from strReturnVar to something Global? Or is my Approach waayyyy off? Any input would be Appreciated :-\ Thanx Burning Quote
samsmithnz Posted January 13, 2004 Posted January 13, 2004 add a module and add the line: Public strReturnVar as string Now you can access the variable from anywhere in the project. You should keep Global variables to a minimum, they are considered bad programming. Quote Thanks Sam http://www.samsmith.co.nz
Burning Posted January 14, 2004 Author Posted January 14, 2004 Thanx, but i don't want any Globals... I just want to pass the reference (byref)... Aren't there Pointers in VB? I specifically only want to change the Variable I pass when instantiating the Class... By doing this my Class won't need to know, what variable, to send back data to. Also, Anybody will be able to use it, without Affecting their style of coding (By adding Globals) I hope that makes sense :) L8er Burning Quote
samsmithnz Posted January 14, 2004 Posted January 14, 2004 Ok I see. I often have a public function inside the form. Similiar to this Public Function ShowForm(byval Argument as String) as String 'do things me.showdialog() End Function IS that what you mean? Quote Thanks Sam http://www.samsmith.co.nz
Grimfort Posted January 14, 2004 Posted January 14, 2004 Or you could overload the exising function: 'Calling code Dim thing As New Form2 Dim strData As String thing.ShowDialog(Me, strData) MsgBox(strData) 'Code to put into the dialog form Public Overloads Sub ShowDialog(ByVal Owner As System.Windows.Forms.IWin32Window, ByRef strData As String) MyBase.ShowDialog(Owner) strData = "show me the money" End Sub Quote
Burning Posted January 15, 2004 Author Posted January 15, 2004 I need to actually Pass a Reference from the Calling form to another form... And in My Other form Work with it, without knowing the name of the Variable on the Owner form. 'Calling code Dim thing As New Form2 '-- Has Variable strData as string on thing.ShowDialog(Me, strData) MsgBox(strData) I need to change the Value of strData in 'thing' without knowing the Variable name strData, so When i use the 'thing' i just pass the reference, and it gets changed... There has to be a way... Come on u Gurus... This should be easy for ya... Help a man out here... Quote
Leaders Iceplug Posted January 15, 2004 Leaders Posted January 15, 2004 Seems as though if you pass any string into the function as ByRef, then you can modify the string passed to 'thing'. You'll only have to modify the strData in thing. If you changed the first character to an A, then you should see the changes in the variable that you passed. And since the procedure is public you can call it from anywhere. Am I on the right track of what you want? :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
splice Posted January 15, 2004 Posted January 15, 2004 Ok, no you can't do it that way I assume in your constructor you are assigning the passed string variable to a private member in the class. The problem is this: Dim x as String = "hi" Dim y As String = x x and y do not point to the same text string! y is a complete copy of x and is not associated with x. You need to create a Property that you can read the final value from after the ShowDialog is complete. Public Class X Inherits Form Protected _m As String Public Sub New(ByVal s As String) MyBase.New() _m = s End Sub Public ReadOnly Property ResultValue() As String Get Return _m End Get End Property End Class so, now you can do something like this: Dim s As String = "First Value" With New X(s) .ShowDialog(Me) Me.txtResult.Text() = .ResultValue() End With Quote -=splice=- It's not my fault I'm a Genius!
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.