-
May 14th, 2022, 11:08 PM
#1
Thread Starter
Frenzied Member
[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.
-
May 14th, 2022, 11:19 PM
#2
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Should be as simple as just not making them public.
-
May 14th, 2022, 11:31 PM
#3
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by DataMiser
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.
-
May 15th, 2022, 12:40 AM
#4
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.
-
May 15th, 2022, 01:58 AM
#5
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by fafalone
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.
-
May 15th, 2022, 09:25 AM
#6
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#.
-
May 15th, 2022, 11:05 AM
#7
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by SearchingDataOnly
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?
-
May 16th, 2022, 01:24 AM
#8
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by Niya
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.
-
May 16th, 2022, 01:26 AM
#9
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by fafalone
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.
-
May 17th, 2022, 04:36 PM
#10
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by SearchingDataOnly
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.
Originally Posted by SearchingDataOnly
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.
-
May 18th, 2022, 03:27 AM
#11
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by Niya
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>
-
May 18th, 2022, 05:13 AM
#12
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by Niya
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.
-
May 18th, 2022, 05:18 AM
#13
Thread Starter
Frenzied Member
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by wqweto
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?
Originally Posted by wqweto
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++?
-
May 18th, 2022, 09:42 AM
#14
Re: Simulate C# "private static" in VB6 (Organization of VB6 Source Code)
Originally Posted by wqweto
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.
-
May 18th, 2022, 10:44 AM
#15
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>
-
May 18th, 2022, 12:13 PM
#16
Re: [RESOLVED] Simulate C# "private static" in VB6 (Organization of VB6 Sourc
Originally Posted by wqweto
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.
Originally Posted by wqweto
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.
Originally Posted by wqweto
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.
-
May 18th, 2022, 04:19 PM
#17
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.
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
|