tags:

views:

66

answers:

3

Hello, I am currently in the process of converting some excel VBA to C# and I am stuck on one thing.

In VBA I have some public variables declared at the top of the module, outside of the Functions and I can access and modify these from any module or function. Example:

Module A:

    public A As Double
    public B As Double

    Public Function Add (double a, double b)

Dim C As Double

C = A+B

End Function

Module B:

Public E As Double
Public Function Subtract(double D)

E = D - Add.C

End Function

Can anyone help me to replicate this in C# ?

A: 

You can't have global variables in C# to the best of my knowledge, it's bad practice regardless. Look to restructure your functions to avoid the need for global variables by passing all necessary data into the function as parameters.

Lazarus
A: 

The closest thing to a module in C# would be a public static class which could then contain public static member variables. Though as Lazarus says, it would be better to refactor your code if possible.

Look at this MSDN page for more info and samples: Static Classes and Static Class Members (C# Programming Guide)

ho1
+2  A: 

I note that your code wouldn't compile in VBA, and isn't even logical. So my sample below is a guess... You really have to take more care when asking questions here.

NB: .NET being a modern language (unlike VBA), it is object oriented to the core and doesn't include the concept of global variables of functions, like are so often used in VBA programming. This goes for C# as for VB.NET.

In .NET all your code is is packaged into classes (OK, structs and enums too), as opposed to modules.

However, VBA Standard Modules will translate to static classes in .NET. So, to get yourself started, you can do that. But on the whole it is bad design. Or rather, a good way to design bugs.

With this caveat, you can do something along the lines of:

namespace MyMultivaluedUniverse
{
    // As these classes are static, all methods and properties need to be static too.
    public static class MyUniverse 
    {
        public static double A { get; set; } // shortcut notation for properties .NET 2+
        public static double B { get; set; }

        public static double Add() // If you are just adding your Properties A and B, no need for this method to have parameters
        {
            return A + B;
        }
    }

    public static class MyWorld
    {
        public static double E { get; set; }
        public static double Subtract(double D)
        {
            E = D - MyUniverse.Add(); // Note how I specify the class name before the method:  this works only for static methods
            return E;
        }
    }

    // This class is not static, so doesn't need static everywhere
    public class MyHome
    {
        public double Arithmetic(double F)
        {
            return MyUniverse.Add() - MyWorld.Subtract(F); // will trivially equal F
        }
    }

    public class MySelf
    {
        MyHome mh = new MyHome(); // shows how to instantiate a non static class and use it
        public double DoWork(double G)
        {
            return mh.Arithmetic(G); // I precede the method name with the Instance name.
        }
    }
}
awrigley