C++ to C# get set question..

A

Andrew McCall

Hi,

I am a (bad) C++ programmer, and I thought I would learn C# so I can
play with Microsoft XNA Game Studio Express. I haven't picked up any
C# books yet, but have been reading articles around the web until I get
chance to get to a shop.

I am a little stuck on creating my get and set methods, using the
article here:

http://www.c-sharpcorner.com/Code/2003/July/CSharpMethodsIntro.asp

I thought I would start off by creating a simple OXO game, but my set
method isn't working. I am getting the error "Error 1 The name 'value'
does not exist in the current context ".

Can someone help me out please?

My code is below:

namespace ExtremeOXO
{
class OXOBoard
{
enum OXOTile
{
EmptyTile,
XTile,
OTile
};

OXOTile[,] oxotOXOGameBoard = new OXOTile[3, 3];

public OXOBoard()
{//OXOBoard()
Console.WriteLine("Entering OXOBoard() Constructor");
InitializeBoard();
OutputBoard();
Console.WriteLine("Leaving OXOBoard() Constructor");
}//OXOBoard()

public void InitializeBoard()
{//InitializeBoard()
Console.WriteLine("Entering InitializeBoard()");
for (int iCountX=0; iCountX < 3; iCountX++)
{//For
for (int iCountY = 0; iCountY < 3; iCountY++)
{//For

Console.WriteLine("Clear tile {0},{1} here when set
works.....",iCountX,iCountY);
}//For
}//For
Console.WriteLine("Leaving InitializeBoard()");
}//InitializeBoard()

void set(int x, int y)
{
oxotOXOGameBoard[x, y] = value;
}

OXOTile get(int x, int y)
{
return oxotOXOGameBoard[x, y];
}
}
}
 
D

David Browne

Andrew McCall said:
Hi,

I am a (bad) C++ programmer, and I thought I would learn C# so I can
play with Microsoft XNA Game Studio Express. I haven't picked up any
C# books yet, but have been reading articles around the web until I get
chance to get to a shop.

I am a little stuck on creating my get and set methods, using the
article here:

http://www.c-sharpcorner.com/Code/2003/July/CSharpMethodsIntro.asp

I thought I would start off by creating a simple OXO game, but my set
method isn't working. I am getting the error "Error 1 The name 'value'
does not exist in the current context ".

Can someone help me out please?

Looks like you want an indexer, like this:


namespace ExtremeOXO
{
public class OXOBoard
{
public enum OXOTile
{
EmptyTile,
XTile,
OTile
};

OXOTile[,] oxotOXOGameBoard = new OXOTile[3, 3];

public OXOBoard()
{//OXOBoard()
Console.WriteLine("Entering OXOBoard() Constructor");
InitializeBoard();


//OutputBoard();
Console.WriteLine("Leaving OXOBoard() Constructor");
}//OXOBoard()

void InitializeBoard()
{
Console.WriteLine("Entering InitializeBoard()");
for (int iCountX=0; iCountX < 3; iCountX++)
{
for (int iCountY = 0; iCountY < 3; iCountY++)
{
this[iCountX, iCountY] = OXOTile.EmptyTile;
Console.WriteLine("Clear tile {0},{1} here when set
works.....",iCountX,iCountY);
}
}
Console.WriteLine("Leaving InitializeBoard()");
}//InitializeBoard()


public OXOTile this[int x,int y]
{
get { return oxotOXOGameBoard[x, y]; }
set { oxotOXOGameBoard[x, y] = value; }
}

}
}
 
B

Bill Butler

I thought I would start off by creating a simple OXO game, but my set
method isn't working. I am getting the error "Error 1 The name
'value'
does not exist in the current context ".

Can someone help me out please?

My code is below:

void set(int x, int y)
{
oxotOXOGameBoard[x, y] = value;
}

OXOTile get(int x, int y)
{
return oxotOXOGameBoard[x, y];
}

Your attempt is somewhere between a method and a property.

Try this:
public OXOTile this[int x, int y]
{
get{return oxotOXOGameBoard[x, y];}
set{oxotOXOGameBoard[x, y] = value;}
}

You will also need to make OXOTile public

Hope this helps
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top