Displaying user name with the help of a method in C# - c#

Cant't figure out what I am doing wrong,want to display user name in console through a method in the same class,missing a minor syntax please have a look and guide me.
public class Program
{
string name;
public void GetName()
{
Console.WriteLine("Enter ur name");
name = Console.ReadLine();
Console.WriteLine("Name is",name);
Console.ReadLine();
}
static void Main()
{
Program p = new Program();
p.GetName();
Console.ReadLine();
}
}

You need to specify the format string try:
public class Program
{
string name;
public void GetName()
{
Console.WriteLine("Enter ur name");
name = Console.ReadLine();
Console.WriteLine("Name is {0}",name);
Console.ReadLine();
}
static void Main()
{
Program p = new Program();
p.GetName();
Console.ReadLine();
}
}

You can also do it like this
Console.Writeline("name is:" + name);

Console.WriteLine("Name is : " + name);

Related

Error on line 117 exception unhandled

What am i doing wrong? I am trying to have my program read the data from a csv file but i keep getting this unhandled exception, is it because of how i wrote the txt file? the exception?
First I tried to have the data be displayed as a list in the console, but that did not happened either. I cant seem to be able to do that here or even get it right to read from my txt file.
//My first program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticeProgram
{
public class Program
{
public static bool W2 { get; private set; }
public static void Main(string[] args)
{
List<Person> myContacts = new List<Person>();
// Loop to collect developers data
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Welcome developer, enter your name to continue");
string name = Console.ReadLine();
Console.WriteLine("Hello, " + name);
Console.WriteLine("enter your address");
string address = Console.ReadLine();
Console.WriteLine(address);
Console.WriteLine("enter your monthly income");
double GrossMonthlyPay = 5000;
Console.WriteLine(GrossMonthlyPay);
Console.ReadLine();
Console.WriteLine("your tax deduction is set at 7%");
double taxes = (0.07);
Console.WriteLine(taxes);
Console.ReadLine();
Console.WriteLine("your monthly tax deduction is ");
Console.WriteLine(taxes = GrossMonthlyPay * taxes);
Console.ReadLine();
Console.WriteLine("Based on your monthly income your Annual gross pay is ");
double AnnualGrossPay = GrossMonthlyPay * 12;
Console.WriteLine(AnnualGrossPay);
Console.ReadLine();
Console.WriteLine("Your annual taxes are ");
double AnnualTaxes = AnnualGrossPay * 0.07;
Console.WriteLine(AnnualTaxes);
Console.ReadLine();
Console.WriteLine("Select 1 for W2, select 2 for 1099");
bool A = W2;
Console.WriteLine();
Console.ReadLine();
if (A)
{
Console.WriteLine("You employment type is W2");
}
else
{
Console.WriteLine("Your employment type is 1099");
}
Console.Write("\nPress any key to continue...");
Console.ReadLine();
myContacts.Add(new Person(name, address, GrossMonthlyPay, taxes, AnnualGrossPay, AnnualTaxes));
}
// Or you could read from CSV or any other source
foreach (string line in System.IO.File.ReadLines(#"C:\Users\Owner\Documents\Visual Studio 2015\Projects\new\data.txt"))
{
myContacts.Add(Person.ReadFromCSV(line));
}
// Serialize your informations at the end
foreach (Person contact in myContacts)
{
contact.SerializeToCSV(#"C:\Users\Owner\Documents\Visual Studio 2015\Projects\new\data.txt");
}
}
}
internal class Person
{
private string address;
private double grossMonthlyPay;
private string name;
private double taxes;
private double annualGrossPay;
private double annualTaxes;
public Person(string name, string address, double grossMonthlyPay, double taxes)
{
this.name = name;
this.address = address;
this.grossMonthlyPay = grossMonthlyPay;
this.taxes = taxes;
}
public Person(string name, string address, double grossMonthlyPay, double taxes, double annualGrossPay, double annualTaxes) : this(name, address, grossMonthlyPay, taxes)
{
this.annualGrossPay = annualGrossPay;
this.annualTaxes = annualTaxes;
}
public override string ToString()
{
return "Name=" + this.name + ",Address =" + this.address + " Monthly Pay=" + this.grossMonthlyPay + " Taxes=" + this.taxes.ToString();
}
internal static Person ReadFromCSV(string line)
{
throw new NotImplementedException();
}
internal void SerializeToCSV(string v)
{
throw new NotImplementedException();
}
}
}
this is the error
Your function "ReadFromCSV" implement a
throw new NotImplementedException();
try to delete it or implement with the right code

How to call a method that takes multiple parameters in c#

i am new at c# and i have a problem in this small program
i want to return the entered information in method ClientsDetails to use them in method Print().
Any help plz ?
public static void Main(string[] args)
{
ClientsDetails();
Print(???,???,???);
Console.ReadLine();
}
public static void ClientsDetails()
{
Console.Write("Client's first name: ");
string firstName = Console.ReadLine();
Console.Write("Client's last name: ");
string lastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
string birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine("Client : {0} {1} was born on: {2}", first, last, Birthday);
}
}
You could use a struct:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
struct Person{
public static FirstName;
public static LastName;
public static Birthday;
}
class Program
{
static void Main(string[] args)
{
Person person = ClientsDetails();
Print(person.FirstName, person.LastName, person.Birthday);
Console.ReadKey();
}
public static Person ClientsDetails()
{
Person returnValue = new Person();
Console.Write("Client's first name: ");
returnValue.FirstName = Console.ReadLine();
Console.Write("Client's last name: ");
returnValue.LastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
returnValue.Birthday = Console.ReadLine();
return returnValue;
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
Or an array:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string person = ClientsDetails();
Print(person[0], person[1], person[2]);
Console.ReadKey();
}
public static string[] ClientsDetails()
{
string[] returnValue = new string[3];
Console.Write("Client's first name: ");
returnValue[0] = Console.ReadLine();
Console.Write("Client's last name: ");
returnValue[1] = Console.ReadLine();
Console.Write("Client's birthdate: ");
returnValue[3] = Console.ReadLine();
return returnValue;
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
Or references (pass by reference):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string firstName, lastName, birthday;
ClientsDetails(ref firstName, ref lastName, ref birthday);
Print(firstName, lastName, birthday);
Console.ReadKey();
}
public static void ClientsDetails(ref string firstName, ref string lastName, ref string birthday)
{
Console.Write("Client's first name: ");
firstName = Console.ReadLine();
Console.Write("Client's last name: ");
lastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
I just corrected your program as your requirement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static String FirstName;
private static String LastName;
private static String Birthday;
static void Main(string[] args)
{
ClientsDetails();
Print(FirstName, LastName, Birthday);
Console.ReadKey();
}
public static void ClientsDetails()
{
Console.Write("Client's first name: ");
FirstName = Console.ReadLine();
Console.Write("Client's last name: ");
LastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
Birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, Birthday));
}
}
}
There is a heap of way that you can pass the required arguments to your method, so for example simply you can do it like this:
String f = "first string";
String l = "last string";
String b = "birthday string";
Print(f,l,b);
BTW, in your case it seems that you want the user's inputs to be passed to the Print method, so a simple way is to just call the Print method inside your ClientsDetails method like this:
Print(firstName, lastName, birthday);
For a comprehensive resource about this, you can refer to the docs, as always. At the moment you can just neglect the Async Methods part.

