Simple projects? (C#)

Started by
16 comments, last by boogyman19946 12 years, 3 months ago
Well, looking at some of their answers to the project, they have classes withint width in them, but I do not know how to get int width, into a different class to use it with that class also.
Advertisement
To access a member of a class instance you just use the . operator on that instance. So if you have a class Maze with a int member variable named width and you had a Maze instance named maze you could just use maze.width to get the width value for that particular maze.
Hmm, still not quite getting it. I think I will do some more reading up on it, then maybe try a text based game first, then attempt that again.

Thanks for the help
I would still like to help you get this point. Can you show a more complete example of what you're trying to do?

I would still like to help you get this point. Can you show a more complete example of what you're trying to do?


This is the code that I currently have (not much):

using System;
class MazeGenerator
{
static void Main()
{

Console.Title = "Welcome to the Random Maze Generator for C#";
Console.WriteLine("Welcome to the Random Maze Generator for C#");
Console.WriteLine("Please press Enter to continue to the Generator");
MazeQuery();

Console.ReadLine();
}
static void MazeQuery()
{
int mazeWidth, mazeHeight, wall, numberOfRooms;
do
{
Console.Write("Please enter the number of columns: ");
mazeWidth = int.Parse(Console.ReadLine());
} while (mazeWidth < 2 || mazeWidth > 50);
do
{
Console.Write("Please enter the number of rows: ");
mazeHeight = int.Parse(Console.ReadLine());
} while (mazeHeight < 2 || mazeHeight > 50);
numberOfRooms = mazeWidth * mazeHeight;

wall = numberOfRooms * 2 - (mazeWidth + mazeHeight);
Console.ReadLine();
}
}


I don't know how to get numberOfRooms or wall to a new method or class to calculate what walls I have to knock down.
Perhaps if you see a code example you'll be able to put things together a little better.

Suppose you have the following:



public class Maze
{
public int width;
}


Then the following code does the following: creates an instance of Maze and calls it myMaze, creates a variable called newWidth with value 32, and writes newWidth into the width variable of the Maze class instance (myMaze)


public class Maze
{
public int width;
}

public class Main
{
public static void Main(string[] arguments)
{
Maze myMaze = new Maze();
int newWidth = 32;
myMaze.width = newWidth;
}
}


If you want to save the width variable into something else, say, the newWidth variable, you can do it like so:



public class Maze
{
public int width;
}

public class Main
{
public static void Main(string[] arguments)
{
Maze myMaze = new Maze();
int newWidth = myMaze.width;
}
}


EDIT: you can create a Maze instance in Main() and pass it as an argument to the mazeQuery() function where you can save the data into its members.

Yo dawg, don't even trip.

In this case you'd use a function parameter. So if you had

static void SomeFunction(int rooms) {
// stuff
}

You could call it in MazeQuery() like:

SomeFunction(numberOfRooms);
I get it now, and if I wanted to get something from a class, I could also use something like this:

using System;
class SimpleClass
{
public static void Main()
{
Console.WriteLine(ThatClass.yes);
Console.ReadLine();
}
}
class ThatClass
{
public static int yes = 10;
}
That's correct. Since variable yes is static, you have no need to instantiate the class ThatClass before using it, you can just call it directly like you have above.

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement