0

パブリッククラスと構造体を宣言してから、メインから関数を実行する必要があり、クラス内の変数とメインから構造体を宣言できないという問題があります。このため、「メソッドのオーバーロードなし」エラーのため、関数は機能しません。これを修正するにはどうすればよいですか?非常にシンプルなものが欠けているような気がしますが、見えません。

前もって感謝します。さらに詳しい情報が必要な場合は、遠慮なくお問い合わせください。

 public struct CellReference
    {
        private int CNorth, CEast;

        public int CellsNorth
        {
            get
            {
                return CellsNorth;
            }
            set
            {
                CNorth = value;
            }
        }

        public int CellsEast
        {
            get
            {
                return CellsEast;
            }
            set
            {
                CEast = value;
            }
        }

    }

    static void Main(string[] args)
    {
        Piece black1, black2, black3, black4, black5, black6, black7, black8, black9, black10, black11, black12, white1, white2, white3, white4, white5, white6, white7, white8, white9, white10, white11, white12;


        StartGame();
        Console.ReadKey();


    }


    public class Piece
    {
        public CellReference Location;
        public bool isBlack;
        public bool isKing;

    }

基本的に、StartGame()が必要とするいくつかの変数を初期化することはできません。

さてStartGamesのように:

static void StartGame(ref int CNorth, ref int CEast, ref bool isKing, ref bool isBlack, ref int CellsEast, ref int CellsNorth, ref Piece black1, ref Piece black2, ref Piece black3, ref Piece black4, ref Piece black5, ref Piece black6, ref Piece black7, ref Piece black8, ref Piece black9, ref Piece black10, ref Piece black11, ref Piece black12, ref Piece white1, ref Piece white2, ref Piece white3, ref Piece white4, ref Piece white5, ref Piece white6, ref Piece white7, ref Piece white8, ref Piece white9, ref Piece white10, ref Piece white11, ref Piece white12)

ただし、メインでStartGame()を呼び出すと、すべての参照をメインに配置していないため、参照が機能しません。

4

2 に答える 2

0

いくつかのコメント:

エラー:

メソッド 'Main' のオーバーロードは 0 引数を取ります

このエラーはStartGame、引数の膨大なリストが必要なときに、引数なしでメソッドを呼び出しているために発生します。この問題を解決するには、メソッドに引数を指定してください ;)

アクセサー:

初めてCellReference.CellsEastorの値を取得しようとすると、アクセサーが自分自身を呼び出しCellReference.CellsNorthているため、 a が得られます。StackOverflowExceptionこの問題を修正するには、次のstructように書き換えます。

public struct CellReference
{
    public int CellsNorth
    {
        get; set;
    }

    public int CellsEast
    {
        get; set;
    }
}

StartGame署名:

19引数、多すぎます!!! Arrayの駒をご利用ください。私はこの種のコードを書きました:

void Main()
{
    var pieces= new Piece[] 
    { 
        new Piece(1), 
        new Piece(2) 
    };
    StartGame(array);
}

private void StartGame(Piece[] pieces)
{
    /* Do your work*/
}

class Piece
{
    public Piece(int i)
    {
        this.Value = i;
    }
    private int Value { get; set; }
}

refキーワード:

なぜキーワードを使用しているのrefですか? StartGameメソッドで使用する引数を変更しますかMain?

それらを変更するためのメソッドを備えたピースの配列を含むクラスを作成しました。ピースの値を取得する必要がある場合は、ゲッターを作成しました。

あなたのフィールドのためのパスカルケース:

通常、フィールドにはキャメル ケース (または " _" または " "を前に付けます) を使用m_し、プロパティ、クラス、およびメソッドにはパスカル ケースを使用します。すべてが Pascal Case である場合、何を使用しているのか本当にわかりません。必須ではありませんが、良い実践規則です。

于 2013-02-24T18:32:13.003 に答える
0

人々が答えない理由は、基本的なプログラミングの概念が欠如していることを実証し、人々があなたの問題を明確に説明するのが難しくなっているからだと思います。@jibedoubleve はそれらの多くをハイライトしています。C# の初心者向けの本を手に取って読んで、理解を深めることをお勧めします。

IMHO、あなたが抱えている基本的な問題は、C# プログラムのエントリ ポイントと OOP の概念を理解することだと思います。したがって、コードで説明する私の試みは次のとおりです。

public static class Program
{
    [STAThread]
    static void Main()
    {
        var game = new Game();
        game.StartGame();
    }
}

public class Game
{
    Piece black1, black2, black3,
    black4, black5, black6, black7, 
    black8, black9, black10, black11,
    black12, white1, white2, white3,
    white4, white5, white6, white7,
    white8, white9, white10, white11, white12;

    public struct CellReference
    {
        public int CellsNorth;

        public int CellsEast;
    }

    public class Piece
    {
        public CellReference Location
        public bool isBlack;
        public bool isKing;
    }

    public StartGame()
    {
        // play game logic here
    }
}

自動生成された Program クラスは、ウィンドウの作成やアプリケーションの実行権限の確認など、アプリケーション レベルの問題を処理します。

Main() 関数は、プログラムのエントリ ポイントです。ゲーム オブジェクトを作成して、ピースとロジックを操作します。Program クラスは StartGame() メソッドを持つべきではないことに注意してください。代わりに、ゲームを開始する方法を知っているのは Game オブジェクトです。

于 2013-02-26T10:49:07.860 に答える