使用未分配的局部变量错误 [英] Use of unassigned local variable error

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

问题描述

我是用于密码验证的接线代码,密码必须包含至少1个大写字母,1个小写字母,1个数字,并且如果不满足这些条件中的一个或多个,则最小长度为12个字符,摘要输出到控制台,允许用户查看密码未被接受的原因。代码如下所示:



I am wiring code for password validation,the password must contain at least 1 uppercase letter, 1 lower case letter, 1 number, and be a minimum length of 12 characters long, if one or more of these criteria are not met, the summary is output to the console allowing the user to see why the password was not accepted. The code is as seen below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a valid password");
            Console.WriteLine("Password must be minimum 12 characters long");
            Console.WriteLine("Password must contain at least one numerical value");
            Console.WriteLine("Password must contain at least one Upper case character");
            Console.WriteLine("Password must contain at least one Lower case character");

            // Variable declaration
            bool bolValidLength;
            bool bolValidUpper;
            bool bolValidLower;
            bool bolValidPassword;

            //Password declaration
            string Password = Console.ReadLine();

            //Loop program if password not acceptable

                //Checking Password Length
                int lengthPass(string pass)
                {
                    int num = 0;

                    foreach (char ch in pass)
                    {
                        if (char.IsDigit(ch))
                        {
                            num++;
                            bolValidLength = true;
                        }
                    }
                    return num;
                }

                //Checking Password for uppercase letters
                int upperCase(string pass)
                {
                    int num = 0;

                    foreach (char ch in pass)
                    {
                        if (char.IsUpper(ch))
                        {
                            num++;
                            bolValidUpper = true;
                        }
                    }
                    return num;
                }
                //Checking Password for lowercase letters
                int lowerCase(string pass)
                {
                    int num = 0;

                    foreach (char ch in pass)
                    {
                        if (char.IsLower(ch))
                        {
                            num++;
                            bolValidLower = true;
                        }
                    }
                    return num;

                }
            do
            {
                //Min pass length declaration
                const int MIN_Length = 12;

                if (Password.Length >= MIN_Length && lengthPass(Password) >= 1 && upperCase(Password) >= 1 && lowerCase(Password) >= 1)
                {
                    Console.WriteLine("The Password is good");
                    bolValidPassword = true;
                    bolValidLength = true;
                    bolValidLower = true;
                    bolValidUpper = true;
                }
                else
                {
                    Console.WriteLine("The Password is not good");
                    Console.WriteLine("Please enter a valid password");
                    Console.WriteLine("Password must be minimum 12 characters long");
                    Console.WriteLine("Password must contain at least one numerical value");
                    Console.WriteLine("Password must contain at least one Upper case character");
                    Console.WriteLine("Password must contain at least one Lower case character");
                }

                if (bolValidLength == false)
                {
                    Console.WriteLine("The Password length is too short");
                }

                if (bolValidUpper == false)
                {
                    Console.WriteLine("The Password did not contain an Uppercase letter");
                }

                if (bolValidLower == false)
                {
                    Console.WriteLine("The Password did not contain a Lowercase letter");
                }
            }
            while (bolValidPass == false);
        }
    }
}
**************************************************************************************

bolValidLength, bolValidUpper, bolValidLower have 'use of unassigned local variable' error

bolValidPass, errors appear as 'does not exist in the current context' and ' is assigned but it's value is never used'

What I have tried:

Any help is much welcomed as i have been stuck on this for the past week. Thank you

推荐答案

// Variable declaration
bool bolValidLength = false;
bool bolValidUpper = false;
bool bolValidLower = false;
bool bolValidPassword = false;

你只需要给你变量在初始化时有些值。编译器不会再抱怨未初始化的变量。



但是代码中还有另一个问题:在C#中,你不能在另一个方法中声明(非匿名)方法。

方法 lengthPass upperCase lowerCase 需要移出 Main 方法。

You just have to give your variables some value at initialization. Compiler will not complain about uninitialized variables anymore.

But there is another problem in your code: in C#, you cannot declare a (non-anonymous) method inside another method.
The methods lengthPass, upperCase and lowerCase need to be moved out of the Main method.


原因很简单:系统可以看到一个可能的在实际为其赋值之前,通过代码路由可能导致变量被使用(即读取或检查)。在实践中路线不太可能,甚至实际上不可能与编译器无关 - 它不会知道其他变量(例如用户输入)中的内容。



例如:

The reason is simple: the system can see a possible route through the code that could result in a variable being used (i.e. read or checked) before you actually assign a value to it. That the route is unlikely, or even not actually possible in practice is irrelevant to the compiler - it doesn't "know" what is going to be in the other variables such as user input.

For example:
string s = "123";
bool b;
foreach (char c in s)
    {
    if (c != 'a') b = true;
    }
if (b) Console.WriteLine("YES");

实际上, b 在循环后总是 true - 但是系统不能依赖于此并告诉您,因为您可以轻松地将其更改为:

In practice, b will always be true after the loop - but the system can't rely on that and tells you off because you could easily change it to this:

string s = Console.ReadLine();
bool b;
foreach (char c in s)
    {
    if (c != 'a') b = true;
    }
if (b) Console.WriteLine("YES");

如果用户只按ENTER,那么 b 永远不会给出值。



所以总是给变量一个默认值:

And if the user just presses ENTER, then b is never given a value.

So always give variables a default value:

bool bolValidLength = false;

你不会有问题。


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

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