Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In place of "Is this a bad design"
I might have asked what are the pitfalls down the road?
Coming from VB.Net I discovered how to use the Module
It made remembering variable names great SO
Much to my dismay C# which I am still learning and plying with
has no Module control.
Here is the code I cooked up when I discovered "const"
I declare the const on a form called frmDataModule and
I use the const on a frmStart
YES I know I can declare const as top level variables on forms
but then I need to include the from name if I use it on another form.
So is this design idea a BAD idea that will bite me down the road?
To construct the code it needs to be public ?

What I have tried:

Here is my declaration code
public partial class DataModule : Form
{
    public DataModule()
    {
        InitializeComponent();
    }
}
public static class GV
{
    public const string dbName = "Friends.db";
}


Here is my usage on frmStart
public void MyMethod()
{
    string textToAdd = GV.dbName;
Posted
Updated 14-Jan-23 18:32pm
Comments
PIEBALDconsult 14-Jan-23 23:43pm    
Static classes are pretty much the same as modules, sorta.
A const is static, but there are limitations on what can be made const.
So also look at static readonly members.
CPallini 15-Jan-23 4:12am    
Don't do that, use (as already suggested) static classes instead.
Mohammad Reza Valadkhani 17-Jan-23 2:50am    
I recommend you to read again Design patterns, it will help to understand what solution would be appropriate for your code, I look at this question in my own way that you need static parameter to be accessible from somewhere, your approach is work, but in OO we have to consider many things to have high level class code structure

1 solution

VB.NET inherits a lot of stuff from the previous incarnation of VB (VB 1 to 6 inclusive) which added Module when it introduced classes to the even older Basic language. The older language was created for a procedural, console based environment where the app was in control of what the user did unlike the Windows GUI based approach where the app responded to what the user did.

C# was designed from the ground up to be an OOPs oriented, GUI based language, so it deliberately has no concept of global variables.

Static classes are one way to "get round" that since there is only ever one instance of a static variable.

Const values - and VB.NET has them as well - allow you to define values which are known at compile time and name them to make your code both more reliable, easier to modify, and easier to read. Unlike VB, C# does not require them to be declared in a static class - so you can write a class to handle DataBase access and include a const string that is the connection string as well as non-static code to SELECT rows from a table. (But it's a poor idea as that should be in an external data file).
In that way, it's a lot more flexible than the outdated VB Module.

Static classes are a very handy tool, but you do need to be careful as they can cause problems if they aren't used carefully. Have a read here: Static Classes and Methods—Are They Terrible?[^] and also here: The Lounge - Unit tests[^] where a problem lurked in the background for years because a he forgot to clear a buffer, making the static method stateful instead of stateless.
 
Share this answer
 
Comments
CPallini 15-Jan-23 4:11am    
5.
BillWoodruff 15-Jan-23 9:09am    
I am tempted to post an example based on the fact that inheriting classes can access 'const fields in the class they inherit from, but, I think in this case, that's not what the OP needs, and, would be a distaction: what do you think ?
OriginalGriff 15-Jan-23 10:04am    
I suspect it would be a distraction - I'm not sure that the OP has really grasped classes and OOPs yet, even in VB. You know how easy it is to write "procedural code with a class, honest" in either language! :laugh:
BillWoodruff 15-Jan-23 10:52am    
Yes, thanks for your time ! imho, the compiler should break on duplicate names in an inheriting class ... by default. I am surprised ReSharper didn't flag this. Is it possible a new update of VS has affected ReSharper settings: I'll ask JetBrains.
PIEBALDconsult 15-Jan-23 10:49am    
Hey now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900