|
-
Jun 18th, 2003, 10:00 AM
#1
Thread Starter
Junior Member
reading a variable from a different form
I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
how can i do this?
thanks to all in advance.
-
Jun 18th, 2003, 10:07 AM
#2
Fanatic Member
You have to declare a public variable in the first form and then, you just access it like so:
VB Code:
'This is form one (Form1)
Public txtDragValue As String
txtDragValue = "Jeremy is the man!"
VB Code:
'This is form two (Form2)
MsgBox Form1.txtDragValue
HTH, Jeremy
He who listens well, speaks well.
-
Jun 18th, 2003, 10:07 AM
#3
Re: reading a variable from a different form
Originally posted by Jorgin
I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
how can i do this?
thanks to all in advance.
you must declare it as public.. then you can simply access it by using
form1.variablename
-
Jun 18th, 2003, 10:12 AM
#4
Frenzied Member
If you are using a module you can make it Global or Public in the module and then you wont have to use the forms name..
with form..\
VB Code:
' in one form
public X as integer
' some sub in another form or module
form1.x = xxx
with a module (The prefered way IMO)..
VB Code:
' declarations
Global X as integer
'or
Public X as integer
' any place you want
x = xx
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 18th, 2003, 11:03 AM
#5
Fanatic Member
Globals
But then you have a bunch of (unnecessary) global variables running around that don't need that level of scope. Dangerous as you can unintentionally change the values anywhere in your code.
-
Jun 18th, 2003, 11:24 AM
#6
Frenzied Member
Re: Globals
Originally posted by Briantcva
But then you have a bunch of (unnecessary) global variables running around that don't need that level of scope. Dangerous as you can unintentionally change the values anywhere in your code.
They are NOT unnecessary if they are needed. The purpose of making them Global or Public in a module is so that they can be easiely access from anywhere in your code. like what is needed here. We have two forms sharing a variable. Make it Global and both hae equal access to it, and both will effect it globaly. Just like if you made it public within a form. The big difference here is that by making it Global/Public in a module you will not have to have both forms loaded all the time. If you make it public in a form, you will have to have form1 loaded for form2 to have access to the variable, Thus taking up memory...
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 18th, 2003, 11:31 AM
#7
Fanatic Member
I respectfully disagree
The original problem was how to pass a variable from Form1 to Form2. Form2 is the only variable consumer. Thus the variable only needs to exist within the scope of Form2. Were this variable needed for the whole project and multiple forms needed access and manipulation, then a global would be appropriate.
And yeah, I do realize we're arguing and vs. also.
-
Jun 18th, 2003, 11:32 AM
#8
The best way to handle the situation is to add a property to the first form using code like the following:
VB Code:
Option Explicit
Private mintValue As Integer
Public Property Get MyProperty() As Integer
MyProperty = mintValue
End Property
Public Property Let MyProperty(ByVal intNewVal As Integer)
mintValue = intNewVal
End Property
Usage in Form1
VB Code:
MyProperty = ValueToBeAssigned
Usage in Form2
VB Code:
ThevalueIs = Form1.MyProperty
-
Jun 18th, 2003, 11:37 AM
#9
Martin,
what would the benefit of having a let/get and private variable in the form versus just a public variable if you aren't doing anything in the let/get statements?
-
Jun 18th, 2003, 11:39 AM
#10
Fanatic Member
There are many ways to skin a cat, we all know this, but why make it any harder than it has to be? My first idea is the most simple, working example yet. USE IT!
He who listens well, speaks well.
-
Jun 18th, 2003, 11:45 AM
#11
Frenzied Member
Re: I respectfully disagree
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 18th, 2003, 11:46 AM
#12
Fanatic Member
He who listens well, speaks well.
-
Jun 18th, 2003, 11:56 AM
#13
Originally posted by kleinma
Martin,
what would the benefit of having a let/get and private variable in the form versus just a public variable if you aren't doing anything in the let/get statements?
- It's more flexible
- He's not doing anything now, but maybe later he'd like to
- It allows for validation
-
Jun 18th, 2003, 12:00 PM
#14
Originally posted by JCScoobyRS
There are many ways to skin a cat, we all know this, but why make it any harder than it has to be? My first idea is the most simple, working example yet. USE IT!
Yes your way is the simplest and there's nothing wrong with it by itself, but once you start throwing in public variables you can wind up with a mess unless you are careful.
-
Jun 18th, 2003, 12:04 PM
#15
Fanatic Member
Once again...I agree. No one is wrong here. Isn't that great. WOOHOO!!!
He who listens well, speaks well.
-
Jun 18th, 2003, 12:44 PM
#16
Hyperactive Member
Re: Re: Globals
Originally posted by RudyL
They are NOT unnecessary if they are needed. The purpose of making them Global or Public in a module is so that they can be easiely access from anywhere in your code. like what is needed here. We have two forms sharing a variable. Make it Global and both hae equal access to it, and both will effect it globaly. Just like if you made it public within a form. The big difference here is that by making it Global/Public in a module you will not have to have both forms loaded all the time. If you make it public in a form, you will have to have form1 loaded for form2 to have access to the variable, Thus taking up memory...
Rudy
that kind of defeats the purpose of object oriented programming though...... thats back to the old structured programming ways.
although global vars are needed at some points..... i think in this case its best to have it at the form-level. (public var within a form class)
-
Jun 18th, 2003, 01:11 PM
#17
Frenzied Member
Re: Re: Re: Globals
Originally posted by Eras3r
that kind of defeats the purpose of object oriented programming though...... thats back to the old structured programming ways.
although global vars are needed at some points..... i think in this case its best to have it at the form-level. (public var within a form class)
I can see your point about object oriented programming, but if all you need is the variable, then why create the object?
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 18th, 2003, 01:32 PM
#18
Fanatic Member
Re: Re: Re: Re: Globals
Originally posted by RudyL
I can see your point about object oriented programming, but if all you need is the variable, then why create the object?
B/c the variable only exists within, and b/c of, the (form) object - it's unnecessary outside of it. To use your memory example from before, why allocate storage space for the lifetime of a project (a global) when it's (the variable) only needed in two froms which always talk to one another (i.e, form2 only exists b/c form1 called it)? For the same reaon we don't dim longs when we only need a byte, or don't declare everything as variants.
-
Jun 18th, 2003, 01:54 PM
#19
Hyperactive Member
he wasnt making a form just to hold a variable... i assumed that form was a functional part of his program and had purpose.
making a form to hold variables wouldnt make any sense... because you just just stick the variable in the form your actually using... lol?
-
Jun 18th, 2003, 03:11 PM
#20
Frenzied Member
Originally posted by Eras3r
he wasnt making a form just to hold a variable... i assumed that form was a functional part of his program and had purpose.
making a form to hold variables wouldnt make any sense... because you just just stick the variable in the form your actually using... lol?
That isn't what I ment..
Lets say you have a variable called X. You create it in form1 and use it for stuff like counters in "while loops" or "for next" statement.. (For example)
Now you have a new form, form2. In that form you have loop as well so you use X as your counter. Well, in order to do this you need to use "form1.x = ", thus causing form1 to load. Now form1 may already be loaded, but still a bad practice. You should have a module for variables like that.
Now I will say that I might be over analyzing the scope here. In this case, assuming (bad thing to do) that form2 is only driven by form1, then I agree with using a variable in form1 for form2. But unless you release the form from memory when you are done with it (The form, not the program, big difference) you are still holding that variable in memory, thus having no different effect on the code (memory ways) than creating a global/public variable. But you will have the annoying task of calling form1.x everytime for no reason...
I guess the moral is, make sure you release your form form memory to make using a local variable to form usefull..
Make sense??
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 18th, 2003, 03:12 PM
#21
Fanatic Member
Jorgin,
Can you please tell us if your issue has been resolved? Later, Jeremy
He who listens well, speaks well.
-
Jun 18th, 2003, 03:29 PM
#22
PowerPoster
for what my 2 cents is worth:
I dont see what all the panic is about Public variables in modules. I dont mind using them when needed. It certainly makes more sense to me than passing paramaters around all over the place when a simple public declaration would suffice. I can see how they can be dangerous, but so can 90% of everything else we use in VB.
-
Jun 26th, 2003, 03:25 PM
#23
New Member
Passing array between forms
I'm doing something similar, only I'm using a dynamic array. I'm trying to write an app to add a list of users to a group. As
I've got it now, the array is declared and utilized only in Form2 and Form3 (it prints it's contents to a ListBox in 3).
However, I want Form1 to utilize the array once it's been populated, and unload Form2&3.
As I understand this, I need to move the declaration of UserArray() out of the subroutine on Form2 and make it a general
declaration as a public array on Form1? What happens to the array when Form2 gets unloaded? Will the array still be populated
with the data provided by Form2?
VB Code:
'This is Form1 as it stands
Private Sub AddMember_Click()
Dim Group As IADsGroup
Dim GroupName As String
Dim GroupDomain As String
Dim User As IADsUser
Dim UserName As String
Dim UserDomain As String
Dim Prompt As String
Prompt = "Please enter the target group."
GroupName = InputBox(Prompt)
GroupDomain = "TestDomain"
UserName = "target_user_name" 'This is gonna get changed to UserArray()
UserDomain = "LSNA"
'Obviously this next part will have to be changed to utilize an array.
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
Set Group = GetObject("WinNT://" & GroupDomain & "/" & GroupName & ",group")
Group.Add (User.ADsPath)
Group.SetInfo
End Sub
'This is the main subroutine of Form2 as it stands
Private Sub File1_Click()
Dim UserList
Dim UserArray() As String
Dim UserCounter As Integer
Dim X As Integer
Set Fso = CreateObject("Scripting.FileSystemObject")
SelectedFile = File1.Path & "\" & File1.FileName
Set UserList = Fso.OpenTextFile(SelectedFile, 1)
UserCounter = 0
ReDim UserArray(UserCounter + 1)
Do Until UserList.AtEndOfLine = True
ReDim Preserve UserArray(UBound(UserArray) + 1)
UserArray(UserCounter) = UserList.ReadLine
UserCounter = UserCounter + 1
MsgBox (UserCounter)
Loop
Load Form3
For X = 0 To (UserCounter - 1)
Form3.List1.AddItem UserArray(X)
Next X
Form3.Show 1
End Sub
-
Jun 26th, 2003, 03:33 PM
#24
Frenzied Member
If the array is declared within a form, general or in a sub it will be destroyed (removed from memory) when the form is unloaded or even if it is "reloaded".
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 26th, 2003, 03:39 PM
#25
New Member
What I'm thinking is I declare the dynamic array as being public in Form1. Then Form2 loads, populates the array and then gets unloaded, returning back to Form1. Since Form1 was open the entire time, will the array remain in tact or will it get busted up when Form2 closes?
-
Jun 26th, 2003, 03:43 PM
#26
Frenzied Member
As long as form1 is still loaded it will be fine.
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Jun 26th, 2003, 04:07 PM
#27
New Member
Hmmm... I've hit a new snag . At the top of Form1, I've created the following general declaration:
VB Code:
Public UserArray() As String
When I test the app, I immediately get a compile error that says:
Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object models
I haven't been doing this long enough to understand why it's not working
-
Jun 26th, 2003, 04:22 PM
#28
Lively Member
That's because you can't have public arrays. You can create an array on one form and then set public regular variables to the part of an array that you want to transfer to another form.
Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules.
Which means "Arrays are not allowed as Public members of object modules."
Try creating a BAS module and put that syntax in that BAS module, then try it.
~B-Rabbit
-
Jun 26th, 2003, 05:09 PM
#29
New Member
Sorry, I don't follow. So what I should do is declare the array in the subroutine of Form1, then call on it form Form2 so that it can be populated? How do I do that?
Thanks for the help. I greatly appreciate it.
-
Jun 26th, 2003, 08:34 PM
#30
Lively Member
No, you create a module by going to Project -> Add Module. Then, in that module, you can do type Public UserArray() As String.
-
Jun 26th, 2003, 09:12 PM
#31
Fanatic Member
Originally posted by Keeper
Sorry, I don't follow. So what I should do is declare the array in the subroutine of Form1, then call on it form Form2 so that it can be populated? How do I do that?
Thanks for the help. I greatly appreciate it.
As B-Rabbit said, you can't have a public array in a form. That's because Arrays cannot expose thier properties as public members of a class. A from is just a module of the form class. Arrays can't be public members of class modules either.
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
-
Jun 26th, 2003, 09:59 PM
#32
PowerPoster
Originally posted by RudyL
If the array is declared within a form, general or in a sub it will be destroyed (removed from memory) when the form is unloaded or even if it is "reloaded".
Rudy
All Form Variables are still there in memory even if you "unload" the form. All that happens when you "unload" a form is the visual components are gone, but every variable that form had is still in memory. Unless you set the form to = Nothing you can unload the form the load it again and still get your Variable values...
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jun 26th, 2003, 10:11 PM
#33
Lively Member
How? Can you give an example?
-
Jun 26th, 2003, 10:18 PM
#34
Originally posted by B-Rabbit
How? Can you give an example?
Form1:
VB Code:
Option Explicit
Private Sub Command1_Click()
Form2.Show
End Sub
Form2:
VB Code:
Option Explicit
Public SomeVar As Long
Private Sub Command1_Click()
SomeVar = SomeVar + 1
Caption = SomeVar
End Sub
Private Sub Form_Load()
Caption = SomeVar
End Sub
Click the button on Form1 to load Form2, then click the button on Form2 to increment the counter.
Close Form2, unloading it, then Click the Button on Form1 to reload Form2, notice the variable is still set to it's last value.
-
Jun 26th, 2003, 10:22 PM
#35
PowerPoster
2 forms
Form 1 has 1 Public Variable called Test as String declared in the general section of the form
Form 1 has 2 buttons
Button1 has code that gives Test a value and unloads form1 and shows Form 2.
Button2 has code that has a msgbox show the value of Test.
Form 2 has 1 button
Button1 loads Form 1
When Form 1 is loaded press button2 on Form 1 and it will display the value of the Variable Test that you gave it before you unloaded Form1.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Jun 26th, 2003, 10:26 PM
#36
Lively Member
I knew that, I thought you meant when your app unloads and there are no more forms, you can still retrieve variables, although you can't.
-
Jun 26th, 2003, 10:29 PM
#37
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|