我的代码有错误“并非所有代码路径都返回一个值”。 [英] My code has error "not all code paths retuns a value"

查看:70
本文介绍了我的代码有错误“并非所有代码路径都返回一个值”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public  DotNetFrameworkCheck()
{
this .Title = 1. Microsoft .Net Framework 4.5 SP1;
this .Description = 检查是否服务器安装Microsoft .Net Framework 1.1和Service Pack 1.;
.path = SOFTWARE \ \微软\\ NET Framework设置\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
this .dotNetFWRegistryKey = @ HKEY_LOCAL_MACHINE \\ \\ +路径;

}

受保护 覆盖 CheckResult OnCheck( )
{
if (RegistryHelper.KeyExists( this .dotNetFWRegistryKey))
{


使用(RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,RegistryView.Registry32).OpenSubKey( @path))
{
int releaseKey = Convert.ToInt32(ndpKey.GetValue( 发布));

if true
{
< span class =code-keyword> return
new CheckResult(CheckResultState.Passed, - );
}
其他
{
返回 new CheckResult(CheckResultState.Failed, .Net Framework 4.5是没安装。);
}

}

}

}
}
}

解决方案

您的代码应该返回'checkResult'类型。在你的代码中,似乎你的if else语句没有返回正确的值。



检查你的if和else caluse。 both子句应该返回正确的数据类型值。


如果你从未输入这个if语句会怎么样?

  protected  覆盖 CheckResult OnCheck()
{
if (RegistryHelper.KeyExists( this .dotNetFWRegistryKey))
{
// 您的其他代码
}

// 也许在这里添加一个默认的return语句: - )
}





这是你的编译器抱怨的东西


你能读懂吗?当然,IDE有一条波浪线可以显示错误的位置,这正是错误所说的。如果注册表项不存在,则代码不会返回值

public DotNetFrameworkCheck()
        {
            this.Title = "1. Microsoft .Net Framework 4.5 SP1";
            this.Description = "Check whether the server installs the Microsoft .Net Framework 1.1 and Service Pack 1.";
            this.path = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full";
            this.dotNetFWRegistryKey = @"HKEY_LOCAL_MACHINE\" + path;

        }

        protected override CheckResult OnCheck()
        {
            if (RegistryHelper.KeyExists(this.dotNetFWRegistryKey))
            {


                using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@path))
                {
                    int releaseKey = Convert.ToInt32(ndpKey.GetValue("Release"));

                    if (true)
                    {
                        return new CheckResult(CheckResultState.Passed, "-");
                    }
                    else
                    {
                        return new CheckResult(CheckResultState.Failed, "The .Net Framework 4.5 is not installed.");
                    }
                      
                }
                 
            }

        }
    }
}

解决方案

Your code should return 'checkResult' type. in your code it seems that your if else statement is not returning proper value.

check your if and else caluse. both clause should return proper datatype values.


What happens if you never enter this if statement?

protected override CheckResult OnCheck()
{
    if (RegistryHelper.KeyExists(this.dotNetFWRegistryKey))
    {
        // Your other code
    }

    // Maybe add a little default return statement here :-)
}



This is what your compiler is complaining about


Can you read ? Surely the IDE has a squiggly line to show you where the error is, it is exactly what the error says. If the registry key does not exist, your code does not return a value


这篇关于我的代码有错误“并非所有代码路径都返回一个值”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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