CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Public arrays?

  1. #1
    Join Date
    Dec 2009
    Posts
    10

    Angry Public arrays?

    Hey,
    In Visual C# how do I make an array that can be used in private and public functions. Visual Studio won't let me use public to declare it. Example below.

    private void main()
    {
    int[,,] test = new int[100,100,3]; //this works
    public in [,,] test = new int [100,100,3]; //this produces and error!
    }

    public int laterfunction(int x, int y)
    {
    test[x,y,0] = 27; // doesn't exist in current context!?!?!
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Public arrays?

    It always helps to post the actual error message...

    That line produces an error because it has an error; aside from the obvious typo, you cannot declare local variables as public, it just doesn't make sense. It is defined inside of a method, so it will be destroyed at the end of the method. If you need to make it public, declare it as an instance or static field. You should study up on variable scope and object lifetimes.

    Aside from that, you should not be exposing public fields or arrays (use properties and a collection interface, i.e., IList<T>) there is nothing illegal about dec

  3. #3
    Join Date
    Dec 2009
    Posts
    10

    Re: Public arrays?

    Thanks! I'm a little confused on C#. I've been trying to learn it for XNA.

  4. #4
    Join Date
    Dec 2009
    Posts
    10

    Re: Public arrays?

    Is there a way I can declare a variable inside a method and make it available to the entire class including other methods.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Public arrays?

    i think you should first learn how OOP works... classes, methods, fields, scope etc. you don't even have a basic knowledge as i see and i'm sorry but you wouldn't even understand our attempts to help you :\ ... and no, you cannot make a method's local variable visible outside this method. make it a field in the class or when public the make it accessible via a property
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Public arrays?

    No. You need instance variable (field) for this purpose.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Sep 2006
    Posts
    31

    Re: Public arrays?

    Quote Originally Posted by redcodefinal View Post
    Hey,
    In Visual C# how do I make an array that can be used in private and public functions. Visual Studio won't let me use public to declare it. Example below.

    private void main()
    {
    int[,,] test = new int[100,100,3]; //this works
    public in [,,] test = new int [100,100,3]; //this produces and error!
    }

    public int laterfunction(int x, int y)
    {
    test[x,y,0] = 27; // doesn't exist in current context!?!?!
    }
    Here you go:
    public statements only work outside functions.
    Code:
    public in [,,] test;
    private void main()
    {
        test = new int[100,100,3];
    }
    
    public int laterfunction(int x, int y)
    {
        test[x,y,0] = 27;
    }
    Quote Originally Posted by memeloo View Post
    i think you should first learn how OOP works... classes, methods, fields, scope etc. you don't even have a basic knowledge as i see and i'm sorry but you wouldn't even understand our attempts to help you :\ ... and no, you cannot make a method's local variable visible outside this method. make it a field in the class or when public the make it accessible via a property
    I agree with the fact the he first has to learn how OOP works.. but if someone who's just started asks a question.. why don't you try to explain it He'll learn much more then when you're trying to bash him
    Last edited by kristof1104; December 16th, 2009 at 06:03 AM.

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Public arrays?

    Quote Originally Posted by kristof1104 View Post
    but if someone who's just started asks a question.. why don't you try to explain it He'll learn much more then when you're trying to bash him
    sure, but how do you want to explain someone this kind of problem when he doesn't know the "basic basics". and there are hundres of websites where one can learn it. i think we don't have to rewrite them in this forum. i came here hoping to learn some tricks and to get inspired by others or inspire others myself and not to teach people OOP or to hear that the try/catch way is the solution for everthing no matter what.
    Last edited by memeloo; December 16th, 2009 at 06:26 AM.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  9. #9
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: Public arrays?

    Just wanted to clarify WHY you cannot declare variables public in a function.
    The reason is simply that variables defined in a function scope "die" when the function has completed its execution.

    Consider this:
    Code:
    public void SomeFunction()
    {
    // Define a local variable
    int xyz = 3;
    
    // Write it out to the console, perfectly accessible still as we have not exited the function
    Console.WriteLine(xyz);
    
    // Once we hit the ending curly bracket for this function bellow, the variable seize to exist
    }
    Now, IF and I emphasize IF, it would be possible to make the xyz variable public, what would be the point of that? For the program, the xyz variable would be "dead" as it is out of its scope.

    The solution, as mentioned before is simply to declare the variable outside of the function scope, but still within the classes scope. Remember, the class is very much alive as long as you do not cut all references to it, hence making the variable "xyz" also alive.

    Hope this helps you understand the lifecycle of objects in C#, just think of the scopes

  10. #10
    Join Date
    Nov 2009
    Posts
    14

    Re: Public arrays?

    Hey memeloo,

    If you have constructive input then please enlighten us with your endless knowledge. Otherwise , do not solicit feedback on posts that do not address the issue.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Public arrays?

    Quote Originally Posted by lexavision View Post
    Hey memeloo,

    If you have constructive input then please enlighten us with your endless knowledge. Otherwise , do not solicit feedback on posts that do not address the issue.
    Memeloo really has the right idea. Anyone that wants to learn programming really needs to spend some time doing it and work through a book, online tutorial, or whatever to get some base knowledge and post questions to a forum as they run into trouble.

    To do the reverse of that is a waste of forum time and isn't going to help someone learn.

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: Public arrays?

    Quote Originally Posted by Arjay View Post
    Memeloo really has the right idea. Anyone that wants to learn programming really needs to spend some time doing it and work through a book, online tutorial, or whatever to get some base knowledge and post questions to a forum as they run into trouble.

    To do the reverse of that is a waste of forum time and isn't going to help someone learn.
    ++ on that one. Forums like this aren't "Teach me C# in 10 easy steps". There's dozens of great books available along with thousands of beginners tutorials online. That's where you learn C#. Here is more for when you get stuck or can't understand a particular concept once you've gone through the basics.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured