?
Solved

how can I refer to a static method wothout prefixing it with class name ?

Posted on 2008-06-25
6
Medium Priority
?
217 Views
Last Modified: 2013-12-16
Visual Studio 2005 C#

This is my class below. I want to go AddNumbers(10,20); from anywhere in the solution instead of
Test.AddNumbers(10,20);
public  class Test
    {
        public static int AddNumbers (int x, int y)
        {
            return x + y;
        }    
    }
0
Comment
Question by:indikad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21871877
I don't think you can do that. C# is an object-oriented language, function calls are made with the class name as the scope.
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 21871911
You can declare a delegate and then assign the method to this delagate and use this delegate in the solution.

public  class Test
    {
        public static int AddNumbers (int x, int y)
        {
            return x + y;
        }    
    }

In your Main class declare the following

public delegate AddNumbersDelegate(int x, int y);
public static AddNumbersDelegate  MyAddNumbers = new AddNumbersDelegate(Test.AddNumbers);

Then you can use MyAddNumbers() function throughout the main Class.
0
 

Author Comment

by:indikad
ID: 21871923
CuteBug,

I want to refere to it from anywhere in the solution - not only the Main.
looks like it is not possible? I am not entirely surpised with C# being object oriented, but would be nice to have.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 21871940
Check this out, it's a page from microsoft showing differences between c++ and c#. Once on the page, search for "Globals:".
http://msdn.microsoft.com/en-us/library/yyaad03b.aspx
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21871950
Personally I don't think just AddNumbers is much different from Test.AddNumbers, except that it looks cleaner. You can take a look at VB.NET, if this is important to you. :)
0
 

Author Closing Comment

by:indikad
ID: 31470855
thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question