Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Public Variables in all forms


Reply
 
Thread Tools Display Modes
  #1  
Old 03-04-2004, 11:20 AM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default Public Variables in all forms


Hi everybody

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

Code:
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)
__________________
Pri Oh Persia
Reply With Quote
  #2  
Old 03-04-2004, 11:26 AM
thingimijig thingimijig is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Posts: 1,300
Default

put it in a module.

thingimijig.
Reply With Quote
  #3  
Old 03-04-2004, 11:29 AM
Smeagol Smeagol is offline
Junior Contributor
 
Join Date: Nov 2003
Location: Philadelphia, Pa. USA
Posts: 197
Default

Quote:
Originally Posted by Peperl
Hi everybody

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

Code:
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.
Reply With Quote
  #4  
Old 03-04-2004, 11:32 AM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default

Well .. what I want to do is to "invent" a property to that form ...

that i could do in every place:

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

without to have to declare MyProperty for each form individually
__________________
Pri Oh Persia
Reply With Quote
  #5  
Old 03-04-2004, 11:43 AM
Agent707 Agent707 is offline
Retired Contributor
 
Join Date: Mar 2002
Posts: 1,829
Default

I don't think you can create a property for a control.
Code:
MyForm1.MyProperty = "1" MyForm2.MyProperty = "2"
You should create an Array for your property something like
Code:
'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?

Last edited by Agent707; 03-04-2004 at 03:03 PM.
Reply With Quote
  #6  
Old 03-04-2004, 12:44 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

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:
Code:
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:
Code:
Form1.MyOwnVar = "test" 'or msgbox(Form1.MyOwnVar) 'and so on...

Is that what you wanted to know?

Zeek
Reply With Quote
  #7  
Old 03-04-2004, 01:23 PM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default

Quote:
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>
__________________
Pri Oh Persia
Reply With Quote
  #8  
Old 03-04-2004, 02:32 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

You don't have to declare it in every form... The Variable is accessible in every form just by using Form1.MyOwnVar

Zeek
Reply With Quote
  #9  
Old 03-04-2004, 02:49 PM
Gilad_r's Avatar
Gilad_r Gilad_r is offline
Senior Contributor
 
Join Date: Mar 2004
Posts: 851
Default

"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?
Reply With Quote
  #10  
Old 03-04-2004, 03:00 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

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?

Zeek
Reply With Quote
  #11  
Old 03-04-2004, 03:06 PM
Agent707 Agent707 is offline
Retired Contributor
 
Join Date: Mar 2002
Posts: 1,829
Default

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.
Reply With Quote
  #12  
Old 03-04-2004, 03:10 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

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....

Zeek
Reply With Quote
  #13  
Old 03-04-2004, 03:12 PM
Alex C Alex C is offline
Centurion
 
Join Date: Feb 2004
Location: Tasmania, Australia
Posts: 114
Default

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?
Reply With Quote
  #14  
Old 03-04-2004, 03:17 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

He wrote he doesn't want to declare it in each form...

Nevermind, guess he'll make it...

Zeek
Reply With Quote
  #15  
Old 03-04-2004, 03:33 PM
Gilad_r's Avatar
Gilad_r Gilad_r is offline
Senior Contributor
 
Join Date: Mar 2004
Posts: 851
Default

from what I understand he wants every form to have a DIFFERENT MyOwnVar without having to WRITE a declaration in every form...
Reply With Quote
  #16  
Old 03-04-2004, 03:51 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

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
Reply With Quote
  #17  
Old 03-04-2004, 04:11 PM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default

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
__________________
Pri Oh Persia
Reply With Quote
  #18  
Old 03-04-2004, 04:16 PM
Gilad_r's Avatar
Gilad_r Gilad_r is offline
Senior Contributor
 
Join Date: Mar 2004
Posts: 851
Default

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.
Reply With Quote
  #19  
Old 03-04-2004, 04:18 PM
zeek's Avatar
zeek zeek is offline
Regular
 
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
Default

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:

Code:
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
Reply With Quote
  #20  
Old 03-04-2004, 04:20 PM
Peperl's Avatar
Peperl Peperl is offline
Senior Contributor
 
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
Default

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?
__________________
Pri Oh Persia
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PostMessage, SendMessage, SendInput, Not what I need.. Cert API 29 02-01-2004 04:54 AM
Hardware Information Mimo .NET General 13 01-29-2004 03:17 AM
.bas in .dll files Viperchief General 5 03-29-2003 09:17 PM
Eeeeeek! lighth7015 API 4 02-25-2003 10:24 PM
MS Winsocket gibson General 3 07-20-2001 10:03 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->