Results 1 to 17 of 17

Thread: [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Resolved [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    When it comes to the organization of VB6 source code, another question comes to my mind:

    C# makes code better organized by providing static variables and static functions.

    We can simulate public static variables and public static functions in C# by providing Public Functions in VB-Module. But how do we simulate variables/functions decorated with "private static"? E.g:
    Code:
    public class MyClass {
        private static readonly int myLength = 1000;
        private static readonly int mask = BitVector32.CreateMask();
    
        private static bool IsKeyDown(Keys key) {
    
        }
    }
    Last edited by SearchingDataOnly; May 14th, 2022 at 11:13 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Should be as simple as just not making them public.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by DataMiser View Post
    Should be as simple as just not making them public.
    It doesn't seem to be the case. If "private static" is defined as private in VB-Module, then these variables and functions cannot be accessed by functions inside MyClass.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    I don't understand how what you're asking is different from a private constant that's module level inside the class module... Since VB doesn't support inline classes.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by fafalone View Post
    I don't understand how what you're asking is different from a private constant that's module level inside the class module... Since VB doesn't support inline classes.
    My question doesn't seem to be related to inline-classes.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    If this is about your transpiler and you want to transpile similar behavior to VB6 from your language. It's actually not that difficult. VB6 modules provide most of what you need already. Lets go through it bit by bit. Lets start with something simple like this:-
    Code:
            class Class1
            {
                private static int _myInt = 10;
            
                private static int GetValue() { return _myInt * 2; }
    
            }
    The above could be transpiled to VB6 like this:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    
    Private g_init As Boolean
    Private g_myInt As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt = 10
            g_init = True
        End If
    End Sub
    
    Private Function GetValue() As Long
        Init
        GetValue = g_myInt * 2
    End Function
    The above is normal module that mimics the C# behavior. Lets look at something with a little more meat:-
    Code:
            class Class1
            {
    
                private static int myInt1 = 20;
    
                public static int myInt2 = 40;
    
                private static int Method1() { return myInt1; }
    
                public static int Method2() { return myInt2; }
    
            }
    It would look like this in VB6:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    
    Private g_init As Boolean
    
    Private g_myInt1 As Long
    
    Public g_myInt2 As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt1 = 20
            
            g_myInt2 = 40
            
            g_init = True
        End If
    End Sub
    
    
    Private Function Method1() As Long
        Init
        Method1 = g_myInt1
    End Function
    
    Public Function Method2() As Long
        Init
        Method2 = g_myInt2
    End Function
    The idea here is very simple and straightforward. If it's a private static variable or function then it becomes a private variable or function in the VB6 module. If it's a public static variable or function, then it becomes a public variable or function in the VB6 module.

    Now lets look at something that is a little more tricky. I want you to really pay attention to this part because it is extremely important. Lets say you have this:-
    Code:
            class Class1
            {
                private static int myInt = 100;
    
                private static int GetValue() { return myInt; } 
    
                public int DoCalc(int a) { return GetValue() * a; }
            }
    Now we have a class where a non-static method of the class is calling a private static method. Highlighted in red is the call to the private static method of the class. What's also important is that the class now has a non-static member which means we need to involve class modules in VB6. The above code could be transpiled to VB6 like this:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    Private g_init As Boolean
    
    Private g_myInt As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt = 100
            
            g_init = True
        End If
    End Sub
    
    Public Function GetValue() As Long
        Init
        GetValue = g_myInt
    End Function
    Code:
    '================
    'Class1.cls
    '================
    Public Function DoCalc(ByVal a As Long)
        DoCalc = STATIC_Class1.GetValue() * a
    End Function
    There are two key things here. One, we use a module to represent all the static members of the class and we use a class module for all the non-static members. Another very important detail is highlighted in red above which is the fact that the private static method has been rendered as a public module in VB6. We must do this because it must be callable from the class module.

    It's important that your transpiler check for non-static members calling private static members because in these cases the member must be made public in the module. If no instance member ever calls a private static method or use a private static variable, then they can be rendered as private in a VB6 module.

    This setup will give you all the proper behaviors one would expect for static members in languages like C#.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by SearchingDataOnly View Post
    My question doesn't seem to be related to inline-classes.
    Yes so how is this not just about a private module level member of a class module?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by Niya View Post
    If this is about your transpiler and you want to transpile similar behavior to VB6 from your language. It's actually not that difficult. VB6 modules provide most of what you need already. Lets go through it bit by bit. Lets start with something simple like this:-
    Code:
            class Class1
            {
                private static int _myInt = 10;
            
                private static int GetValue() { return _myInt * 2; }
    
            }
    The above could be transpiled to VB6 like this:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    
    Private g_init As Boolean
    Private g_myInt As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt = 10
            g_init = True
        End If
    End Sub
    
    Private Function GetValue() As Long
        Init
        GetValue = g_myInt * 2
    End Function
    The above is normal module that mimics the C# behavior. Lets look at something with a little more meat:-
    Code:
            class Class1
            {
    
                private static int myInt1 = 20;
    
                public static int myInt2 = 40;
    
                private static int Method1() { return myInt1; }
    
                public static int Method2() { return myInt2; }
    
            }
    It would look like this in VB6:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    
    Private g_init As Boolean
    
    Private g_myInt1 As Long
    
    Public g_myInt2 As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt1 = 20
            
            g_myInt2 = 40
            
            g_init = True
        End If
    End Sub
    
    
    Private Function Method1() As Long
        Init
        Method1 = g_myInt1
    End Function
    
    Public Function Method2() As Long
        Init
        Method2 = g_myInt2
    End Function
    The idea here is very simple and straightforward. If it's a private static variable or function then it becomes a private variable or function in the VB6 module. If it's a public static variable or function, then it becomes a public variable or function in the VB6 module.

    Now lets look at something that is a little more tricky. I want you to really pay attention to this part because it is extremely important. Lets say you have this:-
    Code:
            class Class1
            {
                private static int myInt = 100;
    
                private static int GetValue() { return myInt; } 
    
                public int DoCalc(int a) { return GetValue() * a; }
            }
    Now we have a class where a non-static method of the class is calling a private static method. Highlighted in red is the call to the private static method of the class. What's also important is that the class now has a non-static member which means we need to involve class modules in VB6. The above code could be transpiled to VB6 like this:-
    Code:
    '=====================
    'STATIC_Class1.bas
    '=====================
    Private g_init As Boolean
    
    Private g_myInt As Long
    
    Private Sub Init()
        If Not g_init Then
            g_myInt = 100
            
            g_init = True
        End If
    End Sub
    
    Public Function GetValue() As Long
        Init
        GetValue = g_myInt
    End Function
    Code:
    '================
    'Class1.cls
    '================
    Public Function DoCalc(ByVal a As Long)
        DoCalc = STATIC_Class1.GetValue() * a
    End Function
    There are two key things here. One, we use a module to represent all the static members of the class and we use a class module for all the non-static members. Another very important detail is highlighted in red above which is the fact that the private static method has been rendered as a public module in VB6. We must do this because it must be callable from the class module.

    It's important that your transpiler check for non-static members calling private static members because in these cases the member must be made public in the module. If no instance member ever calls a private static method or use a private static variable, then they can be rendered as private in a VB6 module.

    This setup will give you all the proper behaviors one would expect for static members in languages like C#.
    Very detailed and helpful reply, thank you very much, Niya.

    Maybe the C language also adopts this method to implement the definition of the class.

    We have to admit that static variables/functions in c# make the code better organized. It takes a lot of effort to simulate these features in VB6. Thankfully, the heavy and dirty work can be done in-house by Transpiler.

    VB6 doesn't give us a door, but it gives us a window.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by fafalone View Post
    Yes so how is this not just about a private module level member of a class module?
    Niya explained my problem in great detail and gave a good solution.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by SearchingDataOnly View Post
    We have to admit that static variables/functions in c# make the code better organized.
    Static members are naturally the next stage in the evolution of a system like VB6 modules. It combines the features of modules and classes into a single system. A VB6 module can be thought of as a class with only static members.

    Quote Originally Posted by SearchingDataOnly View Post
    It takes a lot of effort to simulate these features in VB6.
    Well remember that VB6 is decades behind languages like C# and JavaScript so some features are going to take some real effort to map.

    However, the true challenge will come when all of these features you're implementing have to play with each other. For example, you have to take great care with how you implement static members if you're also going to implement features like inheritance. Your transpiler could break if you're not careful with how it translates static members and how it translates inheritance together. They could step on each other's toes. This would probably be your greatest challenge writing this transpiler.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by Niya View Post
    Static members are naturally the next stage in the evolution of a system like VB6 modules. It combines the features of modules and classes into a single system. A VB6 module can be thought of as a class with only static members.
    Removing a feature (namely standard modules) is a devolution IMO and Java was the first weirdo which lacked global functions and then C# blindly copied the design with no second thought being fancy and "completely OOP" by design.

    C/C++ introduced classes but did not shed global functions -- this would have been ridiculous. They just introduced namespaces and resolved whatever issues one has with global scope pollution.

    cheers,
    </wqw>

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by Niya View Post
    However, the true challenge will come when all of these features you're implementing have to play with each other. For example, you have to take great care with how you implement static members if you're also going to implement features like inheritance. Your transpiler could break if you're not careful with how it translates static members and how it translates inheritance together. They could step on each other's toes. This would probably be your greatest challenge writing this transpiler.
    In terms of class inheritance, I haven't found a final solution yet. C# is just one of my reference objects, and I'm still looking for a more perfect solution, which is to implement class inheritance in a simpler way without increasing the complexity of the scripting language.

    Not sure how Olaf handles class inheritance.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by wqweto View Post
    Removing a feature (namely standard modules) is a devolution IMO and Java was the first weirdo which lacked global functions and then C# blindly copied the design with no second thought being fancy and "completely OOP" by design.
    Which language do you think does this better than C# and Java?

    Quote Originally Posted by wqweto View Post
    C/C++ introduced classes but did not shed global functions -- this would have been ridiculous. They just introduced namespaces and resolved whatever issues one has with global scope pollution.
    Does Rust or Golang do better than C/C++?

  14. #14
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)

    Quote Originally Posted by wqweto View Post
    Removing a feature (namely standard modules) is a devolution IMO and Java was the first weirdo which lacked global functions and then C# blindly copied the design with no second thought being fancy and "completely OOP" by design.

    C/C++ introduced classes but did not shed global functions -- this would have been ridiculous. They just introduced namespaces and resolved whatever issues one has with global scope pollution.

    cheers,
    </wqw>
    C++ introduced classes but still needed to appeal to the existing C developers, removing global functions would have been a complete blocker to just about any attempt to port code from C to C++ (one of the selling points was you could treat C++ as a "better" C without needing to use any OO features).

    C# didn't need to have the same constraints regarding backwards compatibility and not allowing global functions was a deliberate design decision. Associating functions with a type (and a namespace) is a great way of avoiding name clashes or ambiguity in name resolution.

    When typing code such as Math.Sin I have never really felt that it was ridiculous, qualifying functions like that seems perfectly natural. If it is an issue then you could always do something like
    Code:
    using static System.Math;
    
    var x = Sin(100);
    to avoid qualifying the methods.

  15. #15
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Sourc

    The world is moving forward from OOP "purist" design decisions like removing global functions and retrofitting these as static methods on a class.

    So now top-level statements in C# try to remedy poor choices in the past with syntactic sugar.

    Namespaces have proven to be good language construct, "let's pretend everything is class" is not ok IMO.

    cheers,
    </wqw>

  16. #16
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Sourc

    Quote Originally Posted by wqweto View Post
    The world is moving forward from OOP "purist" design decisions like removing global functions and retrofitting these as static methods on a class.
    These have been a dotnet concept since the original version, so not entirely sure how "retrofitting" describes something that has always been present in the language and was a deliberate design decision. Static classes in C# did only arrive in C# 2.0 but that is only a convenience for enforcing a class to contain only static members.

    Quote Originally Posted by wqweto View Post
    So now top-level statements in C# try to remedy poor choices in the past with syntactic sugar.
    Top level statements definitely reduce the amount of boilerplate code when doing something simple, can't argue with that.

    Quote Originally Posted by wqweto View Post
    Namespaces have proven to be good language construct, "let's pretend everything is class" is not ok IMO.
    Namespace are definitely a good idea, can't argue with that. Not sure what is wrong with an OO language treating everything as an object / requiring functionality to be associated with a type though. It just provides a way of grouping related functionality together, or associating functionality with a type.

    Perhaps I have just spent a lot of time with C# and it seems a natural way of doing things, I certainly prefer the idea of static functions associated with a type in preference to global functions in a global namespace. At least having a type often makes it easier to find appropriate functionality due to logical grouping.

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Sourc

    With regards to this particular topic, VB6 and C# are actually almost the same. VB6 having a global namespace is really just an illusion created by the compiler fully qualifying module function calls for you. When you make a call like MyFunc the compiler turns it into MyModule.MyFunc. The only true difference between modules in VB6 and classes with static members in C# is that the classes in C# could also be made to hold instance data. Like I said before, classes in .Net are like VB6 modules and class modules combined into a single entity, which to me makes perfect sense. When you want module functionality, you make the member static and if you want object functionality, use non-static members. The beauty about this system is that you can easily group related functionality that have both static and non-static functionality into the same logical unit. The MD5 and SHA256 classes in .Net are examples of this. You use static members of the SHA256/MD5 class to create SHA256/MD5 objects.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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