Solved

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

Posted on 2008-06-25
6
210 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

808 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