Mac N.
Mac N.

Reputation: 51

How to call a Class = new Class(); declare one time?

In windows Form App I get call

  private Class1 c1;
  private void Print()
  {
            c1 = new Class1();
  }

but in next one i don't want to declare new Class because it make a wrong value for my program and get error. it's like

  private void Print2()
  {
            c1 ; //**( I don't want to declare new one )**
  }

edit 2 i'd like to do like you said but argument in my code is like

    public CandleCollection GetCandleCollection()
    {
        CandleCollection collection = null;
        try
        {
            collection = SymbolList[cbxSymbol.Text];
        }
        catch (Exception) { }
        return collection;
    }       
    private Class1 c1 = new Class1(<collection>);  **it's need to call collection**
    private void Print()
    {
            c1 = new Class1();
    }
    private void Print2()
    {
            c1 ; //**( I don't want to declare new one )**
    }

edit 3 This is my original code to call c1

    private void Print()
    {
        CandleCollection collection = GetCandleCollection();
        Class1 c1 = new Class1(collection);
    }

Upvotes: 0

Views: 115

Answers (1)

Koby Douek
Koby Douek

Reputation: 16693

Move your CandleCollection variable out of the public method so you can also use it out of this method.

Then, you can instantiate the Class1 variable only once:

private CandleCollection collection = null;

public CandleCollection GetCandleCollection()
{
    try
    {
        collection = SymbolList[cbxSymbol.Text];
    }
    return collection;
}       
private Class1 c1 = new Class1(collection);
private void Print()
{
        c1;
}
private void Print2()
{
        c1;
}

Upvotes: 1

Related Questions