Public Variables in all forms

Peperl
03-04-2004, 12:20 PM
Hi everybody

If I have a form, and I declare a public var in it like:


Option explicit

Public MyOwnVar

' ... the code
' Private SUB Form_Load ()



I could use it from other place in the app, ej: MyForm.MyOwnVar = 10

but ... there is a way to declare a property that affecs to all the forms in my app?. What I don't want to do is to declare that property in each form


Thanks

(If I'm not clear, please tell me and I'll try to explain a little better)

thingimijig
03-04-2004, 12:26 PM
put it in a module.

thingimijig.

Smeagol
03-04-2004, 12:29 PM
Hi everybody

If I have a form, and I declare a public var in it like:


Option explicit

Public MyOwnVar

' ... the code
' Private SUB Form_Load ()



I could use it from other place in the app, ej: MyForm.MyOwnVar = 10

but ... there is a way to declare a property that affecs to all the forms in my app?. What I don't want to do is to declare that property in each form


Thanks

(If I'm not clear, please tell me and I'll try to explain a little better)


if you want to change a property in all forms at run time, you can use the Forms object and a For Each loop.

Check it out. You can loop through all your forms and change thier properties.

Peperl
03-04-2004, 12:32 PM
Well .. what I want to do is to "invent" a property to that form ...

that i could do in every place:


MyForm1.MyProperty = "1"
MyForm2.MyProperty = "2"


without to have to declare MyProperty for each form individually

Agent707
03-04-2004, 12:43 PM
I don't think you can create a property for a control.

MyForm1.MyProperty = "1"
MyForm2.MyProperty = "2"

You should create an Array for your property something like

'Module
'Say you have 7 forms
Public MyCustomFormValue(1 to 7)

'Then you can just set the value for each of the forms
'For form3
MyCustomFormValue(3) = "Whatever"

That make sense?

zeek
03-04-2004, 01:44 PM
Here's how to solve that problem:

Let's say you have Form1, Form2 and Form3

In Form1 you declare a public variable as follows:

Option Explicit

Public MyOwnVar

' ... the code
' Private SUB Form_Load ()


In all other forms (Form2 and Form3) you now can access this variable like follows:

Form1.MyOwnVar = "test"
'or
msgbox(Form1.MyOwnVar)
'and so on...


Is that what you wanted to know?

Zeek

Peperl
03-04-2004, 02:23 PM
In all other forms (Form2 and Form3) you now can access this variable like follows:

Form1.MyOwnVar = "test"
'or
msgbox(Form1.MyOwnVar)



not exactly ...

I want that all forms have the MyOwnVar without that I have to declare it in each form

that i want is that all forms have his <OwnVar>

zeek
03-04-2004, 03:32 PM
You don't have to declare it in every form... The Variable is accessible in every form just by using Form1.MyOwnVar

Zeek

Gilad_r
03-04-2004, 03:49 PM
"I want that all forms have the MyOwnVar |without that I have to declare it in each form"

I don't think that's possible, you have to declare variables..
how many forms do you have?

zeek
03-04-2004, 04:00 PM
You only have to declare the variable in the first form (Form1) as public.

After that it is accessable in ANY other form of your project w/out declaring it there!

In Form2, Form3, Form4, ....

You just have to use Form1.MyOwnVar instead of just MyOwnVar in all other Forms.

Got it? :D

Zeek

Agent707
03-04-2004, 04:06 PM
But if you declare it in a Module, you don't have to use form1.MyOwnVar, you just use MyOwnVar.

However, I don't see where he's going with this anyway.

zeek
03-04-2004, 04:10 PM
Maybe he doesn't want to add a module to his project just because of one single public var... :)

Either way will do it and I'm sure he'll understand that pretty soon.... :p

Zeek

Alex C
03-04-2004, 04:12 PM
From what you say in your first post you don't want to declare the variable in any Form so declaring it in a Module seems like what you want?

zeek
03-04-2004, 04:17 PM
He wrote he doesn't want to declare it in each form...

Nevermind, guess he'll make it... :)

Zeek

Gilad_r
03-04-2004, 04:33 PM
from what I understand he wants every form to have a DIFFERENT MyOwnVar without having to WRITE a declaration in every form...

zeek
03-04-2004, 04:51 PM
Where's the problem then? In VB it's possible to use a variable w/out having to declare it at all, as long as "Option Explicit" is not set.

Zeek

Peperl
03-04-2004, 05:11 PM
Hi!

Well .. I'll try to exaplain what I have in mind

Always I make what you said, make a public var when i want to store a value, and calling it from other places, like a year or anything

But if I have more than one form open that uses that value i have to declarate more than one public var and i thinked that it was a mess, and maybe there was a way to create a new value of each form.

So I have a new <TAG> to store values

I could make that in the TAG but how I store here more than one value i thinked that if I put a var named <Anio> for each form, i improved the code and clarity.

P.S: My idea is that a form have a different value, and Form1.Anio could be differeent to Form2.anio

Gilad_r
03-04-2004, 05:16 PM
you mean two vars with the same name but with different values?
if so, then just set the var as Private and you can give a var on another Form the same name... as long as you set it to Private too.

zeek
03-04-2004, 05:18 PM
I see! If you don't need the variables in any other forms, simply declare them as private.

You can also declare a public array in a module, like this:

Public Anio(5) As Long

After that you could use Anio(0) for first form, Anio(1) for second form and so on.

If you don't want to use an array but need to access the variables though all forms, you have to declare the variables in every form.

Zeek

Peperl
03-04-2004, 05:20 PM
No no ... (ugh ... i have to speak really bad english :( ...

what I said is, at the same way that every form as a property named TAG where you can put a value, create other property named like I want

This is not the problem, the problem is that I want that only one time and all the users have this property.


Could you think that it can be with a class module?

Gilad_r
03-04-2004, 05:23 PM
I don't think you can add properties to a Form..
don't take my word for it though

zeek
03-04-2004, 05:41 PM
Sure you can...

See http://www.visualbasicforum.com/showthread.php?threadid=150404

But I still couldn't figure out what exactly you wanna know... Can you post an example to make it more clear?

Zeek

Peperl
03-05-2004, 02:27 AM
It's only ideas ... it's for a program than I make what works with expedients. If i could have more properties for each form I could store here the code of the expediente, so each form have his own code and works with his expedient only

If I do that with public variables every time that I make a form I have to create his variable with his name and his value. The way i want to do things all forms I want automatically have that property and I could called it at run mode

If i could i'm try what you said your post and if I make, I'll talk with the admins to post

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum