C
C#12mo ago
drewhosick

Damned Arrays

I've been trying since yesterday to figure out what I'm doing wrong. I try to initialize a multidimentional array in a method inside a class and then fill it using a double for loop. Trying to fill it all with # characters. I know if I take out the word public it seems to have less errors but then I can't access it outside of the method. I assume I could pass it out as a return but I was hoping I could just access it directly for now. With the public word in front it gives me a whole bunch of errors. I assume I have the syntax wrong somewhere or I'm doing something I can't do inside of a method but don't know why.
internal class CreateMaze
{
public void BuildMaze()
{
public string[ , ] maze = new string[30, 40];

for (int i = 0; i <29; i++)
{
for (int j = 0; j < 39; j++)
{
maze [i, j] = "#";
}
}
}

public void DisplayMaze()
{
for(int i = 0;i < 29;i++)
{
for( int j = 0;j < 39;j++)
{
Console.WriteLine(maze[i, j]);
}
}
}
}
internal class CreateMaze
{
public void BuildMaze()
{
public string[ , ] maze = new string[30, 40];

for (int i = 0; i <29; i++)
{
for (int j = 0; j < 39; j++)
{
maze [i, j] = "#";
}
}
}

public void DisplayMaze()
{
for(int i = 0;i < 29;i++)
{
for( int j = 0;j < 39;j++)
{
Console.WriteLine(maze[i, j]);
}
}
}
}
10 Replies
jcotton42
jcotton4212mo ago
public and other accessibility modifiers like private and internal are not valid on local variables only on classes, fields, methods, properties you need to either return the array from BuildMaze, or move it to a field or property on the class @drewhosick
Angius
Angius12mo ago
You won't be access it ourside of the method either way They're called "local variables" for a reason
jcotton42
jcotton4212mo ago
$structure
MODiX
MODiX12mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius12mo ago
If you want the array to be available to other methods in the class, you will need to move it to the class scope $scopes
MODiX
MODiX12mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
jcotton42
jcotton4212mo ago
personally I'd change this to
public string[,] BuildMaze() { ... }
public void DisplayMaze(string[,] maze) { ... }
// usage
var maze = BuildMaze();
DisplayMaze(maze);
public string[,] BuildMaze() { ... }
public void DisplayMaze(string[,] maze) { ... }
// usage
var maze = BuildMaze();
DisplayMaze(maze);
drewhosick
drewhosickOP12mo ago
ah crap eh. Ok thanks I knew it couldn't be hard but I just couldn't figure out what I was doing wrong. I just moved the declaration outside the method and it works now. Thank you for the quick lesson on scope. I didn't know you couldn't create a public variable in the method that would be available outside by using the public modifier.
jcotton42
jcotton4212mo ago
$close
MODiX
MODiX12mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server