Solved

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

Posted on 2008-06-25
6
208 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
  • 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Accepted Solution

by:
philipjonathan earned 125 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now