Daniel
Daniel

Reputation: 7724

C# cannot find static method inside public method of another class

I have a static class TV:

namespace nTV {
    public static class TV {

        public static bool isOn;

        public static void SetOn(){
            //do stuff to set attribute isOn
        }
    }
}

and I have the class House:

namespace nHouse{
    public class House{

        public void CheckTV(){
            TV.SetOn();
        }

    }
}

When I call TV.SetOn() (or even TV.isOn) from inside House, it says

TV does not contain a definition for SetOn.

What am I doing wrong? How can I find TV's methods inside another class?

Upvotes: 5

Views: 2062

Answers (3)

ispiro
ispiro

Reputation: 27673

Edited:

I think you have 2 TV classes, one in nHouse and one in nTV.

Original answer:

Either as Vulpex suggested, or

add using nTV; to the file.

The issue is that the full name of TV is nTV.TV, but when called from within the same namespace, you can shorten it. So you either

  • use the full name
  • use it from within the same namespace (obviously not possible here)
  • or add a using statement as I mentioned.

EDIT

It looks like your problem arises from the class being static. You therefore (according to https://stackoverflow.com/a/28123407/939213) need to use the following syntax:

using static nTV;

EDIT 2

I've tested my 1st edit solution (using static) and seen that a) there is no need for that and b) it doesn't even work (I get an error in Visual Studio). Here is my code which works fine. The files are in separate directories and I put another namespace with a class before each (to test whether that might throw VS off. It didn't.)

namespace calledNS
{
    public static class CalledClass
    {
        public static void CalledMethod() { }
    }
}

and

using calledNS;
...
namespace callerNS
{
    public class CallerClass
    {
        public void CallerMethod() { CalledClass.CalledMethod(); }
    }
}

So my only idea (which is actually the most fitting to your errors) is that:

You have 2 TV classes, one in nHouse and one in nTV.

This would explain all of the behavior you're seeing, I think.

Upvotes: 2

Vulpex
Vulpex

Reputation: 1083

Use the full name. You need to tell it from what namespace.

namespace nHouse
{
    public class House
    {
        public void CheckTV()
        {
            nTV.TV.SetOn();
        } 
    } 
}

OR

using nTV;

namespace nHouse
{
    public class House
    {
        public void CheckTV()
        {
            TV.SetOn();
        } 
    } 
}

you have to tell the compiler in what namespace to look for the method you're trying to call. Unless you give the fullname nTV.TV.SetOn() you will have to name at the beginning of the file to use the namespace with using nTV;

working cs Fiddle

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

The namespaces of the classes are different (nTV and nHouse), that's why you should mention nTV as using nTV;

 using nTV;

 ...   

 namespace nHouse {
   public class House{
     public void CheckTV(){
       TV.SetOn();
     }
   }

   ...
 }

If you want address nTV.TV by its short name TV. Another possibility is to declare both classes in the same namespace:

 namespace Houses {
   public static class TV {
     public static bool isOn;

     public static void SetOn(){
       //do stuff to set attribute isOn
     }
   }
 }

 ...

 namespace Houses {
   public class House {
     public void CheckTV() {
       TV.SetOn();
     }
   }
 }

Upvotes: 1

Related Questions