Class accessible in every application C # [closed]

0

I would like a class that is not static to be available to the entire application. At first I thought of making this class static, but for obscure reasons that is not the case, I can not make it static.

Then I researched something and found some things, following example:

Considering the classes

 public class TesteInstancia
{
    public decimal A { get; set; }
    public string B { get; set; }
}

public static class TesteStatic
{
    public static TesteInstancia testeInstancia;
}

So I understand the class TesteStatic will get the reference to the object TesteInstancia .

How to pass the value instead of the reference?

Is there a way to do this in a "better" way?

EDIT

Thanks to everyone for the clarification, I came up with a "solution". Creating a clone of the original class can be changed that would not reflect on the clone, this solves my problem for now, I'll post here.

public class TesteInstancia : ICloneable
{
    public decimal A { get; set; }
    public string B { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

public static class TesteStatic 
{
    public static TesteInstancia MyProperty { get; set; }
}
    
asked by anonymous 17.01.2017 / 20:01

2 answers

1

Try a startup function:

public class TesteInstancia
{
    private decimal _a
    public static decimal A 
    { 
      get { Inicializar(); return _testeInstancia._a; } 
      set { Inicializar(); _testeInstancia._a = value; }  
    }

    private static TesteInstancia _testeInstancia;
    private void Inicializar()
    {
        if (_testeInstancia == null)
            _testeInstancia = new TesteInstancia();
    }
}
    
17.01.2017 / 20:29
1

Complementing the response from @maiconmm you can do this initialization in Program.cs if it is Winforms project or Application console.

You can also read about the Singleton standard to better understand the maiconmm code and unique instances of a class.

    
17.01.2017 / 20:40