Accessible class across C#

Asked

Viewed 94 times

0

I would like a class that is not static to be available for the entire application. At first I thought about turning this class into static, but for obscure reasons that are beside the point, I can’t make it static.

So I researched something about and found some things, follow 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;
}

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

How to pass the value instead of the reference?

There is how to do this in a "better way" ?

EDIT

Thank you all for the clarifications, I have reached a "solution". Creating a clone of the original class it can be changed that did not reflect in the clone, it solves for now my problem, I will 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; }
}
  • An available class or instance of that class?

  • Could you explain further what you mean when you say "available for the entire application"? A public class is now available.

  • But this is a static class ;) The problem is this obscure motif, especially if you can’t use anything static, but the solution is to use something static :) The best way is to make a static class soon. You cannot store a value in something that is a reference. Perhaps a more concrete example will help demonstrate what you want.

  • @Totallyuncool, a class instance.

  • @bigown, it is precisely this obscure motive that disturbs.

  • I closed because with the edition the question became completely meaningless, if that’s what had been asked originally and answered was not what I really wanted to know. If you make the question clearer you can reopen it. The fact that you have an obscure reason also contributes.

  • Singleton is the answer to your problem

  • @Totallyuncool according to his edition, no :)

  • @Bigown is, I was wrong by the way s

Show 4 more comments

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();
    }
}

1

Complementing the @maiconmm response you can do this startup in Program.Cs if you are a Winforms or Application console project.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.