This question already has answers here:
Use a variable from another method in C#
(2 answers)
Closed 1 year ago.
Hey I'm very very new to c# coding and OOP, I'm currently working on a system that allows users to create login details, and then login with said details, however when I get to the part where I verify the users details I am having issues getting it to recognize my object, AccName, credentialCheck() can't successfully get the information from accountCreation(), I know this is a very basic thing to know but I have tried so many things and none of them have worked, or I haven't been doing them properly. My code is shown below and any guidance that could be provided would be greatly appreciated.
static void accountCreation()
{
Console.Write("Username: ");
string userNameInput = Console.ReadLine();
Console.Write("Email: ");
string userEmailInput = Console.ReadLine();
Console.Write("Password: ");
string userPasswordInput = Console.ReadLine();
Account AccName = new Account($"{userNameInput}", $"{userEmailInput}", $"{userPasswordInput}");
Console.Write($"{userNameInput} Registered successfully");
Console.WriteLine();
}
static void credentialCheck()
{
Console.Write("Email: ");
string userEmailCheck = Console.ReadLine();
Console.Write("Password: ");
string userPasswordCheck = Console.ReadLine();
if (userEmailCheck == AccName.userEmail )
{
Console.WriteLine();
Console.WriteLine("Success");
}
I suppose you are calling credentialCheck() just after accountCreation() and in the same thread. That out of the way, I supposed you have accName as a static variable and your class overall looks like this:
class Login {
protected static Account accName;
static void accountCreation()
{
// ...
accName = new Account(userNameInput, userEmailInput, userPasswordInput);
}
}
This means the variable accName remains as far as the execution context is concerned and not immediately destroyed after accountCreation() returns.
Related
I have coded a system based of the theory that while loops can work anywhere in a system. So once a while loop has been passed by it can run. Here is a simple version of my code:
using System;
namespace test___login
{
class Program
{
static void Main(string[] args)
{
string location = "homepage";
while (location.Equals("Homepage", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("homepage");
Console.WriteLine("WHere to now: ");
location = Console.ReadLine();
}
while (location.Equals("login", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("login");
Console.WriteLine("Where to now: ");
location = Console.ReadLine();
}
}
}
}
So I assumed that once the variable changes from "login" to "homepage" in the second while loop, would mean the first while loop would run once again. Is my theory on how the code works wrong or am i simply not typing the right thing. I just started last week so sorry for the basic question
It wouldn't run again, because the code has already executed past that line so unless you call it again, the next thing down the line will execute, in your case nothing - this is called procedural programming.
Why don't you make one while loop, and a switch statement instead.
bool run = true;
while (run)
{
Console.WriteLine("WHere to now: ");
string location = Console.ReadLine();
switch(location.ToLower()){
case "homepage":
Console.WriteLine("HomePage");
break;
default:
run = false;
break;
}
}
Not with the way you have your code currently structured. It will exit the application. You need to surround all of that with one more while loop, which can be similar to your other loops. See below:
using System;
namespace test___login {
class Program
{
static void Main(string[] args)
{
string location = "homepage";
while (!location.ToUpper().Equals("EXIT"))
{
while (location.Equals("Homepage", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("homepage");
Console.WriteLine("WHere to now: ");
location = Console.ReadLine();
}
while (location.Equals("login", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("login");
Console.WriteLine("Where to now: ");
location = Console.ReadLine();
}
}
}
}
}
This question already has answers here:
How can I run both of these methods 'at the same time' in .NET 4.5?
(5 answers)
Closed 4 years ago.
I'm trying to make a log in which has two validation methods to check who logged in. Now I have a problem where I need to call two methods at the same time. The code for now is like this, any help is appreciated.
public void Log()
{
Login loginAccount = new Login();
Console.WriteLine("Enter username: \n");
loginAccount.Username = Console.ReadLine();
Console.WriteLine("Enter password: \n");
loginAccount.Password = Console.ReadLine();
List<Login> UserLog = new List<Login>()
{
loginAccount
};
loginAccount.CheckUser(loginAccount.Username);
loginAccount.CheckAdmin(loginAccount.Username);
}
You may looking for something like this
Task.Run(() => loginAccount.CheckUser(loginAccount.Username));
Task.Run(() => loginAccount.CheckAdmin(loginAccount.Username));
This question already has answers here:
What's does the dollar sign ($"string") do? [duplicate]
(2 answers)
Closed 4 years ago.
this might be a trivial question for the experienced programmers, however I came across this line of code and want to understand what it does.
please see the code below where we are adding the in the entries list and also incrementing the count as well.
public class Journal
{
private readonly List<string> entries = new List<string>();
private static int count = 0;
public int AddEntry(string text)
{
entries.Add($"{++count}: {text}"); // my question is about this line of code
return count;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
what is the purpose of doller sign in the mentioned line.
it is used for string interpolation,
with it you can write something like:
Console.WriteLine($"Test, this is your message: {message}");
instead of using the older command and syntax:
Console.WriteLine(String.Format("Test, this is your message: {0}", message));
I am currently Trying to learn C#, I am coming from another Language so learning C# has been a breeze (so far). However, I was creating a console application in which the user enters a username and password then they get logged in. When they are logged in, I simply ask them for what they want and they type in their request. Now the thing is, whenever they pass the if/else if/else statements, I move them to a new method that greets them with a "Hello, welcome, Please enter a request". after they put in the request, I want the console to only say "Please enter a request". Thus I want to check if the method is being ran for the first time. Any ideas on how to do it.
I think this should do it
using System;
namespace ConsoleApplication1
{
class Program
{
static bool hasRun = false;
static void Main(string[] args)
{
GreetUser();// first call
GreetUser();// second call
}
private static void GreetUser()
{
var message = "Hello, welcome. Please enter a request: ";
// could be refactored to
if (hasRun)
{
message = "Please enter a request: ";
}
Console.WriteLine(message);
var requestText = Console.ReadLine();
hasRun = true;
}
}
}
first call to GreetUser() will be:
Hello, welcome. Please enter a request:
second and subsequent calls to GreetUser() will be:
Please enter a request:
EDIT:
Oh one thing I forgot is that you could further refactor the message using ternary operator to this:
private static void GreetUser()
{
var message = (!hasRun ? "Hello, welcome. " : string.Empty) + "Please enter a request: ";
Console.WriteLine(message);
var requestText = Console.ReadLine();
hasRun = true;
}
Turns 4 lines of code into 1
Keep getting the error message does not contain a static main method for suitable entry point. Would anyone be able to explain this error to me and possibly help me fix it? Thanks. New to C#
{
class Authenticator
{
private Dictionary<string, string> dictionary = new Dictionary<string, string>();
public void IntialValues()
{
dictionary.Add("username1", "password1");
dictionary.Add("username2", "password2");
dictionary.Add("username3", "password3");
dictionary.Add("username4", "password4");
dictionary.Add("username5", "password5");
}
public bool Authenticate(Boolean authenticated)
{
Console.WriteLine("Please enter a username");
string inputUsername = Console.ReadLine();
Console.WriteLine("Please enter your password");
string inputPassword = Console.ReadLine();
if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
{
authenticated = true;
}
else
{
authenticated = false;
}
return authenticated;
}
}
}
If all of your code consists only of the block shown above then the error is more than clear. A Main method is required in your program.
The Main method is, by convention, the default point where the code starts executing. You can get here a more in depth explanation
So, for example, you need to add the following code
class Authenticator
{
static void Main(string[] args)
{
Authenticator au = new Authenticator();
au.InitialValues();
if(au.Authenticate())
Console.WriteLine("Authenticated");
else
Console.WriteLine("NOT Authenticated");
Console.WriteLine("Press Enter to end");
Console.ReadLine();
}
// Move the boolen variable inside the method
public bool Authenticate()
{
bool authenticated = false;
Console.WriteLine("Please enter a username");
string inputUsername = Console.ReadLine();
Console.WriteLine("Please enter your password");
string inputPassword = Console.ReadLine();
if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
{
authenticated = true;
}
else
{
authenticated = false;
}
return authenticated;
}
}
By the way, you should remove the parameter passed in input to the Authenticate method. You should declare it as an internal variable, set it depending on the outcome of the check and return it.
However, you could remove that variable altogether writing
....
return (dictionary.ContainsKey(inputUsername)) &&
(dictionary[inputUsername] == inputPassword)
}
All executable programs must have a Main function somewhere in the project that is compiled to the exe.
If you just want to compile a class (eg to a dll) then you have to set that as the "project type" in visual studio.
The easiest way is to create a new project, but select class library as project type, and then paste your code in there. Alternatively you can use the command line to compile a file to a dll like so:
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library Authenticator.cs