Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function From Class file without creating Object of that class

Tags:

c#

asp.net

I have created a function in a class. and i want to call it throughout my project. but i don't want to create the object of that class in every page. is there any global declaration for that class so that we can call in every page ? Inheritance is not possible in code behind file of aspx page .cs file.

like image 274
Moiz Kachwala Avatar asked Jan 20 '12 08:01

Moiz Kachwala


People also ask

How do you call a class function without creating an object?

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

Which function is called without object of class?

Like static data members, you may access a static member function f() of a class A without using an object of class A .

Can I call a class method without creating an instance?

We can also make class methods that can be called without having an instance. The method is then similar to a plain Python function, except that it is contained inside a class and the method name must be prefixed by the classname. Such methods are known as static methods.

How do you call a method from another class without creating an object in C#?

You just need to make it a static method: public class Foo { public static void Bar() { ... } } Then from anywhere: Foo.


2 Answers

You need to create a Static Method in your class so that you can call the function without creating an object of that class as shown in following snippet:

public class myclass
{
 public static returntype methodname()
 {
    //your code
 }
}

to call the function just use

//ClassName.MethodName();
myclass.methodname();

you can have look at MSDN: Static Members

Suggestion

One more resolution to your problem is to make use of SINGLETON DESIGN PATTERN

Intent

  1. Ensure that only one instance of a class is created.
  2. Provide a global point of access to the object.

UML diagram

like image 141
Pranay Rana Avatar answered Oct 27 '22 00:10

Pranay Rana


You just need to make it a static method:

public class Foo
{
    public static void Bar()
    {
        ...
    }
}

Then from anywhere:

Foo.Bar();

Note that because you're not calling the method on an instance of the type, there won't be any instance-specific state - you'll have access to any static variables, but not any instance variables.

If you need instance-specific state, you'll need to have an instance - and the best way of getting hold of an appropriate instance will really depend on what you're trying to achieve. If you could give us more information about the class and the method, we may be able to help you more.

Admittedly from what I remember, dependency injection in ASP.NET (pre-MVC) is a bit of a pain, but you may well want to look into that - if the method mutates any static state, you'll end up with something which is hard to test and hard to reason about in terms of threading.

like image 25
Jon Skeet Avatar answered Oct 27 '22 00:10

Jon Skeet