从Main()调用

时间:2013-02-24 14:43:29

标签: c# class main

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

提前致谢。如果您想了解更多信息,请随时提出。

 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)

但是,如果我在main中调用StartGame(),如果我将所有引用放在其中,则不起作用,因为它们不在Main中。

2 个答案:

答案 0 :(得分:0)

一些评论:

错误:

  

方法'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方法中使用的参数?

我创建了一个包含一系列片段的类,其中包含修改它们的方法。如果你需要获得碎片的价值,我就创造了吸气剂。

Pascal案例适用于您的字段:

通常,我们对字段使用Camel case(或者我们使用“_”或“m_”前缀)和Pascal Case作为属性,类和方法。如果所有内容都在Pascal Case中,那么您并不知道自己在使用什么。这不是强制性的,但这是一个很好的实践规则。

答案 1 :(得分:0)

我认为人们没有回答的原因是因为你证明缺乏基本的编程概念,人们很难向你解释你的问题。 @jibedoubleve突出了很多。我建议你拿一本C#初学者书来阅读以获得更多的理解。

恕我直言,我认为你的基本问题是理解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对象。