0

I'm new to c# and am looking to find how to call global object instances in my winform.

 namespace BeastEngine {
     public partial class Form1 : Form
     {

          private  Root mRoot = new Root();
          private  RenderWindow mWindow;
          private  SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
          private Camera cam = mgr.CreateCamera("Camera");

Right now I'm calling my variables like so, but I'm getting various errors.

private void meshToolStripMenuItem_Click(object sender, EventArgs e)
      {
          Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
          mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
          cam.LookAt(ent.BoundingBox.Center);
      }

I want it so that all of my functions can use the one instance. Does anyone know of the proper way? I've googled everywhere.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    What errors do you get? – SLaks Mar 03 '13 at 03:27
  • A field initializer cannot reference the non-static field, method, or property – user2040490 Mar 03 '13 at 03:28
  • 1
    For future - consider first checking MSDN about error code that is part of the message. In your case it is [CS2036](http://msdn.microsoft.com/en-us/library/5724t6za(v=VS.80).aspx) which points to article that covers your case. – Alexei Levenkov Mar 03 '13 at 03:34

2 Answers2

1

You are looking for the Singleton pattern. If you search for that, you should get a lot more results.

Here's Microsoft's recommendation for Implementing a Singleton.

TylerOhlsen
  • 5,485
  • 1
  • 24
  • 39
0

You cannot use an instance variable outside of the constructor

private Root mRoot = new Root();
private SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
private Camera cam = mgr.CreateCamera("Camera");

You should instantiate this in your constructor.

private Root mRoot;
private SceneManager mgr;
private Camera cam;

public Form1()
{ 
    mRoot = new Root();
    mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
    cam = mgr.CreateCamera("Camera");
}

If you want to make sure it is known that this is only created at construction, you can make the variables readonly

Here is an article on constructors

Look at Jon Skeet's answer for more information

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I'm sorry, but how would I go about doing that? @Justin Pihony – user2040490 Mar 03 '13 at 03:30
  • @user2040490 Updated my answer to give you the basic syntax – Justin Pihony Mar 03 '13 at 03:31
  • I have tried this already, but then it gives me an error whenever I call the instance ex. cam.position(0,0,0); - Object reference not set to an instance of an object. – user2040490 Mar 03 '13 at 03:33
  • @user2040490 Sorry, I didnt see you were doing even more...I updated....do not do basic initialization outside of the constructor – Justin Pihony Mar 03 '13 at 03:36
  • on my initialization of cam, I get the error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. – user2040490 Mar 03 '13 at 03:40
  • OK, now you really need to learn how to debug your own code. Most likely this is coming from somewhere in the CreateCamera method. Look at your callstack or add a debug breakpoint. Your question is answered, now you are talking about another error from something I have no knowledge of – Justin Pihony Mar 03 '13 at 03:43