-1

Ho do you get the current instance of a class?

The class have search method and cancel method.

Code example:

if (btnSearch.Text =="Search")
{
    Searcher srch = new Searcher();
    srch.Search();
    btnSearch.Text = "Cancel";
    return;
}
if (btnSearch.Text == "Cancel")
{
    //call a method in the instance above for example
   srch.Cancel();
}

I want to create the instance only when btnSearch.Text =="Search"; and when btnSearch.Text =="Cancel"; i want to call srch.Cancel();

//// Thanks to nmclean, problem solved, makes sense i had to declare the Search class at a higher scope to be able to access the current running instance.

NetInfo
  • 587
  • 2
  • 6
  • 19
  • 3
    What is meant by "the instance above"? An instance of `Searcher`? – O. R. Mapper Jan 27 '14 at 13:39
  • 1
    Do you want to call method of `srch` from above scope? – Hamlet Hakobyan Jan 27 '14 at 13:39
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 27 '14 at 13:39
  • 2
    Please define "the current instance of a class".. You mean a singleton, or service locator, or...? – Yves Schelpe Jan 27 '14 at 13:39
  • 1
    Store `srch` somewhere – L.B Jan 27 '14 at 13:40
  • 2
    There's no such thing as "the current instance". What if there were more than one instance? What if there were multiple threads? – John Saunders Jan 27 '14 at 13:40
  • The thing is, i only want to create the instance when btnSearch.Text =="Search"; since that class is doing things in the background, so its pretty much a different thread. – NetInfo Jan 27 '14 at 13:49
  • 1
    @NetInfo: Frankly, I do not understand what you are asking. If you only create the instance if `btnSearch.Text == "Search"` (which is what the above code is doing), then how do you expect that instance to be around if that condition *does not* apply? Also, while you have implied it, you still have not responded to our questions *what instance* (of which class?) you are actually referring to. – O. R. Mapper Jan 27 '14 at 13:58
  • 2
    I also wouldn't depend on the text of the button. Instead, keep a "search-in-progress" flag around. You could also follow the suggestions below for raising the scope of `srch`, and use `srch != null` as your "search-in-progress" flag. – John Saunders Jan 27 '14 at 14:08
  • Is this code inside of an event handler for `btnSearch_Click` or something? If so, you should show that. – John Saunders Jan 27 '14 at 14:29

1 Answers1

7

Your srch variable must be declared at a higher scope than the function, otherwise it will not persist to the next time the function is called. Most likely this means it should be a field of the class:

class YourClass
{
    private Searcher srch;

    void YourMethod()
    {
        if (btnSearch.Text == "Search")
        {
            srch = new Searcher();
            srch.Search();
            btnSearch.Text = "Cancel";
            return;
        }
        if (btnSearch.Text == "Cancel")
        {
            srch.Cancel();
        }
    }
}
nmclean
  • 7,564
  • 2
  • 28
  • 37
  • After lots of debugging and trials, this worked like a charm. thanks a lot, sorry if there were any confusion. – NetInfo Jan 27 '14 at 16:22