1

When trying to compile my c# windows app I am getting the following error:

The name 'GetActiveLB' does not exist in the current context

Here's the code that calls that function:

using F5LTMMaintenance;

......

    private void btnLBSetA_Click(object sender, EventArgs e)
    {

        List<string> range = GetActiveLB();
        foreach (string item in range)
        {
            // Do something with item
        }
    }

Then I have a class with the following:

namespace F5LTMMaintenance
{
    public class F5LTM<T>
    {
        public List<T> GetActiveLB()
        {
            var client = new RestClient("mylb.domain.local");
            var request = new RestRequest("mgmt/tm/cm/failover-status", Method.GET);
            var queryResult = client.Execute<List<T>>(request).Data;
            return queryResult == null ? new List<T>() : queryResult;
        }
    }
}

The GetActiveLB function does exist, its a public function so why am I getting this error? Any help would be appreciated.

weston
  • 54,145
  • 21
  • 145
  • 203
Brad
  • 1,979
  • 7
  • 35
  • 47

4 Answers4

2

Yes it's a public function but it's defined inside a different class than your calling event handler class. You need to create a instance of your class F5LTM<T> and on that instance call your method GetActiveLB() rather like

   private void btnLBSetA_Click(object sender, EventArgs e)
    {
        F5LTM<Type> test = new F5LTM<Type>();
        List<string> range = test.GetActiveLB();
Rahul
  • 76,197
  • 13
  • 71
  • 125
2

It has to be used with an instance of F5LTM<T>.

e.g.:

var f5ltm = new F5LTM<string>();
List<string> range = f5ltm.GetActiveLB();

Alternatively, if you declare it as static like this:

public class F5LTM //not generic here
{
    public static List<T> GetActiveLB<T>() //generic here and static
    {
         //unchanged
    }
}

Usage:

List<string> range = F5LTM.GetActiveLB<string>();

Or with C# 6 using static syntax:

using static F5LTMMaintenance.F5LTM; //at top of file

List<string> range = GetActiveLB<string>();

This is as close as you can get to the code you posted.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
  • Thank you - in hindsight this makes sense. I just couldn't see it. Thank you so much! – Brad Jan 03 '16 at 14:17
2

You will need an instance of your F5LTM class (say typF5LTM), to be able to call typF5LTM.GetActiveLB(). Or you need to make GetActiveLB a static function to be able to call it without an instance like F5LTM.GetActiveLB();

Grantly
  • 2,546
  • 2
  • 21
  • 31
1

As another poster pointed out, you have to call the method on the class.

F5LTM<string> listItems  = new F5LTM<string>();
List<string> range = listItems.GetActiveLB();
Hamish
  • 119
  • 7
  • So that the entire reasoning and solution are together in a single post. I suppose I could have simply given the code and made no reference to the cause of the issue, but that would be an incomplete and unhelpful post to make. Your answer was more thorough, though, and the post to which I referred (which just gave the reason without any illustrative code) has been either deleted or edited to include sample code. – Hamish Jan 03 '16 at 14:28
  • I see fair enough, yes there was another post without sample code around that time, but I actually thought you were referring to me, because I posted near identical code 3 minutes before you did, but it does sometimes get missed. – weston Jan 03 '16 at 14:36
  • Oh and welcome Hamish, I see this is your first day on the site! Didn't mean to be mean! – weston Jan 03 '16 at 15:02
  • No worries. I thought your answer was quite excellent, and I noted the style and thoroughness (I thought about mentioning static methods but decided against it). I will probably be around, as I became disabled last year so I am looking for productive stuff to do. Helping people seems like a good idea. – Hamish Jan 03 '16 at 15:04