Getter returns no value

I'm having trouble using setters and getters. When I tried to call a private var from a class to another class, I do not get the value of the var.
Here are some snippets of my code for you guys to check out.
This is my setter getter for the variable.
class PlayerStats
private string _pName;
public string pName
{ get { return _pName; } set { _pName = value; } }
}
This is where I want to show my var.
public void Welcome()
{
PlayerStats pStats = new PlayerStats();
Header();
Console.WriteLine("Hello!");
Console.WriteLine(pStats.pName);
}
This is the method where I inserted a value on my var (this method is executed first before the method welcome)
public void Username()
{
PlayerStats pStats = new PlayerStats();
string name;
Header();
Console.Write("\nChoose your USERNAME: ");
name = Console.ReadLine();
pStats.pName = name;
}
None of this are running from the main method. I thought of doing my program by just calling out different methods from different classes in the main method so it kinda looks like this:
static void Main(string[] args)
{
Jobs jobCl = new Jobs();
GUI gui = new GUI();
gui.Header();
gui.StartPage();
gui.Username();
gui.ChoosepJob();
gui.Welcome();
Console.ReadLine();
}
When I call the var from the username method, I have no problem printing it but I cant make it to print if I am to call it from other methods.
Thank you for any help you can provide. Also if you can suggest a different way of doing this, please don't hesitate to say it to me
Don't make a new PlayerStats every method, make it once outside of the methods then pass it in to each one. You likely have the same problem with Jobs and that likely needs to be passed in to ChoosepJob
static void Main(string[] args)
{
PlayerStats pStats = new PlayerStats();
Jobs jobCl = new Jobs();
GUI gui = new GUI();
gui.Header();
gui.StartPage();
gui.Username(pStats);
gui.ChoosepJob(jobCl);
gui.Welcome(pStats);
Console.ReadLine();
}
public void Welcome(PlayerStats pStats)
{
Header();
Console.WriteLine("Hello!");
Console.WriteLine(pStats.pName);
}
public void Username(PlayerStats pStats)
{
string name;
Header();
Console.Write("\nChoose your USERNAME: ");
name = Console.ReadLine();
pStats.pName = name;
}
Your printing method here is creating a new instance of PlayerStats
public void Welcome()
{
PlayerStats pStats = new PlayerStats();
Header();
Console.WriteLine("Hello!");
Console.WriteLine(pStats.pName);
}
The one you're creating in here is not being used in Welcome() method
public void Username()
{
PlayerStats pStats = new PlayerStats();
string name;
Header();
Console.Write("\nChoose your USERNAME: ");
name = Console.ReadLine();
pStats.pName = name;
}
Create one instance in your main to follow through like the following:
public string Username()
{
Header();
Console.Write("\nChoose your USERNAME: ");
name = Console.ReadLine();
return name;
}
public void Welcome(PlayerStats pStats)
{
Header();
Console.WriteLine("Hello!");
Console.WriteLine(pStats.pName);
}
static void Main(string[] args)
{
PlayerStats pStats = new PlayerStats();
Jobs jobCl = new Jobs();
GUI gui = new GUI();
gui.Header();
gui.StartPage();
pStats.pName = gui.Username();
gui.ChoosepJob();
gui.Welcome(pStats);
Console.ReadLine();
}

