Click here to Skip to main content
15,880,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to call a method which is define inside this type of the code. need to call method a demo present inside the this code
C#
private class MapPanel : Control
{
   public void demo()
   {
      // Code here
   }
}

i am not able to get by simply creating object of the Class.
Posted
Updated 2-Jul-14 19:44pm
v2
Comments
Awadhendra Tripathi 3-Jul-14 1:06am    
Are you creating a user control?
g@urav123 3-Jul-14 1:15am    
it is control class which is inherited and created and user define control
Member 10579673 3-Jul-14 1:08am    
are you associating method call with any Control?like button or something?
g@urav123 3-Jul-14 1:14am    
no no its is method that i need to call out this class
MuhammadUSman1 3-Jul-14 2:29am    
Create object of this class and Access method with object name.

I have created class like this and called with in the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace ConsoleApplication3
{
class MapPanelOuter
{
public MapPanelOuter()
{
MapPanel mp = new MapPanel();
mp.demo();
}
 
private class MapPanel : Control
{
public void demo()
{
Console.WriteLine("Called MapPanel Demo Method");
}
}
 

}
}
 
and in another class i have called like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
MapPanelOuter mo = new MapPanelOuter();
Console.ReadKey();
}
}
}
 
Share this answer
 
Comments
g@urav123 3-Jul-14 6:51am    
Thanks
Chakravarthi Elchuri 3-Jul-14 7:12am    
please rate this if your problem solved
1.Because your demo() method is public and your class MapPanel is private you cannot access this method from outside of this class. In order to can have access to it, first you have to make your class public, then by using an object of type MapPanel you will have access to it.

2.If you want to have access to your demo() method from this class without to use an object of type MapPanel you have to declare your demo() method public static , but also in this 2nd case first you have to make your class public.
 
Share this answer
 
v2
Comments
g@urav123 3-Jul-14 1:31am    
Yes i have made it public also but it is not allowing me to call the method by creating the object of it from outside anywhere.
Raul Iloc 3-Jul-14 7:09am    
I saw some code in your comment to "Solution3", so if this is your code you have to make "public" also your "MapPanelOuter" class!
Sp both classes must be public like this: "public class MapPanelOuter {...}"
you can call this class Mappanel out side the class
if the following class is inner class then you can create an object for inner class in parent class and you can also call inner class method using Mappanel object
 
Share this answer
 
Comments
g@urav123 3-Jul-14 1:59am    
I am not able to call demo method by creating object of parent class also.
Chakravarthi Elchuri 3-Jul-14 4:01am    
I have created class like this and called with in the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication3
{
class MapPanelOuter
{
public MapPanelOuter()
{
MapPanel mp = new MapPanel();
mp.demo();
}

private class MapPanel : Control
{
public void demo()
{
Console.WriteLine("Called MapPanel Demo Method");
}
}


}
}

and in another class i have called like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
MapPanelOuter mo = new MapPanelOuter();
Console.ReadKey();
}
}
}
vangapally Naveen Kumar 3-Jul-14 6:38am    
above code is working fine+5
try following code to access.

C#
MapPanel objMapPanel =new MapPanel();
objMapPanel.demo();
 
Share this answer
 
Comments
g@urav123 3-Jul-14 5:49am    
this thing is not working i have already tried this thing..
You can't call a method on a field outside of a method.

sample code:
class 1:
C#
public class Test
   {
       public Test()
       {

       }
       public void Sum()
       {
           Console.Write("hello");
       }
       public string sayhello()
       {
           return "hello";
       }
   }


class2:(creating object and calling methods)

C#
public class testclient
   {
       Test obj = new Test();

       void rt()
       {
           obj.Sum();
           obj.sayhello();
       }
   }
 
Share this answer
 
Comments
g@urav123 3-Jul-14 5:48am    
This thing didn't worked for me
Pravuprasad 3-Jul-14 9:06am    
why it didn't work for you? What was the error?
g@urav123 4-Jul-14 8:08am    
it is not able to create the object.
Is possible to create the object for that class (MapPanel) in some other class?.if not possible to access this class to other class means you have to check the MapPanel class property(right click className in solution explorer) called
"Build Action " set to "Compile".i think now you may able to access this class.
 
Share this answer
 
Comments
g@urav123 3-Jul-14 5:48am    
i didn't created different class file for the MapPanel in the Project. it is in other Class file.
I think u can not create a class because of it is private class.
 
Share this answer
 
Comments
g@urav123 3-Jul-14 1:21am    
but after making it to public then also i am not able to get method call
purvivani 3-Jul-14 1:38am    
if possible give ur whole code

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



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