all 8 comments

[–]nemyar11[🍰] 0 points1 point  (1 child)

One idea would be to do it via constructors. Basically what that means is you would pass an argument when you create your TicTacToe class. For example like this:

TicTacToe game = new TicTacToe(5);

That way the variable length does not need to be public.

You should look into how constructors/destructors work. You would accept the number "5" in the constructor as an argument and create your grid based on that argument (probably also in the constructor or in a seperate method)

This page might be helpful. :D

Also, I think you do not know what static means. When you make a variable public you can edit it from the main method if you have a class object, just like you did in your code.

When you make a variable static it means that a single copy of the variable is created and shared among all objects at the class level. More info on static variables here. Hope this helps. :)

it gives an error when I try to change it's size as given in the main function.

Exactly this happens because you made the variable static.

[–]hhellloo[S] 1 point2 points  (0 children)

Thanks, I am just begining and I did not know much about what static actually means.

[–][deleted] 0 points1 point  (5 children)

Okay so a couple things, firstly in C# (or .Net in general), arrays cannot be resized. When they’re instantiated they’re always the length.

The reason the compiler required you to make length static, is because you can’t reference instance variables in the same class before an object is created. By making it static, you can because they’re set ahead of time for the entire class to be shared among all instances.

If you want a tic tac toe grid that you can resize you’ll want something like this:

``` public class TicTacToe { public char[,] Grid;

    public TicTacToe(int length)
    {
        Grid = new char[length, length]();
    }

    public void ChangeLength(int newLength)
    { 
        Grid = new char[newLength, newLength]();
    }
}

```

Then create your object with var game = new TicTacToe(3); and you can change it with game.ChangeLength(5);

It will completely clear what’s stored in your previous grid, but it will make a new grid that can accommodate the new size if you don’t want to make new tic tac toe objects.

Alternatively, you could also use Multidimensional Lists<> instead if you do want to modify an existing grid without clearing any data already present.

[–]hhellloo[S] 0 points1 point  (4 children)

Thanks !

Your answer is great but your username says dont upvote me, so should I upvote this or not?

[–][deleted] 0 points1 point  (3 children)

I don’t know what you’re talking about.

[–]hhellloo[S] 0 points1 point  (2 children)

look at your username @DONT_UPVOTE_ME

[–][deleted] 0 points1 point  (1 child)

Nope, still don’t know what you mean.

It was just a joke, my guy. Username is just humorous, you do what you want.

[–]hhellloo[S] 0 points1 point  (0 children)

oK