[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2008-06-25
6
Medium Priority
?
219 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

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.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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