Results 1 to 4 of 4

Thread: [VB6] - Public variables in bas file, but private in instances

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,959

    [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
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,959

    Re: [VB6] - Public variables in bas file, but private in instances

    Quote Originally Posted by LaVolpe View Post
    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
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width