简体   繁体   English

C#SQL插入。 使用未分配的局部变量?

[英]c# sql insert. Use of unassigned local variable?

My application is saying totalTokens is use of an unnassigned local variable and I am not seeing why. 我的应用程序说totalTokens使用的是未分配的局部变量,我不知道为什么。 I must be missing something obvious. 我一定想念一些明显的东西。 Here is the line. 这是线。 Full code is below. 完整代码如下。 Thanks 谢谢

    myCommand.Parameters.Add("@Tokens", totalTokens);



    private void btnSave_Click(object sender, EventArgs e)
    {
        string date = dateTimePicker1.Value.ToShortDateString();
        bool library = chkLibrary.Checked = true;
        string libReason = txtReason.Text;
        int libMin = Int32.Parse(txtLibTime.Text);
        int hwMin = Int32.Parse(txtHwTime.Text);

        int partialTokens;
        int totalTokens;
        int totalHw;

        // calculate Total homework
        totalHw = libMin + hwMin; 

        // calculate bonus library time
        partialTokens = totalHw / 15;

        // calculate Total tokens
        if (chkLibrary.Checked == true)
        {
            totalTokens = partialTokens + 1;
        }

        using (SqlCeConnection con = new SqlCeConnection(conString))
        {
            SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO ChangeTable" + 
            "(Date, Library, LibMissed, LibTime, HwTime, TotalHomework, Tokens)" +
            "VALUES(@Date, @Library, @LibMissed, @LibTime, @HwTime, @TotalHomework, @Tokens)", con);

            myCommand.Parameters.Add("@Date", date);
            myCommand.Parameters.Add("@Library", library);
            myCommand.Parameters.Add("@LibMissed", libReason);
            myCommand.Parameters.Add("@LibTime", libMin);
            myCommand.Parameters.Add("@HwTime", hwMin);
            myCommand.Parameters.Add("@TotalHomework", totalHw);
            myCommand.Parameters.Add("@TotalHomework", totalTokens);

            con.Open();
            myCommand.ExecuteNonQuery();
            con.Close();

            RefreshGrid();
        }
    }

The totalTokens variable is only assigned a value if the condition in the if statement is true. 仅当if语句中的条件为true时,才为totalTokens变量分配一个值。 You have to make sure that the variable always has a value. 您必须确保变量始终具有一个值。

Perhaps you meant something like: 也许您的意思是:

// calculate Total tokens
if (chkLibrary.Checked == true)
{
  totalTokens = partialTokens + 1;
} else {
  totalTokens = partialTokens;
}

Side note: When you use the variable, you are assigning two values to the same parameter, which will of course overwrite the first value: 旁注:使用变量时,您将向同一个参数分配两个值,这当然会覆盖第一个值:

myCommand.Parameters.Add("@TotalHomework", totalHw);
myCommand.Parameters.Add("@TotalHomework", totalTokens);

Judging from the parameters used in the query, it should be: 从查询中使用的参数来看,它应该是:

myCommand.Parameters.Add("@TotalHomework", totalHw);
myCommand.Parameters.Add("@Tokens", totalTokens);

totalsTokens will only be assigned if chkLibrary.Checked is true . totalsTokens如果将只分配chkLibrary.Checkedtrue If it is false then it would remain unassigned. 如果为false则它将保持未分配状态。

 if (chkLibrary.Checked == true)
        {
            totalTokens = partialTokens + 1;
        }

this is the only time totalTokens is assigned to..... so if chkLibrary.Checked isn't checked, then it's an unassigned variable. 这是将totalTokens唯一分配给.....的时间,因此,如果未选中chkLibrary.Checked ,则它是未分配的变量。

Not sure what you want it to be by default? 不确定要默认使用什么? but perhaps when you declare it you want 但是也许当你宣布它想要

int totalTokens = 0;

Wheres this line on your code 这行代码在哪里

myCommand.Parameters.Add("@Tokens", totalTokens);

you should have this between myCommand initialization and con.Open(); 您应该在myCommand初始化和con.Open()之间使用它;

totalTokens is only initialized inside that conditional; totalTokens仅在该条件内初始化; thus it's not guaranteed to be set when you call myCommand.Parameters.Add("@TotalHomework", totalTokens); 因此,不能保证在调用myCommand.Parameters.Add("@TotalHomework", totalTokens); .

Just initialize it to something (like 0) where you declare it, and the warning should go away. 只需将其初始化为声明它的内容(例如0),警告就会消失。

The fix is basically assign it SOME kind of value when you create it, for example 该修复程序基本上是在创建它时为其分配某种值,例如

int partialTokens = 0;
int totalTokens= 0;
int totalHw = 0;

Currently, it is possible it will be used without having a value assigned, due to the assignment inside a conditional statement. 当前,由于条件语句中的分配,可能没有分配值就可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM