I have a traffic jam in my C # WPF Program

0

Good day everyone, I became the user because I have a very big question and I can not continue without solving it.

I'm trying to do the program in different files ".cs" of classes but this is bringing me complications when it comes to wanting to call the functions and modify the variables from one to another.

I have the Configuration class in the Configuration.cs file:

namespace WpfApplication1 
{
public class Configuracion
{
    private string directorio;       
    bool etapa;                 // TRUE es etapa Individual, FALSE es etapa grupal

    public bool confOrden;          //TRUE se ejecuta en azar - FALSE Elección de Orden

    private bool tlimite;       //TRUE tiene un limite de tiempo - FALSE no limitado por tiempo
    private int valorlimite;    // Valor del limite de tiempo

    private int repeticiones;   //Cantidad de Repeticiones x Figura
    private int cantFig;        //Cantidad de Figuras

    int[] orden;                 // Cuando se defina repeticiones y cant Fig debemos hacer    ensayo_suj = new int[repeticiones*cantFig];

    public Configuracion()
           {
                directorio = @"\\CINTRA-M029\DatosBuscFiguras";

                etapa = true;    
                tlimite = true; valorlimite = 60;
                confOrden = false;

                repeticiones = 4;
                cantFig = 4;
           }


    public void guardaconf1()
    {
        //ClaseFigura[] fig = new ClaseFigura[/*cantfig*/];

    }

    public string MuestraDirectorio()
        {
            return directorio;
        }

    public void EditaDirectorio(string a)
    {
        directorio = a;
    }

    public void EditaOrden(bool a)
    {
        confOrden = a;
    }

}

And a configuration window called VentConfiguracion.cs, I'll just put the most important thing and not the whole class:

namespace WpfApplication1
{

public partial class VentConfiguracion : Window
{
    private Configuracion config;
    private Sujeto s1, s2;
    private EdicionFiguras Edicion;
    private ClaseFigura[] Fig;

    StackPanel[] Miniaturas = new StackPanel[8];

    public VentConfiguracion(ref Configuracion c, ref Sujeto clases1, ref Sujeto clases2, ref EdicionFiguras ed, ref ClaseFigura[] f)
    {
        InitializeComponent();
        config=c;
        s1 = clases1; s2 = clases2;
        LabelDirectorio.Content = config.MuestraDirectorio();
        existenciaDirectorio();
        Miniaturas = new StackPanel[] { Mini1, Mini2, Mini3, Mini4, Mini5, Mini6, Mini7, Mini8 };
        Edicion = ed;
        Fig = f;

        for (int i = 0; i < 8; i++ )
            Miniaturas[i].Children.Add(Fig[i].dibujoMini);      // Imprime todos los mini poligonos
    }

 private void radioLista_Checked(object sender, RoutedEventArgs e)           //Seleccionado En ORDEN LISTA
    {
        config.EditaOrden(false);
    }

It is worth mentioning that in the MainWindows I passed by reference an instance of the Configuration class in the VentConfiguration class in order to modify the variables saved in configuration. This is a part of the main:

     public partial class MainWindow : Window
    {

       public MainWindow()
    {
        InitializeComponent();

        Configuracion Config = new Configuracion();
        Sujeto sujeto1 = new Sujeto();
        Sujeto sujeto2 = new Sujeto();

        ClaseFigura[] Figura = new ClaseFigura[8];
        CargaDatosFiguras(ref Figura);

        EdicionFiguras Edic = new EdicionFiguras(Figura);
        VentConfiguracion c = new VentConfiguracion(ref Config, ref sujeto1, ref sujeto2, ref Edic, ref Figura);



        this.Visibility = Visibility.Hidden;
        c.ShowDialog();

        ...

        }

The problem arises when I want to modify some configuration variable within the VentConfiguration class, for example with a RadioButton:

     private void radioLista_Checked(object sender, RoutedEventArgs e)           //Seleccionado En ORDEN LISTA
    {
        config.EditaOrden(false);
    }

When I'm going to run, I get the following error:

"Referencia a objeto no establecida como instancia de un objeto."

I understand the error, my problem is that I do not know how to solve it. Obviously I'm missing concepts I'm new to object-oriented programming, thank you very much.

    
asked by Gustavo Belbruno 02.01.2019 в 15:38
source

1 answer

0

I was able to solve the problem but I do not know if this solution is correct. When calling the constructor of the VentConfiguration class

  public VentConfiguracion(Configuracion c,Sujeto clases1, Sujeto clases2, EdicionFiguras ed, ClaseFigura[] f)
    {
        InitializeComponent();
        config = c;
        s1 = clases1; s2 = clases2;
        LabelDirectorio.Content = config.MuestraDirectorio();
        existenciaDirectorio();
        Miniaturas = new StackPanel[] { Mini1, Mini2, Mini3, Mini4, Mini5, Mini6, Mini7, Mini8 };
        Edicion = ed;
        Fig = f;

        for (int i = 0; i < 8; i++ )
            Miniaturas[i].Children.Add(Fig[i].dibujoMini);      // Imprime todos los mini poligonos
    }

We see that the components (InitializeComponent ()) of the class are initialized first, this calls all the functions that this class has including the functions that are using the Configuration class, which at that moment is null because it is given a value just in the next moment when we do config = c; So what I did was put the config assignment before the initialization of class components.

  public VentConfiguracion(Configuracion c,Sujeto clases1, Sujeto clases2, EdicionFiguras ed, ClaseFigura[] f)
    {
        config = c;
        InitializeComponent();
        s1 = clases1; s2 = clases2;
        LabelDirectorio.Content = config.MuestraDirectorio();
        existenciaDirectorio();
        Miniaturas = new StackPanel[] { Mini1, Mini2, Mini3, Mini4, Mini5, Mini6, Mini7, Mini8 };
        Edicion = ed;
        Fig = f;

        for (int i = 0; i < 8; i++ )
            Miniaturas[i].Children.Add(Fig[i].dibujoMini);      // Imprime todos los mini poligonos
    }

I really do not know if it's okay, it does not seem very elegant but it works because when the initiation goes to look for the config it already has a value.

    
answered by 02.01.2019 в 17:13