Click here to Skip to main content
15,942,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all that read this. I know the difference between a variable and a struc. What I reall want to know are the pros and cons with the examples I have given.

Using Global variables:

const string _maintTypeCode_Change = '001';
const string _maintTypeCode_Addition = '021';
const string _maintTypeCode_Termination = '024';
const string _maintTypeCode_Reinstatement = '025';


Using Struc:

struct MaintenanceTypeCode
public const string Change = '001';
public const string Addition = '021';
public const string Termination = '024';
public const string Reinstatement = '025';
Updated 13-Apr-10 14:29pm

A struct IS a variable. It's just of a type that you define. The end result is the same. Globals are usually a bad idea, but are unavoidable at times.
Share this answer
C# does not support global variables. You'd have to toss those into a struct or a class. However, assuming your "global variables" are actually in a class and your struct is actually instantiated and assigned to a property on a class, the advantage to using a struct would be that you can group your variables logically and pass them around easily. Imagine if all the variables, constants, functions, and so on in the .Net Framework were all global rather than nicely nested in namespaces, classes, and structs. You'd end up with something horrible and impossible to navigate through (e.g., VB6).

[edited] Actually, if you use a struct, a struct is passed by value unless you use the "ref" keyword everywhere. What this means is that if you use any mutable (changeable) members and change the value, the original structure is not updated when passed as a parameter (without "ref") or defined as a property of a class. For example,

internal static class Preferences
  // Nested struct.
  public struct Globals
    public string Option1 = "foo";
    public string Option2 = "bar";

  private static Globals globals;
  public static Globals Globals { get { return globals; } }

If later you change the code like so: Preferences.Globals.Option1 = "baz" - the Globals structure is not changed.

Just best to keep these as members of a class. If constants are all you use, just define them as constants of a class. If you need mutable options, use a singleton class. More information about good singleton patterns can be found here on Code Project or on MSDN[^].

But there's also an entire set of APIs for managing settings like this in the .NET Framework that Visual Studio exposes through the project properties / settings tab. It has the added benefit of being able to merge different user contexts if so desired as well as designer support.
Share this answer
SpeedBump26 wrote:
want to know are the pros and cons

some pros of using a struct instead of individual variables;
1) easier to pass to methods as a set (just pass a pointer/ref to the struct)
2) logical grouping of related variables

But from your code it looks like each variable is a discreet value for a certain type. If so, consider using an enumeration. If not, ignore this!
Share this answer

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