Calling from Main()

本文关键字:Main from Calling | 更新日期: 2023-09-27 18:01:05

我有一个问题,我声明了一个公共类和结构,然后必须从main中运行一个函数,并且不能从main中声明类和结构中的变量。因此,由于"方法无过载"错误,该函数将无法工作。我该怎么解决这个问题?我觉得我好像错过了一些非常简单的东西,但却看不见。

提前谢谢。如果你想了解更多信息来帮助我,请毫不犹豫地询问。

 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((需要的一些变量。

Well StartGames as so:

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)

然而,如果我在main中调用StartGame((,如果我把所有引用都放进去,那就不起作用,因为它们不在main中。

Calling from Main()

一些备注:

错误:

方法"Main"没有重载,采用0个参数

发生此错误的原因是,您调用的方法StartGame没有参数,而它需要一个庞大的参数列表。若要解决此问题,请为方法指定参数;(

访问者:

第一次尝试获取CellReference.CellsEastCellReference.CellsNorth的值时,会得到一个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方法中使用的参数吗?

我创建了一个类,它包含一个片段数组,并带有修改它们的方法。如果你需要得到这些片段的值,我会创建getter。

Pascal案例适用于您的字段:

通常,我们对字段使用Camel大小写(或者前缀为"_"或"m_"(,对属性、类和方法使用Pascal大小写。如果所有内容都在Pascal Case中,那么您并不真正知道自己在使用什么。这不是强制性的,但却是一条良好的实践规则。

我认为人们不回答的原因是因为你缺乏基本的编程概念,人们很难向你清楚地解释你的问题@jibeddoubleve突出了其中的许多。我建议你拿起一本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((函数是程序的入口点。您可以创建一个Game对象来操纵片段和逻辑。注意,Program类不应该有StartGame((方法,相反,它是知道如何启动游戏的Game对象。