Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a static class(CL), one main class(Program) and a simple class(cl1).

I static class i have a method called disply().
When i am trying to access this method in cl1, i can very well able to do it...However when i tried to access it in class cl1, it does not shows it. All the three classes are in one namespace.

Please see the code and help me...

class Program
  {
      static void Main(string[] args)
      {
          cl.display();    //works !!
      }
  }

  public static class cl
  {
      public static string display()
      {
          return "hi";
      }
  }


  public class cl1
  {
           cl.display();    //DOES NOT WORKS..display function is not  //available....why !!
  }
Posted

Unless you have improperly pasted your code, the issue is that you are trying to call a method outside of a method definition in your public class cl1. Here is the way it should look:

C#
public class cl1
{
   public string CallDisplay()
   {
      return cl.display();
   }
}
 
Share this answer
 
v2
Comments
Sander Rossel 15-Jun-12 14:22pm    
You were just a little faster than me. Have a 5 :)
Tim Corey 15-Jun-12 14:25pm    
Thanks. By the way, that pushed me into the Gold level of the Authority category. Thanks for that. :-)
Sander Rossel 15-Jun-12 14:28pm    
Congrats! Getting into new categories is always fun :)
Manas Bhardwaj 15-Jun-12 17:17pm    
Correct +5!
the cl.display(); in cl1 is not inside a method body.
cl.Display(); in your Program class works because it is part of the Main method.
You should change cl1 so it looks something like this:
C#
public class cl1
{
    public void SomeMethod()
    {
        cl.display(); // Now works fine :)
    }
}
 
Share this answer
 
Comments
Tim Corey 15-Jun-12 14:25pm    
Good answer. :-)
Sander Rossel 15-Jun-12 14:28pm    
Thanks. It looks a lot like yours. And I mean A LOT ;)
Tim Corey 15-Jun-12 14:29pm    
Great minds, right?
Sander Rossel 15-Jun-12 14:33pm    
Yep :)
Manas Bhardwaj 15-Jun-12 17:18pm    
Correct +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900