English 中文(简体)
我如何把我的主子的物体带入我的班子?
原标题:How do I pass an object from my main into my class?

我是一名学生,从事课外项目。 我的学者要求我制定一项涉及班级的方案。

因此,我创立了一个称为“Piece”的类别,其代码如下:

namespace GG
{
    class Piece
    {
        public int rank;
        public int player; 
    }
}

我在我的主要方案(表格)中做了介绍,例如:

namespace GG
{
    public partial class frmPGame : Form
    {
        public frmPGame()
        {
            InitializeComponent();
        }

        Piece[,] gameBoard = new Piece[9, 8];

        public void clearGameBoard()
        {
            for (int y = 0; y < 8; y++)
            {
                for (int x = 0; x < 9; x++)
                {
                    gameBoard[x, y] = new Piece();
                    gameBoard[x, y].rank = -1;
                    gameBoard[x, y].player = 0;
                }
            }
        }
    }
}

不管怎么说,形式都有图象,而且视2D阵列一带内产生的情况,情况会发生变化。 然而,在我的主要方案中,我也做了一些算术,因此它已经过时,时间很长。 我要问,我是否能够,在某种程度上,把博拉德人反对另一类人及其内容。

基本来说,现在的方案流动是:

  • Create Object
  • Do math in Main using Object
  • Change form pictures

And, I d like to, if possible, change it into:

  • Create Object
  • Do math in Math class
  • Return / Pass Object from math class to Main
  • Change form pictures

我知道,我看上去看一看问题,但我的教授告诉我,唯一应该做的事情就是使情况恶化。 班级应处理与数学有关的 st。

否 我就此提出了一些建议? 我是否在我的“数学地图集”中 instant过“Piece”级?

最佳回答

页: 1 指出这些阵列是参考类型。 如果你将其中一种方法通过,则该方法可以直接加以修改。

First variant: Here the game board isn t "described" as a full object. It s an array with some (static) utility methods put in a GameBoardMath class.

namespace GG
{
    public partial class frmPGame : Form
    {
        public frmPGame()
        {
            GameBoardMath.ClearGameBoard(gameBoard); // Clear the game board

            InitializeComponent();
        }

        Piece[,] gameBoard = new Piece[9, 8];    
    }

    public class GameBoardMath
    {
        public static void ClearGameBoard(Piece[,] gameBoard)
        {
            int lenX = gameBoard.GetLength(0);
            int lenY = gameBoard.GetLength(1);

            for (int y = 0; y < lenY; y++)
            {
                for (int x = 0; x < lenX; x++)
                {
                    gameBoard[x, y] = new Piece();
                    gameBoard[x, y].rank = -1;
                    gameBoard[x, y].player = 0;
                }
            }
        }
    }
}

<>第二代>: 在此,我们认为,<代码>GameBoard是一个“完整”类别。

public class GameBoard
{
    public readonly Piece[,] Board;

    public GameBoard(int x, int y)
    {
        Board = new Piece[x, y];

        ClearGameBoard();
    }

    public void ClearGameBoard()
    {
        int lenX = Board.GetLength(0);
        int lenY = Board.GetLength(1);

        for (int y = 0; y < lenY; y++)
        {
            for (int x = 0; x < lenX; x++)
            {
                Board[x, y] = new Piece();
                Board[x, y].rank = -1;
                Board[x, y].player = 0;
            }
        }
    }
}

在您的<编码>中使用。 表格:

GameBoard gameBoard = new GameBoard(9, 8);
问题回答

Create a GameBoard class which creates Pieces in constructor or some Initialize method. You can then iterate over the pieces in your Main Form and draw the pictures.

See the implementation of Dragger Hor on CodePlex





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签