Sign in to follow this  

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

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = new Node();
    }



    And here is an excerpt from "Renderer":

    class Renderer
    {
    public void advanceFrame()
    {
    for (int i = 0; i < nodes.Length; i++)
    {
    if (nodes[i].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?

    Share this post


    Link to post
    Share on other sites
    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"?

    Share this post


    Link to post
    Share on other sites
    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?

    Share this post


    Link to post
    Share on other sites
    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();
    }
    }

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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?

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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?

    Share this post


    Link to post
    Share on other sites

    This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

    If you intended to correct an error in the post then please contact us.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this