Creating a Class with Functions that can be used without an instance of the class [C#

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Simply put, I want to make a class with functions that I can run without the need to actually instantiate an instance of the class...

Let me explain with an example; given a class FORM1 & RENDER I want to be able to use the functions of class RENDER in class FORM1 without the need to create an instance of RENDER, something like the following (pseudo-code):

Code:
public class FORM1
{	
	public FORM1()
	{
		// See I am using RENDER without creating a RENDER render = new RENDER(), no need for a instance of RENDER
		RENDER.DRAW();
		RENDER.CHANGES();
		RENDER.UPATE();
	}

	private void FORM1_Load(object sender, System.EventArgs e)
	{
		// See I am using RENDER without creating a RENDER render = new RENDER(), no need for a instance of RENDER
		RENDER.DRAW();
		RENDER.CHANGES();
		RENDER.UPATE();
	}
}

So the idea is, creating a class (RENDER) with functions (such as DRAW, CHANGES, UPDATE, etc..) that can be used without the need to create and hold an instance of RENDER....
Any ideas, hints, and help would be greatly appreciated, thanks
 
If I'm understanding you correctly what you need is the static keyword.
C#:
public class Render
{
    public static void Draw()
    {
        // do stuff
    }

    public static void Changes()
    {
        // do stuff
    }

    public static void Update()
    {
        // do stuff
    }
}
 
Using static (or Shared in VB) you can not only create instance-independent functions but also properties, fields, and even events which are tied to the type itself rather than an instance of the type, which makes for some interesting possibilities, such as static event which is raised each time an object is created or destroyed.

You can also tag a class with a static modifier that will disallow instantiation and the creation and use of instance members. Just don't get too carried away and forget they C# is primarily an object-oriented language.
C#:
// We can't instantiate this class.
// It is essentially a placeholder for functions,
// fields, and properties.
static class UserPrefs{ 
    Rectangle windowLocation; // compile error (instance member)
    static bool LoadOnStartup;
    static string UserName;

    static void SaveSettings(){
    }
    static void LoadSettings(){
    }
}
 
Before I were to use a static method in your case, I'd wonder what you want to do? Your sample shows using 'RENDER' on a form - you could instantiate a variable one time in the form's constructor for example.

Maybe this is a generic type of form to draw graphics - maybe you want a base form that has the Draw, Changes and Update methods.

Your reasons are your own - if you just want some global functions (more or less), use a class with static methods. You could even put those methods in your form if you wanted, though I wonder what it is you're really trying to accomplish...

-ner
 
Back
Top