Taking a user inputed string and using that string to create an object

So I've been looking into this for what feel like forever and can not figure out what I'm doing wrong. here are excerpts from my code where I want the string CashierLOgInNName's value to be the new Cashier objects name:
public class Cashiers
{
public int CashierID;
public int Password;
public string FirstName;
public string LastName;
public void SetCashiers(int CashierID, int Password,string FirstName, string LastName )
{
this.CashierID = CashierID;
this.Password = Password;
this.FirstName=FirstName;
this.LastName = LastName;
}
Console.WriteLine("enter New Log in name");
string CashierLOgInNName= Console.ReadLine();
Console.WriteLine("enter First Name");
string CashierFirstName = Console.ReadLine();
Console.WriteLine("enter Last name");
string CashierLastName = Console.ReadLine();
Console.WriteLine("enter Cashier ID");
string f = Console.ReadLine();
int NewCashierID = Int32.Parse(f);
Console.WriteLine("enter Cashier Password");
string g = Console.ReadLine();
int NewCashierPWD = Int32.Parse(g);
CashierLOgInNName = (Cashiers)Activator.CreateInstance(null, CashierLOgInNName).Unwrap();
First a little correction. It will make sure the Cashier's Object cashierObj is created.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Activators
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter New Log in name");
string CashierLOgInNName = Console.ReadLine();
Console.WriteLine("enter First Name");
string CashierFirstName = Console.ReadLine();
Console.WriteLine("enter Last name");
string CashierLastName = Console.ReadLine();
Console.WriteLine("enter Cashier ID");
string f = Console.ReadLine();
int NewCashierID = Int32.Parse(f);
Console.WriteLine("enter Cashier Password");
string g = Console.ReadLine();
Cashiers cashierObj = (Cashiers)Activator.CreateInstance(typeof(Cashiers), f, g, CashierFirstName, CashierLastName);
}
}
public class Cashiers
{
public string CashierID;
public string Password;
public string FirstName;
public string LastName;
public Cashiers(string CashierID, string Password, string FirstName, string LastName)
{
this.CashierID = CashierID;
this.Password = Password;
this.FirstName = FirstName;
this.LastName = LastName;
}
}
}
Coming to the next part CashierLOgInNName's value to be the new Cashier objects name. This seems to be tricky and non useful. I suggest there are better alternatives :
You can define property loginName in Cashiers.
You can create a Dictionary<string, Cashier> LoginName as key and Cashier Object as value.
But still if you persist there is a way using CSharpCodeProvider Class which allows dynamically creating an assembly from source code.
In source code you can write the object name as loginName and then refer that assembly dynamically.
Yet it serves you no purpose as you will not be able to use that object as its name is known to you at run time only. I hope it helps your curiosity.
β€œIn a time of destruction, create something.” ― Maxine Hong Kingston
The MSDN documentation of activator.createinstance and objecthandle.unwrap might be of help.
Activator.CreateInstance Method
ObjectHandle.Unwrap Method

Calling a class

How would I go about calling a function from another class in a if statement. For example i have a menu and I want to be able to to display the user choice:
namespace _5049COMP_OO
{
class Interface
{
public string menuChoice;
//This creates the loadup menu.
public void menu()
{
DrawLine();
Console.Write(" Welcome to BOSS eAuctions! ");
DrawLine();
Console.WriteLine("1. Browse");
Console.WriteLine("2. Login");
Console.WriteLine("3. Register");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine("4. Quit ");
DrawLine();
Console.Write("Please select on of the options:");
menuChoice = Console.ReadLine();
DrawLine();
}
// Create the login menu
public void LoginMenu(string username, string password)
{
DrawLine();
Console.Write(" Login! ");
DrawLine();
Console.WriteLine("Username:");
Console.WriteLine("Password");
username = Console.ReadLine();
password = Console.ReadLine();
DrawLine();
DrawLine();
}
I want the statment in "Public void Choice()
namespace _5049COMP_OO
{
class Functions
{
public void Choice()
{
}
}
}
You need an instance of the class or it needs to be a static method.
At the top of your Interface file, add a using statement for the Functions namespace.
If you make Choice() a static method, you can do
Functions.Choice();
Otherwise you'll need
var f = new Functions();
f.Choice();

Categories