Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if class have function
C#
public void dbConnection(string ProjectName112)
{
  string strProject = ProjectName112; //Enter your SQL server instance name
}
Posted
Updated 2-Mar-15 20:51pm
v2
Comments
Thanh Xuan Vu 3-Mar-15 2:40am    
what do you want ?
[no name] 3-Mar-15 2:42am    
i want to call class function value in windows forms application.
[no name] 3-Mar-15 2:51am    
What ?

1 solution

In the case of the method you show, in order to call it you need an instance of the containing class: an example of the class that the method is part of, created at some point with the new keyword:
C#
MyClass mc = new MyClass();
...
mc.dbConnection();

If you change it to a static method:
C#
public static void dbConnection(string ProjectName112)
{
  string strProject = ProjectName112; //Enter your SQL server instance name
}
Then you don;t need the instance, you can just use the class name:
C#
MyClass.dbConnection();
But then the method can't access any non-static field, properties or methods of the MyClass class.

But...the example you show does nothing useful: it returns no value, and the string variable strProject is destroyed when the method exits, so nothign will influence the outside world.

I think you need to think a bit about exactly what you are trying to achieve here: this probably isn't going to work...
 
Share this answer
 

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