Module Scope between instance of the same class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frederic H

    Module Scope between instance of the same class

    Hi all,

    If someone as an explanation ... :) Look at this...

    Class MultithreadTest
    ' <--------------
    Option Explicit
    Public Sub Initialize()
    Nbr = 0
    End Sub
    Public Function GetNbr() As Integer
    GetNbr = Nbr
    End Function
    Public Sub Add()
    Nbr = Nbr + 1
    End Sub
    ' -------------->

    Module.bas
    ' <--------------
    Public Nbr As Integer
    ' -------------->

    Application
    ' <--------------
    Private Sub Command1_Click( )
    Dim obj1 As New Srv.Multithread Test
    Dim obj2 As New Srv.Multithread Test
    obj1.Add
    obj1.Add
    obj1.Add
    obj1.Add
    obj1.Add
    obj1.Add
    MsgBox "OBJ1 " & obj1.GetNbr & " ---- " & "OBJ2 " & obj2.GetNbr
    Set obj1 = Nothing
    Set obj2 = Nothing
    End Sub
    ' -------------->

    Result : "OBJ1 6 ---- OBJ2 6"

    Should be "OBJ1 6 ---- OBJ2 0" ?
    Bug ?
    Shared variables in module between objects ?


    Thx for your help


  • Larry Lard

    #2
    Re: Module Scope between instance of the same class

    Frederic H wrote:[color=blue]
    > Hi all,
    >
    > If someone as an explanation ... :) Look at this...
    >
    > Class MultithreadTest
    > ' <--------------
    > Option Explicit
    > Public Sub Initialize()
    > Nbr = 0
    > End Sub
    > Public Function GetNbr() As Integer
    > GetNbr = Nbr
    > End Function
    > Public Sub Add()
    > Nbr = Nbr + 1
    > End Sub
    > ' -------------->
    >
    > Module.bas
    > ' <--------------
    > Public Nbr As Integer
    > ' -------------->
    >
    > Application
    > ' <--------------
    > Private Sub Command1_Click( )
    > Dim obj1 As New Srv.Multithread Test
    > Dim obj2 As New Srv.Multithread Test
    > obj1.Add
    > obj1.Add
    > obj1.Add
    > obj1.Add
    > obj1.Add
    > obj1.Add
    > MsgBox "OBJ1 " & obj1.GetNbr & " ---- " & "OBJ2 " & obj2.GetNbr
    > Set obj1 = Nothing
    > Set obj2 = Nothing
    > End Sub
    > ' -------------->
    >
    > Result : "OBJ1 6 ---- OBJ2 6"
    >
    > Should be "OBJ1 6 ---- OBJ2 0" ?[/color]

    Why would it be that?
    [color=blue]
    > Bug ?[/color]

    No.
    [color=blue]
    > Shared variables in module between objects ?[/color]

    Yes. Variables declared in modules are global to the whole application.
    If you want class variables then declare them in the class!

    --
    Larry Lard
    Replies to group please

    Comment

    • Frederic H

      #3
      Re: Module Scope between instance of the same class

      [color=blue][color=green]
      > > Shared variables in module between objects ?[/color]
      >
      > Yes. Variables declared in modules are global to the whole application.
      > If you want class variables then declare them in the class!
      >[/color]

      Yes indeed, they are global in the application but intra objects... It's
      like a Shared variable in VB.NET...

      Comment

      • David Anton

        #4
        Re: Module Scope between instance of the same class

        All module methods and variables are implicitly shared.
        --
        David Anton
        Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

        Instant C#: VB.NET to C# Converter
        Instant VB: C# to VB.NET Converter
        Instant J#: VB.NET to J# Converter
        Clear VB: Cleans up outdated VB.NET code


        "Frederic H" wrote:
        [color=blue]
        >[color=green][color=darkred]
        > > > Shared variables in module between objects ?[/color]
        > >
        > > Yes. Variables declared in modules are global to the whole application.
        > > If you want class variables then declare them in the class!
        > >[/color]
        >
        > Yes indeed, they are global in the application but intra objects... It's
        > like a Shared variable in VB.NET...[/color]

        Comment

        • Frederic H

          #5
          Re: Module Scope between instance of the same class

          Ok thanks.Well, Bad news for me ...
          Is it possible to explicitly put the variable as "NOT" shared or the
          unique solution is to convert modules to classes ?

          Comment

          • David Anton

            #6
            Re: Module Scope between instance of the same class

            You'll need to put the variable into a class to have it be instance specific.
            (modules' members are always shared)
            --
            David Anton
            Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

            Instant C#: VB.NET to C# Converter
            Instant VB: C# to VB.NET Converter
            Instant J#: VB.NET to J# Converter
            Clear VB: Cleans up outdated VB.NET code


            "Frederic H" wrote:
            [color=blue]
            > Ok thanks.Well, Bad news for me ...
            > Is it possible to explicitly put the variable as "NOT" shared or the
            > unique solution is to convert modules to classes ?[/color]

            Comment

            • david

              #7
              Re: Module Scope between instance of the same class

              On 2005-09-29, Frederic H <FredericH@disc ussions.microso ft.com> wrote:[color=blue]
              > Ok thanks.Well, Bad news for me ...
              > Is it possible to explicitly put the variable as "NOT" shared or the
              > unique solution is to convert modules to classes ?[/color]

              The real question is if you want each instance of MultiThreadClas s to
              get its own copy of Nbr, why aren't you defining it as a class variable
              within MultiThreadClas s.

              And just to be snarky (but serious), why not "Number" instead of Nbr?


              Comment

              • Frederic H

                #8
                Re: Module Scope between instance of the same class

                Sure, in POO it's like that. But this COM DLL is developped with modules...
                Global variables are delcared on modules <= really bad idea.

                When I use multithreading I have one instance of the DLL on each thread.
                Then you understand that DLL dont give the good result :)

                I will try to convert DLL code in a TRUE object oriented code...

                Thanks for your help !

                "david" a écrit :
                [color=blue]
                > On 2005-09-29, Frederic H <FredericH@disc ussions.microso ft.com> wrote:[color=green]
                > > Ok thanks.Well, Bad news for me ...
                > > Is it possible to explicitly put the variable as "NOT" shared or the
                > > unique solution is to convert modules to classes ?[/color]
                >
                > The real question is if you want each instance of MultiThreadClas s to
                > get its own copy of Nbr, why aren't you defining it as a class variable
                > within MultiThreadClas s.
                >
                > And just to be snarky (but serious), why not "Number" instead of Nbr?
                >
                >
                >[/color]

                Comment

                Working...