Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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):

 

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

Posted

If I'm understanding you correctly what you need is the static keyword.

public class Render
{
   public static void Draw()
   {
       // do stuff
   }

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

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

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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.

// 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(){
   }
}

[sIGPIC]e[/sIGPIC]
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...