Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Equivalent for VB 'module'

In Visual Basic, you can use a module as a place to store 'loose' code which can be methods and variables that are accessible from elsewhere in the application without having to first initialize something, and the variable states can be set or changed and will continue to keep that value throughout.

The closest I have found, is static methods in C# as part of a public class, however this has the drawback of variables which are not globally accessible, or internally settable/gettable if the variables are made static.

Take for example the following simple code in VB stored in a blank module.

Private iCount as Integer = 0

Public Sub Increment()
  iCount = iCount + 1
End Sub

Public CheckModulus() As Boolean
  If iCount % 6 == 0 Then
    Return True
  Else
    Return False
  End If
End Sub

Now, you have a class, and from that class, you can then call CheckModulus() as such

Public Class Fruits

   Public Static Function ExactBunches() As String
      If CheckModulus() Then
         Return "You have an exact amount of bunches"
      Else
         Return "You need more fruits to make a bunch"
      End If
   End Function

End Class

Now I realize with some hack and slash, that you could move iCount to 'settings', and reset it on application launch, etc, but please bear in mind this is a very simple example to illustrate the convenience of being able to have a set of global code. Where I have found this most useful in the past is when creating UserControls or custom classes. In addition, the intent is not to make everything globally accessable, but to have certain methods and variables globally accessable while others remain ONLY accessible from within the module. For example, while CheckModulus() and Increment() (global methods) both have access to modify and obtain the iCount value, iCount is not accessible globally, as would the way be with private defined methods in the module.

So the big pickle is this :

What is the functionally equivalent code type in C# to VB & VB.NET's module ?

Due to the complex nature of this simple question, I feel I should impose a boolean for a 'just in case there is no answer' answer as follows.

If, there is nothing functionally equivalent, then what sort of clever hack or workaround (aside from using settings, or external storage like the registry, database, files, etc), to make this happen or something VERY very close ?

like image 384
Kraang Prime Avatar asked Dec 05 '22 03:12

Kraang Prime


1 Answers

You can use a static class. You can also initialise these using a static constructor.

public static class MyStuff
{
    //A property
    public static string SomeVariable { get; set; }

    public static List<string> SomeListOfStuff { get; set; }
    //Init your variables in here:
    static MyStuff()
    {
        SomeVariable = "blah";
        SomeListOfStuff = new List<string>();
    }

    public static async Task<string> DoAThing()
    {
        //Do your async stuff in here
    }

}

And access it like this:

MyStuff.SomeVariable = "hello";
MyStuff.SomeListOfStuff.Add("another item for the list");
like image 87
DavidG Avatar answered Dec 23 '22 13:12

DavidG