Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 25-May-19 6:08am
v5
Comments
phil.o 25-May-19 11:55am    
What you have done is so abusive! Please do not delete your question once answers have been provided. This renders the post useless, and is a mark of profund disrespect to other users of this forum.
Richard MacCutchan 25-May-19 12:08pm    
Fixed.
phil.o 25-May-19 12:36pm    
Thank you Richard.

C#
// Variable declaration
bool bolValidLength = false;
bool bolValidUpper = false;
bool bolValidLower = false;
bool bolValidPassword = false;
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.
 
Share this answer
 
Comments
OriginalGriff 24-May-19 6:21am    
"in C#, you cannot declare a (non-anonymous) method inside another method."
Yes, you can:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

I don't like it, but it was added at C# 7.0
phil.o 24-May-19 6:31am    
Thanks for the info, I don't like it either. Makes me feel like C# is becoming a giant pile of mess.
Chances are, OP just copied some javascript validation code and does not use C# 7.0 "features" yet.
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:
C#
string s = "123";
bool b;
foreach (char c in s)
    {
    if (c != 'a') b = true;
    }
if (b) Console.WriteLine("YES");
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:
C#
string s = Console.ReadLine();
bool b;
foreach (char c in s)
    {
    if (c != 'a') b = true;
    }
if (b) Console.WriteLine("YES");
And if the user just presses ENTER, then b is never given a value.

So always give variables a default value:
C#
bool bolValidLength = false;
and you won't have a problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900