|
-
December 15th, 2009, 07:15 PM
#1
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!?!?!
}
-
December 15th, 2009, 07:34 PM
#2
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
-
December 15th, 2009, 09:45 PM
#3
Re: Public arrays?
Thanks! I'm a little confused on C#. I've been trying to learn it for XNA.
-
December 15th, 2009, 09:49 PM
#4
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.
-
December 16th, 2009, 12:44 AM
#5
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
-
December 16th, 2009, 05:58 AM
#6
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. 
-
December 16th, 2009, 06:00 AM
#7
Re: Public arrays?
 Originally Posted by redcodefinal
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;
}
 Originally Posted by memeloo
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.
-
December 16th, 2009, 06:24 AM
#8
Re: Public arrays?
 Originally Posted by kristof1104
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
-
December 16th, 2009, 07:10 AM
#9
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
-
December 28th, 2009, 05:05 PM
#10
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.
-
December 28th, 2009, 05:46 PM
#11
Re: Public arrays?
 Originally Posted by lexavision
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.
-
December 28th, 2009, 09:30 PM
#12
Re: Public arrays?
 Originally Posted by Arjay
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|