-2

The title may sound a bit confuse, but what I want to do is basicaly create a class, declare a public int with the default value of 0, then inside the Program class I permanently change the value of this variable in a function, and if I print this value using another function, it will print the value set in the first function. For example:

using System;
{

    class Global
    {
       public int variable = 0;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Global test = new Global();
            test.variable = 10;
            //it makes no sense in this code to use another function, 
            //but in my other project it does
            Function();
            Console.ReadLine();
        }
        
        static void Function()
        {
            Global test = new Global();
            //should print 10
            Console.WriteLine(test.variable);
        }
        
    }
    
}
  • Just change the method to `void Function(Global instance)` and call it using `test.variable = 10; Function(test);` and remove `Global test = new Global();` from this method. –  Aug 08 '21 at 20:15
  • With you current sample make variable static `public static int variable = 0;` – Fabio Aug 08 '21 at 20:16
  • 1
    You are creating a ***new*** global instance using `new`, which means it starts at zed again. **Where** you declare a variable determines its scope! Suggested reading: **[Scope of Variables in C#](https://www.geeksforgeeks.org/scope-of-variables-in-c-sharp/)** also **[new operator (C# reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/new-operator)** – Ňɏssa Pøngjǣrdenlarp Aug 08 '21 at 20:17
  • Also rename Global coz not suitable to *WhatIsTheGoalOfMyClass*: [Naming Convention in c#](https://stackoverflow.com/questions/1618316/naming-convention-in-c-sharp) • [C# Naming Conventions](https://www.c-sharpcorner.com/UploadFile/8a67c0/C-Sharp-coding-standards-and-naming-conventions/) • [C# Coding Standards and Naming Conventions](https://github.com/ktaranov/naming-convention/blob/master/C%23%20Coding%20Standards%20and%20Naming%20Conventions.md) • [C# Coding Conventions (C# Programming Guide)](https://docs.microsoft.com/dotnet/csharp/programming-guide/inside-a-program/coding-conventions) –  Aug 08 '21 at 20:20
  • Indeed; you've caused this confusion for yourself by calling the class Global when it really isn't global at all – Caius Jard Aug 08 '21 at 20:23
  • 1
    The context of what's in your other project will probably give a better idea of what you are trying to achieve. Could you post that? The example you've given here doesn't make sense, and I suspect you might be asking the wrong question. With better context, you'll get better answers. – Bryan Aug 11 '21 at 14:24

1 Answers1

1

You could create a static class if you don't want to bother with injection like this:

public static class Global
{
    public int variable = 0;
}
class Program
{
    static void Main(string[] args)
    {
        Global.variable = 10;
    }
    static void Function()
    {
        Console.WriteLine(Global.variable);
    }
}

or you could just inject the class through as a parameter from wherever you call it.

public class Global
{
    public int variable = 0;
}
class Program
{
    static void Main(string[] args)
    {
        var test = new Global();
        test.variable = 10;
    }
    static void Function(Global global)
    {
        Console.WriteLine(global.variable);
    }
}

If you want to permanently change this variable inside every class you could use a static class, but then you wouldn't be able to create instances of it (if you wanted other variables to be non-static.

I would recommend looking into IServiceProviders because they can be really useful if the Function() method is inside another class and you want to pass through the Global class.

jallmen
  • 136
  • 1
  • 7
  • Sorry for the question that might seem too obvious to you, but it's because I'm new to programming. Now how do I call the "Function" function in static void Main in the second case? – dino_monkey Aug 08 '21 at 21:00
  • As long as they are both static you should just be able to put Function(); If you added a parameter just add that in the parenthesis. – jallmen Aug 09 '21 at 21:08