Solved

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

Posted on 2008-06-25
6
212 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net WebRequest Code from JSON curl 7 94
Generate Unique ID in VB.NET 21 104
Change to file doesn't show up 16 79
Installing .NET 3.5 on Windows Server 2012 1 62
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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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