Stackoverflow对属性的get函数的异常

时间:2013-03-07 18:49:34

标签: c# sql stack-overflow

我有以下简单的类来管理我的SQL数据库操作

public class DatabaseManager
    {


        private string CommandString
        {
             set { CommandString = GetCommandString(commandtype); }
             get { return CommandString; }
        }
        public string commandtype
        {
            set;
            get;
        }



        public DatabaseManager(string commandtype)
        {
            commandtype = this.commandtype;
            CommandString = GetCommandString(commandtype);
        }

        public DatabaseManager()
        {

        }       


        public static SqlConnection CreateConnection()
        {
            return new SqlConnection(Properties.Settings.Default.connectionString);
        }




        //returns a datatable if the command requires a dataadapter
        public DataTable ExecuteSelect()
        {
            var x = new DataTable();
            using (var da = new SqlDataAdapter(CommandString, DatabaseManager.CreateConnection()))
                {
                    da.Fill(x);
                }

            return x;
        }




        private string GetCommandString(string commandtype)
        {


            switch (commandtype)
            {
                // select commands
                case ("SELECTMARGINS"): CommandString = "select * from margins"; break;
                case ("SELECTRANKS"): CommandString = "select * from ranks"; break;
                /...and other commands

            return CommandString;
        }

    }

我在get { return CommandString; }

上收到了Stackoverflow异常

4 个答案:

答案 0 :(得分:5)

您无法自行返回属性(它会创建无限循环)。

    private string _CommandString;
    public string CommandString
    {
         set { _CommandString = GetCommandString(commandtype); }
         get { return _CommandString; }
    }

答案 1 :(得分:5)

get功能是你的问题

 get { return CommandString; }

这是以下

的士气
public string GetCommandString() { 
  return GetCommandString();
}

这将创建无限递归,最终将抛出StackOverflowException。您需要更改getset以对包含实际值的支持字段进行操作,然后使用该字段

private string _commandString;
public string CommandString {
  get { return _commandString; }
  set { _commandString = GetCommandString(commandtype); }
}

答案 2 :(得分:1)

您无法设置甚至获取CommandString,在这种情况下您必须创建一个私有变量。

private string _commandString;
public string CommandString
{
     set { _commandString = GetCommandString(commandtype); }
     get { return _commandString; }
}

您当前的代码中发生的事情是您正在执行以下操作:

CommandString = "x";

调用

CommandString = GetCommandString(type);

调用

CommandString = GetCommandString(type);

等....所以它一直循环直到它溢出。私有变量使您无法一遍又一遍地设置相同的属性

此外,看起来你实际上从未使用传递给set函数的value,这似乎是一个bug

答案 3 :(得分:1)

你不能让Get函数返回它,它只会使它无限地尝试自己检索它,直到堆栈溢出。

创建一个私有变量来获取并设置为:

private string _CommandString;
private string CommandString
{
    //Also you probably want to change commandtype to value, since you will be
    //discarding whatever you attempt to set the variable as
    set { _CommandString = GetCommandString(commandtype); } 
    get { return _CommandString; }
}
相关问题