使用未分配的局部变量-if语句 [英] Use of unassigned local variable - if statements

查看:129
本文介绍了使用未分配的局部变量-if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下代码块,并且编译器抱怨未分配的局部变量,并且可以使用一些帮助来确定发生了什么.

I'm doing the following block of code and the compiler is complaining about unassigned local variables and could use some help identifying what's up.

while (rsData.Read())
{
    if (rsData["TYPE"] != DBNull.Value)
        strType = rsData["TYPE"].ToString().Trim();


    if (strType == "01")
    {
        if (rsData["Text"] != DBNull.Value)
            strwho = rsData["Text"].ToString();

        if ((strwho.Length < 10 || (strwho.IndexOf("NULL") > 1)))
            strwho = "";
    }
    else if (strType == "07")
    {
        if (rsData["Text"] != DBNull.Value)
            strmetades = rsData["Text"].ToString();

        if ((strmetades.Length < 10 || (strmetades.IndexOf("NULL") > 1)))
            strmetades = "";
    }

它在所有的'if(strType =="01")'行上抱怨,我不确定是怎么回事.我曾考虑为此使用开关,但这似乎也遇到了相同的问题.

It complains on all of the 'if (strType == "01")' lines and I'm not sure what's up. I've thought of using a switch for this but that seems to get the same issue also.

有什么想法吗?

推荐答案

在声明字符串strType时,您必须分配一个值,例如

when declaring string strType you must assign a value, something like

string strType = null;

更多详细信息:编译器错误CS0165

这篇关于使用未分配的局部变量-if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