Hi!
How can i declare a variable in a form in a way that I can use its value in another form?
Printable View
Hi!
How can i declare a variable in a form in a way that I can use its value in another form?
The best course of action really all depends on what you need
You could declare a global variable in a module. This usually should be avoided.
If you declare a variable public in a form and the other form knows about you can access. Here's an example
VB Code:
Public Class Form2 'this is a form Public MyVariable as String End Class Public Class Form1 'this is another form Private Sub ShowForm1() Dim f as New Form2 f.ShowDialog() Messagebox.Show(f.MyVariable) End Sub End Class
There are a lot of other ways to do it as well.
You may want to take a look at this article about using multiple forms.
Hi!!
To do that do i really must enter f.ShowDialog() because that is not very convinient.
Ins't there anything like:
Global MyVariable as String
You don't have to use showdialog. I just used it as an example. Take a look at this example
VB Code:
Public Class Form2 'this is a form Public MyVariable as String End Class Public Class Form1 'this is another form Dim f as New Form2 Private Sub ShowForm1() f.Show() End Sub Private Sub GetOtherFormValue() Messagebox.Show(f.MyVariable) End Sub End Class
But to answer your question, you can have global variables. But you should only use those if you have to. Here's an example
VB Code:
Module Module1 Publis MyVariable As String End Module Public Class Form1 Public Sub SetVariable MyVariable = "Hello World" End SUb End Class Public Class Form2 Public Sub DisplayVariable Messagebox.Show(MyVariable) End Sub End Class
Sort of. I would doand then just do yourVB Code:
Friend MyVariable As StringVB Code:
form#/formname.myvariable.property
http://msdn2.microsoft.com/en-us/lib...y2(d=ide).aspx
Thks guys i'll test them later.
I strongly suggest that you read the Forms in VB.NET tutorial in my signature. It addresses this and other form-related questions. Also, you should keep in mind that forms are objects like any other, and thus follow the same rules as any other objects. If you want to access a member of an object then you must have a reference to that object.
Another option would be to expose the variable through a property:
VB Code:
Public Class MyForm private myVar as string public ReadOnly Property GetVar() as string Get return myVar end Get End Property End Class
Often considered to be the more 'proper' means of exposing a member variable of ANY class.
Ok i've been looking and reading about forms in VB.Net tutorials from jmcilhinney signature and i conclude that they just don't give me the
information i need (considering that I would prefer not to use a Module) so i decided to explain my problem much better.
I've two forms:
1 - MuShop (the principal)
2 - Customize
In MuShop when u click in Customize button he shows the Customize form with the command Customize.Show().
In Customize I've a checkbox.
What i need it to do?
When i open the customize control and put the value of the checkbox to true he puts the value to a "global" variable then i close Customize form.
I go to MuShop and i click in a Search Button if the "global" variable is true then he searchs in a way if its false he searchs in a different way.
What i've tried?
1)Declared in Customize Class
Public FindWholeWords As Boolean
And when hes leaving the form:
FindWholeWords = SearchWords.Checked (SearchWords is a Checkbox)
Then in Search button (in MuShop form) something like:
if Customize.FindWholeWords = true then
...
elseif Customize.FindWholeWords = False then
...
end if
The problem:
This only work if the customize form stays open, something that can't happen
2) The same as above but instead of Public FindWholeWords As Boolean
i put Friend FindWholeWords As Boolean
The problem:
The same as Above
What did i miss or not tried??
How about something like
VB Code:
If SearchWords.Checked = True Then Customize.FindWholeWords = true Else If End If
I don't know if thats what you are looking for? (BTW I am doing this off the top of my head so I don't remember about the checked property = boolean). :)
And i put that where in MuShop form?
Ok I am a little confused with your project...Could you please upload the source code files and the designer files (all except .exe files). I will then give you the correct code.
Here is it:
PS: i'm noob so do be amazed if the code is a little bit confusing or messy lool
I would avoid dealing with it using what you are doing, but consider it this way:
You are using the customize form to get information from the user. That information needs to be available both to ANY instance of the customize form (in case the user goes back into it), as well as at least once instance of the MuShop form.
If you keep this information in a single instance of the customize form, then that instance MUST remain in existence as long as that information is needed. However, as you recognize, that's not what you want.
There are two ways to handle this.
1) Put that information in a global variable (declared Public in a module). This will make it available to all form instances, all modules, etc. However, it is generally considered to be undesirable to use global variables, though I suspect most people do to some extent.
2) Create a class that holds the data, and exposes it through a property. Create an instance of that class to receive the data from the Customize form, and retain the data until it is needed by whichever form needs it. That leaves you with the question of where should you create that instance of the class. Since the instance of the class must always be in scope for the code that uses it, one obvious place to put it is declared Public in a module....in other words, make a class to wrap a global variable. Depending on the design of your code, you may find some more restrictive scope for the class instance, but if you could, then you could use that location for suggestion #1, as well.
That should do it but I'm not positive since I never received the database. Good Luck! ;)
VB Code:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click If SearchWords.Checked Then Teste = True Else Teste = False End If Timer2.Start() End Sub
Thks a lot guys.
PS:I'm taking out the source code.
By the way, what is this project anyway?