Calling a class - c#

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();

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

Arithmetic operator wont register when compiling

Just started kearning c#, and I have been having some trouble when running the console application. For some reason, when I run the console application, it runs the sayHello method, but won't run the Arithmetic method.
Any help would be appreciated.
using System;
class Mainclass {
public static void sayHello(string username, string servername)
{
Console.WriteLine("Could you please enter your name : ");
username = Console.ReadLine();
Console.WriteLine("Hello " + username + "," + " my name is " + servername + ".");
Console.ReadLine();
}
public static float Arithmetic (float value)
{
return value * value;
}
static void Main()
{
Console.WriteLine("Hello, Welcome to the football scout app.");
sayHello((Console.ReadLine()), "Mr. Scout");
Arithmetic(200);
}
It certainly does run the method, you just have not implemented any output.
static void Main()
{
Console.WriteLine("Hello, Welcome to the football scout app.");
sayHello((Console.ReadLine()), "Mr. Scout");
Console.WriteLine("200 squared equals " + Arithmetic(200).ToString());
}

how to have a comma after each string but not in front

I have a problem with my class where it displays a comma before it displays the string and I cant find a way to take the comma out in the front and keep it in all the other places
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Player
{
public string Name { get; set; }
public double SpendingMoney { get; set; }
public string PrizesWon { get; set; }
//constructors
public Player( string n, double sp)
{
Name = n;
SpendingMoney = sp;
PrizesWon = "";
}
//methods
public string Play(GameBooth aGames)
{
string newPrize;
if (SpendingMoney >= aGames.Cost)
{
newPrize = aGames.Start();
//here is what I need to fix
PrizesWon = "," + newPrize + PrizesWon ;
return newPrize;
}
else
{
return "error no money fool";
}
}
}
}
here is the main code if you need it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class Program
{
static void Main(string[] args)
{
//local variables
string name;
double money;
int choice = 0;
string newPrize;
//Game objects.
GameBooth balloonDartToss = new GameBooth(2, "tiger plush", "sticker");
GameBooth ringToss = new GameBooth(2, "bear keychain", "pencil");
GameBooth breakAPlate = new GameBooth(1.5, "pig plush", "plastic dinosaur");
Console.ForegroundColor = ConsoleColor.Cyan;
//asking player name
Console.Write("Welcome to the Carnival. What is your name? ");
name = Console.ReadLine();
//asking how much spending money the player has
Console.Write("How much money do you have? ");
money = Convert.ToDouble(Console.ReadLine());
Console.ResetColor();
//Creating player object.
Player Gambler = new Player(name, money);
//main program loop
while (choice != 4)
{
//print menu. Check out the local method below
printMenu();
//retrieve user choice. See the local method below
choice = getChoice();
//main switch. User Choices:
// 1 - we play Baloon Darts Toss and show prize
// 2 - we play ring Toss and show prize
// 3 - we play Break a Plate and show prize
// 4 - Show users all his prizes
switch (choice)
{
case 1:
newPrize = Gambler.Play(balloonDartToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 2:
newPrize = Gambler.Play(ringToss);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 3:
newPrize = Gambler.Play(breakAPlate);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(newPrize);
break;
case 4:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0}, here is your prize list: {1}", Gambler.Name, Gambler.PrizesWon);
Console.ResetColor();
break;
}//end switch
}//end while
//next line simply pauses the program until you hit Enter.
Console.ReadLine();
}//end main
//this method prints the main menu
public static void printMenu()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
Console.WriteLine("Select the game you would like to play:");
Console.WriteLine("1. Balloon Dart Toss");
Console.WriteLine("2. Ring Toss");
Console.WriteLine("3. Break a Plate");
Console.WriteLine("4. Get Prizes and Leave Carnival");
Console.Write("Enter Your Choice: ");
Console.ResetColor();
}//end printMenu
//this methods accepts user input 1-4
public static int getChoice()
{
Console.ForegroundColor = ConsoleColor.Yellow;
int input = 0;
do
{
Console.ForegroundColor = ConsoleColor.Yellow;
input = Convert.ToInt32(Console.ReadLine());
if (input < 1 || input > 4)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Out of range. Input 1-4 only");
Console.Write("Enter your choice: ");
Console.ResetColor();
}
} while (input < 1 || input > 4);
Console.ResetColor();
return input;
}
}
}
here is my other class if you need that too but you probably don't
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Carnival
{
class GameBooth
{
//properties
public double Cost { get; set; }
public string FirstPrize { get; set; }
public string ConsolationPrize { get; set; }
public int w { get; set; }
public int l { get; set; }
//constructors
public GameBooth(double c, string fp, string cp)
{
Cost = c;
FirstPrize = fp;
ConsolationPrize = cp;
}
//methods
public string Start()
{
Random r = new Random();
w = 1;
l = 1;
for (int i = 1; i < 3; i++)
{
int randomChoice = r.Next(0, 400);
if ( randomChoice == 1)
{
w++;
}
}
if (w == 3)
{
return FirstPrize;
}
else
{
return ConsolationPrize;
}
}
}
}
Update the line to:
PrizesWon = string.IsNullOrEmpty(PrizesWon) ? newPrize : newPrize + "," + PrizesWon;
This uses the conditional operator to return a different result depending on whether or not PrizesWon already has something in it.
In your code you had put (,) before newPrize. For solving this, you have to just put it after PrizesWon variable and it will look like this :
**PrizesWon = newPrize + PrizesWon + ",";**
I hope your problem will be solved by this.

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.

Displaying user name with the help of a method in 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);

Categories