|
-
Nov 13th, 2009, 04:40 PM
#1
Thread Starter
PowerPoster
[VB6] - Public variables in bas file, but private in instances
i have some public variables in a bas(module) file, how can i put them private for other instances?
thanks
-
Nov 13th, 2009, 08:34 PM
#2
Re: [VB6] - Public variables in bas file, but private in instances
Since this is posted in the ActiveX/COM forum, I will make an assumption this is related to a usercontrol or dll (referred to as object below)?
Public is Public. When in a bas module, public variables are shared among all instances of your object. If one instance of your object changes the value, that is the value all instances will see.
If you want variables private to each instance, declare them in the object not the bas module. For example, in one of my projects, the bas module has a public bitmap that all instances of my object can call and draw on. When one object needs it, it calls the module and gets the bitmap. Anything drawn on it before is now gone. All instances of my object know that and assume the bitmap should be considered blank. Continuing with that example, if I wanted each object to have its own bitmap, I'd create one in each object when needed and it would be private to that object.
-
Nov 14th, 2009, 02:06 AM
#3
Thread Starter
PowerPoster
Re: [VB6] - Public variables in bas file, but private in instances
 Originally Posted by LaVolpe
Since this is posted in the ActiveX/COM forum, I will make an assumption this is related to a usercontrol or dll (referred to as object below)?
Public is Public. When in a bas module, public variables are shared among all instances of your object. If one instance of your object changes the value, that is the value all instances will see.
If you want variables private to each instance, declare them in the object not the bas module. For example, in one of my projects, the bas module has a public bitmap that all instances of my object can call and draw on. When one object needs it, it calls the module and gets the bitmap. Anything drawn on it before is now gone. All instances of my object know that and assume the bitmap should be considered blank. Continuing with that example, if I wanted each object to have its own bitmap, I'd create one in each object when needed and it would be private to that object.
but i can't declare public enums\types variables in my project(usercontrol), because give me an error
see these example:
Code:
Public Type JOYINFOEX
dwSize As Long
dwFlags As Long
dwXpos As Long
dwYpos As Long
dwZpos As Long
dwRpos As Long
dwUpos As Long
dwVpos As Long
dwButtons As Long
dwButtonNumber As Long
dwPOV As Long
dwReserved1 As Long
dwReserved2 As Long
End Type
Public MYJOYEX As JOYINFOEX
in usercontrol, in General section, i can't do these variables(publics), then how can i resolve the problem?
thanks
-
Nov 14th, 2009, 10:11 AM
#4
Re: [VB6] - Public variables in bas file, but private in instances
You can declare them in two places:
If you want UDTs/Enums public to just your usercontrol & classes and not public to the customer/user, then declare them public in the bas module. Of course do not try to pass UDT as method parameters. Do not use those enums in public methods.
If you want them public to the user and your project, then create a class that only has the public Enums & Types.
Edited: Public Enums can be added just to your usercontrol if you wish; you should have no problems with them. But UDTs are different.
Try this experiment
1. Create a NEW usercontrol project
2. Add new class and paste this code in that class
Code:
Option Explicit
Public Enum BorderStyle
Style3D = 0
StyleFlat = 1
StyleSunken = 2
End Enum
Public Type JOYINFOEX
dwSize As Long
dwFlags As Long
dwXpos As Long
dwYpos As Long
dwZpos As Long
dwRpos As Long
dwUpos As Long
dwVpos As Long
dwButtons As Long
dwButtonNumber As Long
dwPOV As Long
dwReserved1 As Long
dwReserved2 As Long
End Type
3. In a bas module copy & paste this code
Code:
Public MyJOYEX As JOYINFOEX
4. In your user control, copy & paste this code
Code:
Option Explicit
Public Sub SetJoyStick(joy As JOYINFOEX)
MyJOYEX = joy
MsgBox "Joy Button Number is " & MyJOYEX.dwButtonNumber
End Sub
Public Property Get Style() As BorderStyle
Style = StyleFlat
End Property
5. Now from the IDE menu click: File | Add Project and add a new EXE project
6. Right click on that project in the project explorer and make it the Startup project
7. Add one usercontrol to the form and copy & paste this code
Code:
Private Sub Form_Load()
Dim J As JOYINFOEX
J.dwButtonNumber = 10
UserControl11.SetJoyStick J
Debug.Print UserControl11.Style = StyleFlat
End Sub
8. Run the project. Test it compiled too, just to be sure it still works.
Last edited by LaVolpe; Nov 14th, 2009 at 01:53 PM.
Reason: typo
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
|