C# problem: The name 'nodes' does not exist in the current context

Started by
8 comments, last by The Batfrog 13 years, 3 months ago
I am writing a program that includes 3 classes:
  • Program (the main program class)
  • Node
  • Renderer

    Here is an excerpt from "Program":
    Node[] nodes = new Node[100];            for (int i = 0; i < nodes.Length; i++)            {                nodes = new Node();            }


    And here is an excerpt from "Renderer":
    class Renderer    {        public void advanceFrame()        {            for (int i = 0; i < nodes.Length; i++)            {                if (nodes.active = true)                {                    //bla bla bla                }            }        }    }

    However, in the "Renderer" class, I am getting the error:
    The name 'nodes' does not exist in the current context


    Any ideas?
  • Advertisement
    From what you've written, "nodes" only exists in Programs, not Renderer. Most likely it's private as well. So when you use "nodes" in Renderer, the compiler doesn't know what you are talking about and throws that error. I'm surprised any IDE didn't throw any squiggly lines or any other graphical indication to tell you that. Which IDE are you using? Code::Blocks, Visual Studio, or something else?

    Where in the Renderer class are you declaring and initializing "nodes"?

    Beginner in Game Development?  Read here. And read here.

     

    Thankyou for your reply.

    Quote:Original post by Alpha_ProgDes
    From what you've written, "nodes" only exists in Programs, not Renderer. Most likely it's private as well.


    Yes, this is the case.

    Quote:Original post by Alpha_ProgDes
    I'm surprised any IDE didn't throw any squiggly lines or any other graphical indication to tell you that. Which IDE are you using? Code::Blocks, Visual Studio, or something else?


    i'm using Visual Studio. It creates red squigglys under every instance of the word "nodes" inside of the Renderer class. (I probably should have said that in the original post :P)

    Quote:Original post by Alpha_ProgDes
    Where in the Renderer class are you declaring and initializing "nodes"?


    "nodes" is declared and initialized in the "Program" class (as in the code sample above). I would like to make it public, but i'm not sure how. I can make int, bool, string, etc public; but it seems to be different for an array of objects.

    Do you know how i could make the "nodes" array public?
    Couple of ways, but I have to question WHY you want to do that? A better choice would be to simply either pass the list of nodes straight to the concerned object, or to have the concerned object HOST the node array its self. Also, since the node array will inevitably change size, using a List would probably be a better idea.

    class RenderThingy {    public RenderThingy(Node[] nodes) {        this.nodes = nodes;    }    private Node[] nodes;}class Program {    static void Main(string[] args) {        var nodes = new Node[1024];        var renderThingy = new RenderThingy(nodes);    }}

    class RenderThingy {    public RenderThingy() {        this.nodes = new List<Node>(1024);    }    private List<Node> nodes;}class Program {    static void Main(string[] args) {        var renderThingy = new RenderThingy();    }}

    In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

    The Batfrog, if you don't mind me asking (not trying to be condescending), are you new to programming?

    If so, it sounds like you're not familiar with how scope works. Check this out, it explains your error:

    http://en.wikipedia.org/wiki/Scope_%28programming%29

    Or just Google C# scope and take a look at some of the examples.
    Quote:Original post by Washu
    Couple of ways, but I have to question WHY you want to do that?


    Let me explain what i'm trying to accomplish;
    I'm making a soft body physics Sim. Like most, it will calculate the forces and bonds between atoms/nodes. The program is made from three main parts:
    >Program.cs (turns the simulation into a usable piece of software, adds GUI, etc)
    >Physics.cs (is called to calculate the forces, and derive new positions, velocities, etc)
    >Renderer.cs (uses data about the currant game state to draw a representation of the scene)

    As you can see, all these classes need to have access to the data stored in the "Node"s in the nodes array.

    Quote:Original post by Washu
    A better choice would be to simply pass the list of nodes straight to the concerned object

    Considering that i would need to pass the nodes, and all of the information stored in them, constantly, throughout the execution of the program; (if i wanted a frame rate of 30, i would need to pass the nodes, with their updated information, to the renderer, 30 times a second. And i would need to do the same thing for physics) would this really be efficient?

    Quote:Original post by TheOrzo
    The Batfrog, if you don't mind me asking (not trying to be condescending), are you new to programming?


    No, i'm not really new; I've been learning for about 2 years now, but before that i was learning bits and pieces, as early back as 7 years ago. I guess it wouldn't be strange if i had forgotten something i learnt back then, that is hindering me now. Maybe it might be time for me to break out some old programing books, and re-read the first chapters.

    Quote:Original post by TheOrzo
    Scope.


    So what your saying is that if i declare and initialize the array in the namespace, but outside of the classes, i should be able to access it from any class?
    Quote:Original post by The Batfrog
    Considering that i would need to pass the nodes, and all of the information stored in them, constantly, throughout the execution of the program; (if i wanted a frame rate of 30, i would need to pass the nodes, with their updated information, to the renderer, 30 times a second. And i would need to do the same thing for physics) would this really be efficient?



    C# uses what is known as the reference model of variables for classes. This means that when you pass your nodes container from one place to another, you're just handing off a very light-weight reference (typically 32 or 64 bits depending on platform/etc.) rather than a copy of all the node data.

    So yes, this'll be perfectly efficient enough for your needs.

    Wielder of the Sacred Wands
    [Work - ArenaNet] [Epoch Language] [Scribblings]

    Quote:Original post by ApochPiQ
    Yes, this'll be perfectly efficient enough for your needs.


    Yay, Thanks.
    This should definitely be fine for my Renderer.cs

    However, my Physics.cs also needs access to "nodes", but not just Read access, it needs Write access too.

    Will this method allow changes to be made to the "nodes" array in Program.cs, from Physics.cs?
    Yes. C# references permit any type of access (generally speaking) including the ability to invoke member functions.

    Wielder of the Sacred Wands
    [Work - ArenaNet] [Epoch Language] [Scribblings]

    Sweet!
    Thanks everyone! :D

    I'll post here if i have troubles implementing this solution, but for now it looks like i'm good to go.

    Thanks again guys!

    This topic is closed to new replies.

    Advertisement