HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Login and registration functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andfunctionsloginregistration

Problem

I'm currently learning C# as first programming language and I've made a simple program to exercise the basic concepts. It's nothing fancy especially compared to advanced stuff you normally talk about, but I would like to know if I've made some common mistakes or I missed some good practices.

Basically I created a log-in and register functions that, once logged in, gives access to some simple apps like a calculator. I have to work out a way to store the registration data and eventually add more apps, but as I said it doesn't have a real utility outside making some practice with coding so for now it's just fine that it works as I intended.

```
class MainClass
{
public static List nomi = new List();
public static List password = new List ();
public static bool startup = true;
public static bool logged = false;

public static void Main (string[] args)
{

Start ();

}

//Starting loop
public static void Start()
{
string choice;

while (startup == true)
{

Console.WriteLine ("Do you want to login or register?");

choice = Console.ReadLine ();

switch (choice)
{
case "register":
Register ();
break;
case "login":
Login (ref logged, out startup);
break;
default:
Console.WriteLine ("Error, please repeat.");
Console.WriteLine ();
break;
}

}

}

//Logged in loop
public static void Run()
{

string app;

while (logged == true)
{
Console.WriteLine ("Choose an app: Calculator, Logout, Exit.");
app = Console.ReadLine ();

if (app == "calculator") {
Calc ();
} else if (app == "check") {
Check();
}else if (app == "exit") {
Console.WriteLine ("Good

Solution

I have a few observations to make, which hopefully will help you out

-
In the start method you have a switch on the value of choice, now string comparison in c# by default is case sensitive and so you will only get the desired behaviour if the user types in lower case. The easiest way round this is to make use of the ToUpper or ToLower methods on the string which will give you a guaranteed case. Or if comparing strings follow the example shown here.

-
If you have defined logged as globally accessible in the class, why are you passing it from methods to other methods by reference. As its defined with a global scope it is not necessary to pass it to make use of it.

Some people prefer not to declare variables with global scope and would instead pass it round. In this case I would say a global scope is the best way though.

-
In the run method I would have used a switch rather than the multiple nested if and else if as it produces more easily readable code.

Hope those observations proove useful.

Context

StackExchange Code Review Q#85411, answer score: 5

Revisions (0)

No revisions yet.