How to invoke a public static int variable in c #

0

I am doing a questionnaire, and I need to add the variables of several forms. First I declare them as public static int , and I call them from a form that executes a graph. This is part of the code that I use to invoke my variables. But at the time of drawing the graph I do not get anything

int economico = Testya.eco1;
int salud = Testya1.salud;
int sociales = Testya2.sociales;
int exactas = Testya3.exactas;
int biologicas = Testya4.biologicas;
int arte = Testya5.arte;

string[] series = { "Ciencias Econòmicas-Administrativas", "Ciencias Sociales y Humanidades",
    "Ciencias Exactas e Ingenierìas", "Ciencias Biològicas Agropecuarias", "Arte, Arquitectura y Diseño", "Ciencias de la Salud" };
int[] puntos = { economico, sociales, exactas, biologicas, arte, salud};
chart1.Palette = ChartColorPalette.Pastel;
chart1.Titles.Add("Resultados");
for (int i =0;  i>series.Length; i++)
{
    Series serie = chart1.Series.Add(series[i]);
    serie.Label = puntos[i].ToString();
    serie.Points.Add(puntos[i]);
    
asked by Point Vocational 25.05.2017 в 03:44
source

2 answers

0

and why do not you use better propiedades ?

just like you are doing with static public variables, you can create some properties like this:

public class Testya
{
  // Un atributo privado
  private int economico = 0;

  // Oculto mediante una "propiedad"
  public int economico
  {
    get
    {
      return economico;
    }

    set
    {
      economico= value;
    }
  }
}

Then from the other form you just have to call them like this:

Testya ejemplo = new Testya();

//asi le asignas valores
ejemplo.economico= 6;

//asi lo puedes leer/mostrar
Console.WriteLine(ejemplo.economico);
    
answered by 25.05.2017 в 13:20
0

You can declare a static public variable in the Program class in the following way:


    namespace WindowsFormsApplication1
    {
        static class Program
        {
            public static int variable1 = 0;
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }

And you can send it to call from any form of the program in the following way:


    Program.variable1 = 10;

You can also declare it in any class and make reference to the class without having to instantiate it:


    public class Clase1
    {
        public static int variable2 = 0;
    }

And send her call like this:


    Clase1.variable2 = 10;

Note: I think it's the answer to your question, however the use of static variables is generally not a recommended practice, here I leave a link to a discussion about it: Why are static variables considered evil

    
answered by 25.05.2017 в 19:01