I am trying to create a console application that converts centimeters to meters
Here are my objectives
Store number of centimeters entered in an attribute
Use a default constructor to store zero in the attribute that stores the number of centimeters meters entered
Use a primary constructor to accept and store number of centimeters entered.
A function call getMeters to return the number of meters
A function called Remainder to get the number of centimeter remanding after conversion
A function called Printout that will display the results
The application should carry on accepting values for conversion until the user decides to end it.
What I have so far:
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
//choice = Console.ReadLine();
choice = Console.ReadKey().KeyChar;
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout();
}
while(choice.CompareTo(n) == 0);
}
public static void getMeterst()
{
int meters = c.cmvar / 100;
}
public static void Remainder ()
{
int cmremain = c.cmvar % 100;
}
public static void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", meters, cmremain);
}
}
}
I am getting errors regarding:
prog.cs(24,5): warning CS0168: The variable `meters' is declared but never used
prog.cs(41,11): error CS0103: The name `c' does not exist in the current context
prog.cs(41,2): error CS0103: The name `meters' does not exist in the current context
prog.cs(47,24): error CS0103: The name `c' does not exist in the current context
prog.cs(53,61): error CS0103: The name `meters' does not exist in the current context
prog.cs(53,69): error CS0103: The name `cmremain' does not exist in the current contex
Any help would be appreciated.
In some programming languages, a context is usually defined by { and }, meaning that given this:
{
int a = ...
}
a exists specifically within that block. Assuming that no other variable named a has been declared outside the braces, doing something like so:
{
int a = ...
}
print(a)
Will result in a fault, since a no longer exists.
In your case for instance, you are declaring the following variable: Centimeter c = new Centimeter();. Notice that this is enclosed within the do...while scope, so it exists only in there. Thus, when you try to access your variable from another method, which has its own scope, you get the exception you are getting.
To start solving the issue, you should move the 3 methods getMeterst, Remainder and Printout in their appropriate class, which is Centimeter.
I would recommend you start by looking at some tutorials, since you have other issues with your code.
As pointed out by #user2864740, different languages treat scopes differently. Taking Javascript in consideration:
function hello()
{
{
var i = 44;
}
{
alert(i);
}
}
Yields an alert with the value of 44.
However, the code below does not compile in C#:
private static void Hello()
{
{
int i = 0;
}
{
Console.WriteLine(i); //The name i does not exist in the current context.
}
}
you've got a lot of issues with scope in your code, when you declare something, it can (as a rule) only be accessed inside whatever set of brackets you declare it in, so when you try and access c and cmremainand things like that without specifying their location or accessing them correctly you get errors like this.
I have working code below, but feel free to ask any extra questions as to 'why' this works.
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout(c);
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
}
while (choice != n);
}
public static int getMeters(Centimeter c)
{
int meters = c.cmvar / 100;
return meters;
}
public static int Remainder(Centimeter c)
{
int cmremain = c.cmvar % 100;
return cmremain;
}
public static void Printout(Centimeter c)
{
Console.WriteLine("{0} Meters and {1} Centimeters", getMeters(c), Remainder(c));
}
}
}
you need to learn oriented object programming before programming any oop language.
Basic is good but polute your mind by not thinking object, but sequencial...
Here is your code fixed
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
public Centimeter(int mm)
{
cmvar = mm;
}
public int getMeterst()
{
return cmvar / 100;
}
public int Remainder()
{
return cmvar % 100;
}
public void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", this.getMeterst(), this.Remainder());
}
}
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine(); // for pure design needs
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
c.Printout();
}
while (choice != n);
}
}
}
Related
This question already has answers here:
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
(9 answers)
Closed 3 years ago.
I'm still learning to code so if there are some issues, please tell me! This is for a school essay.
I'm writing a code that asks some information about a game: game name, the devs behind it, the publisher, and its cost.
The essay has multiple different tasks, like: make a program that uses get set, arrays, methods, or and so on. I made it all into a program, and I got stuck with the error
CS0120 An object reference is required for the non-static field, method, or property 'Program.gameInfoArray'
The code:
class Program
{
//the class array that stores the games
public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
bool done = false;
// this is where i get the issue
for (int i = 0; i >= gameInfoArray.Length || done == false; i++)
{
//asks what the game is called
Console.WriteLine("What is the game called:");
string gameName = Console.ReadLine();
//asks who the devolopers are
Console.WriteLine("Who where the devolopers behind the game:");
string devoloper = Console.ReadLine();
//asks who published the game
Console.WriteLine("Who released the game:");
string publisher = Console.ReadLine();
//ask how much the game costs
Console.WriteLine("How much does the game cost:");
string cost = Console.ReadLine();
//inputs the information in to the class array, this is also where i get the issue
gameInfoArray[i].gameInformationGrabber(gameName, devoloper, publisher, cost);
//asks if the
Console.WriteLine("are there any more games? Y/N");
while(true)
{
ConsoleKeyInfo yesOrNo = Console.ReadKey();
if ((yesOrNo.KeyChar == 'Y') || (yesOrNo.KeyChar == 'y'))
{
done = true;
break;
}
else if ((yesOrNo.KeyChar == 'N') || (yesOrNo.KeyChar == 'n'))
{
break;
}
}
}
}
}
The script:
class gameInfo
{
private string gameName;
private string devoloper;
private string publisher;
private string cost;
public void gameInformationGrabber(string game, string dev, string publisher, string cost)
{
theGameName = game;
theDevs = dev;
thePublisher = publisher;
theCost = cost;
}
public string theGameName
{
get { return gameName; }
set { gameName = value; }
}
public string theDevs
{
get { return devoloper; }
set { devoloper = value; }
}
public string thePublisher
{
get { return publisher; }
set { publisher = value; }
}
public string theCost
{
get { return cost; }
set { cost = value; }
}
}
Thanks a ton in advance. Sorry if I messed up big time somewhere in the code.
You've got two comment, it is perfect solution for you.
I hope you spend a time to get a basic concept from it.
However, this will help you.
1. Static only
This is a solution mentioned comment.
class Program
{
// Add static keyword. Because Main() method is static.
// So, every variable inside it should be static.
// public gameInfo[] gameInfoArray = new gameInfo[10];
public static gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
......
}
}
2. Work with local variable
class Program
{
//public gameInfo[] gameInfoArray = new gameInfo[10];
static void Main(string[] args)
{
// Declare as local variable.
// In static method will now complain using local variable.
gameInfo[] gameInfoArray = new gameInfo[10];
......
}
}
I've tried to run this code, plase don't forget initialize gameInfoArray to prevent null reference exception.
you may need this before entering for loop,
for (int index = 0; index < gameInfoArray.Length; index++)
{
gameInfoArray[index] = new gameInfo();
}
I am trying to solve or be pointed in the right direction. I am having difficulty determining where to place my Area formula in the Triangle Class (not the main). Area can only have a 'get' and not a 'set'.
Next issue is identifying the type of triangle based on the inputed side and if it is a 'right' triangle, appending the 'type' with '-right' for example (isosceles-right). I have an enum for the triangle types.
I'm not looking for the direct answer to solve this but rather some help and coaching to help better build my skills
Here is the class structure I have generated so far in C#, please keep in mind it is not complete.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriangleCheck;
namespace TriangleCheck
{
public class Triangle
{
private StringBuilder _ErrorMsg;
private int[] _Sides;
private const int _nSides = 3;
private int _Area;
public Triangle(int[] Sides)
{
//Track amunt of errors recieved.
int nErrors = 0;
//Make sure ErrorMsg is cleared
_ErrorMsg = new StringBuilder();
//Did I get _nSides? If not, append to ErrorMsg and throw exception
if(Sides.Length != _nSides)
{
_ErrorMsg.Append(string.Format("Expected {0} sides but recieved {1}", _nSides, Sides.Length));
nErrors += 1;
}
//Is each side positive? If not, append to ErrorMsg and throw exception
for (int i = 0; i < Sides.Length; i++)
{
if (Sides[i] <= 0)
{
_ErrorMsg.Append(string.Format("{0} side is not a postive integer", Sides[i]));
nErrors += 1;
}
}
//Set input from user to private property _Sides
_Sides = Sides;
_Area = Area;
}
public int Area
{
get { return _Area; }
private set
{
int parameter =
}
}
public string ErrorMsg
{
get
{ return ErrorMsg.ToString();}
}
public bool IsRight
{
get
{
return ;
}
}
public int Sides
{
get
{ return _Sides; }
set
{
if (value > 0)
{
_Sides = value;
}
else
throw new ArgumentOutOfRangeException("Value must be postive!");
}
}
public TriangleTypes TriangleTypes
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
public void ScaleUp(int[] ScaleFactor)
{
throw new System.NotImplementedException();
}
public override string ToString()
{
return "A Triangle with sides " + _Sides + " is Type: " + TriangleTypes + " with Area:" + Area;
}
}
}
You mention that you can't set the Area property... it looks like you're trying to enforce that by making a private set, but why not just exclude the set leaving it as a read-only property?
The Area formula could go a couple places; the key is that it is derived from the sides but only matters when someone asks for it. So you could reasonably:
Apply the formula and update internal state every time sides changes
Apply the formula and return the value every time someone does a get operation on Area
Remember the point of getter and setter being functions is that they could contain logic to execute (to fully update internal state in setter, or to calculate the value of a read-only derived property).
More sophisticated patterns exist if performance of the area calculation were very worrisome, but I wouldn't get into that at this point.
As for determining if the triangle is right... if it is, which side must be the hypotenuse? What relationship do you know between the length of the hypotenuse and the lengths of the other sides, if the triangle is right?
using System;
namespace ConsoleApp
{
class Program
{
static void Main()
{
var t = new Triangle(2, 3, 5);
//var Triangle = new Triangle(2); // won't compile as no Triangle constructor can be found that takes 1 integer
//var Triangle = new Triangle(2, 3, 5, 7); // won't compile as no Triangle constructor can be found that takes 4 integers
//var Triangle = new Triangle(2, -3, 5); // won't compile as the 2nd value is negative - and we've asked for unsigned for all 3 values
Console.WriteLine("The triangle ({0}, {1}, {2}) has an area of {3}.", t.A, t.B, t.C, t.area());
Console.ReadKey();
}
}
public class Triangle
{
public uint A { get; set; }
public uint B { get; set; }
public uint C { get; set; }
public Triangle(uint a, uint b, uint c)
{
this.A = a;
this.B = b;
this.C = c;
}
public uint area()
{
return A * B * C; // this needs fixing ...
}
}
}
Isn't this roughly what you are trying to achieve with your Triangle class - a way of stopping it being used incorrectly with too few or incorrect types of arguments. This one only allows 3 positive (uint) integers. Nothing else will comple - which is what you want. Sorry if I have misunderstood.
So Im learning basic C# and wonders how I can return x and y from AGE(); and NUMBER(); and use those variables in Name();
Right now x and y in the Name(); parameters are understandably wrong since there are no local variables in the constructor. But I returned x and y from the function. So how can I use them in the Name function?
namespace CSharp_001
{
class Program
{
public Program()
{
Name(x, y);
AGE();
NUMBER();
}
public int AGE()
{
int x;
Console.WriteLine(" And enter age: ");
x = Console.Read();
return x;
}
public int NUMBER()
{
int y;
Console.WriteLine(" And favorite number: ");
y = Console.Read();
return y;
}
public void Name(int x, int y)
{
Console.WriteLine("Enter your name: ");
string test = Console.ReadLine();
Console.WriteLine("Hi " + test);
}
static void Main(string[] args)
{
new Program();
Console.ReadLine();
}
}
}
In c#, the Type of a value is really important. When you read from the console, you get a string. However, your Age() and NUMBER() functions return ints. A function that is declared to return an int cannot return a string. You need to convert those values to int before you can return them:
public int AGE() {
Console.WriteLine(" And enter age: ");
return int.Parse(Console.Read());
}
After you fix both functions, you can call Name() like this:
public Program()
{
int x = AGE();
int y = NUMBER();
Name(x, y);
}
or like this:
public Program()
{
Name(AGE(), NUMBER());
}
In either case, if you want to pass those values to the Name() function, the calls to AGE() and NUMBER() must be resolved before the the call to Name(). The console text indicates you want the prompts in Name() to come first. In that case, you might do this:
public void Name()
{
Console.WriteLine("Enter your name: ");
string test = Console.ReadLine();
Console.WriteLine("Hi " + test);
int age = AGE();
int number = NUMBER();
}
public Program()
{
Name();
}
For educational purposes:
public Program()
{
var age = AGE();
var num = NUMBER();
Name(age, num);
}
When a function returns a value, you need to store it or use it immediately. If you look at your example, in the AGE method, you have
x = Console.Read();
Console.Read is returning the value you're storing in x. But this x is local to the AGE method, nothing else called x will have that value (its scope is local to the AGE method). The AGE method returns this value, but that doesn't mean you can use the variable x anywhere else.
When you call AGE in the Program constructor, you need to save its return value. If you have that value saved, then you can pass it in as an argument to Name.
int age = AGE();
Name(age, y);
(assuming y is set by your call to NUMBER). When Name is called, you'll be able to use x, and it will have the value you passed in (which above, is called age).
A few comments:
Console.Read only read one character and returns the UTF-16 code of that character - you should use Console.ReadLine instead and parse the results if necessary.
Functions should be named to describe what they do, so GetAge and GetName would be more appropriate function names
Constructors should only be used to initialize the object to a usable state. Your code should either be in the Main function or in a separate function of Program that Main calls.
variables should have meaningful names. x and y do not give any indication of what they represent.
So something like this would be more appropriate:
class Program
{
public void Run()
{
int age = GetAge();
int number = GetNumber();
GetName(age, number);
}
public int GetAge(){
string age;
Console.WriteLine(" And enter age: ");
age = Console.ReadLine();
return int.Parse(x); // or TryParse and loop until the parse succeeds
}
public int GetNumber(){
string number;
Console.WriteLine(" And favorite number: ");
number = Console.ReadLine();
return int.Parse(number); // or TryParse and loop until the parse succeeds
}
public void Name(int x, int y)
{
Console.WriteLine("Enter your name: ");
string test = Console.ReadLine();
Console.WriteLine("Hi " + test);
}
static void Main(string[] args) {
new Program().Run();
Console.ReadLine();
}
}
I am trying to be an educated lazy chemistry student, by making a C# program that can do chemistry calculation for me. In order to make the code, I have to understand well the procedures in chemistry class.
I am new to any kind of programming, C# is my first language.
The code works fine for 1 Element calculation, but not 2 Elements calculation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
MassCalculation myMassCalculation = new MassCalculation();
TwoMassCalculation myTwoMassCalculation = new TwoMassCalculation();
Console.WriteLine("How many elements are in the compound?");
string userMainInput = Console.ReadLine();
if (userMainInput == "1")
{
myMassCalculation.Amount1 = 1;
Console.WriteLine("What is the ELEMENT?");
string userInput1 = Console.ReadLine();
Elements element;
if (Enum.TryParse<Elements>(userInput1, true, out element))
{
switch (element)
{
case Elements.Na:
myMassCalculation.Element1 = 22.990;
break;
case Elements.Cl:
myMassCalculation.Element1 = 35.453;
break;
default:
break;
}
}
Console.WriteLine("How many?");
string userAmount1 = Console.ReadLine();
int myAmount1 = int.Parse(userAmount1);
myMassCalculation.Amount1 = myAmount1;
myMassCalculation.DoCalculation();
resultOfMassCalculation(myMassCalculation);
}
if (userMainInput == "2")
{
Console.WriteLine("What is the First ELEMENT?");
string userInput1 = Console.ReadLine();
Elements element;
if (Enum.TryParse<Elements>(userInput1, true, out element))
{
switch (element)
{
case Elements.Na:
myMassCalculation.Element1 = 22.990;
break;
case Elements.Cl:
myMassCalculation.Element1 = 35.453;
break;
default:
break;
}
}
Console.WriteLine("How many?");
string userAmount1 = Console.ReadLine();
int myAmount1 = int.Parse(userAmount1);
myMassCalculation.Amount1 = myAmount1;
Console.WriteLine("What is the Second ELEMENT?");
string userInput2 = Console.ReadLine();
if (Enum.TryParse<Elements>(userInput2, true, out element))
{
switch (element)
{
case Elements.Na:
myTwoMassCalculation.Element2 = 22.990;
break;
case Elements.Cl:
myTwoMassCalculation.Element2 = 35.453;
break;
default:
break;
}
}
Console.WriteLine("How many?");
string userAmount2 = Console.ReadLine();
int myAmount2 = int.Parse(userAmount2);
myTwoMassCalculation.Amount2 = myAmount2;
myTwoMassCalculation.DoCalculation();
resultOfMassCalculation(myTwoMassCalculation);
}
Console.ReadLine();
}
private static void resultOfMassCalculation(MassCalculation calculation)
{
Console.Write("The Mass is {0}g/mol", calculation.DoCalculation());
}
}
enum Elements
{
Na,
Cl,
}
class MassCalculation
{
public double Element1 { get; set; }
public int Amount1 { get; set; }
public virtual double DoCalculation()
{
double result = Element1 * Amount1;
return result;
}
}
class TwoMassCalculation : MassCalculation
{
public double Element2 { get; set; }
public int Amount2 { get; set; }
public override double DoCalculation()
{
double result = Element1 * Amount1 + Element2 * Amount2;
return result;
}
}
}
Please help! I know it seems somewhat unprofessional. I have just started programming a week ago, and this is the best I can do. I need guidance.
The only elements defined in the code is Na and Cl, I am trying to calculate NaCl. When everything is in place, I will add more elements to the list, and many more different types of calculations.
I'll take constructive opinions.
Thank you so much in advance.
I refactored your code a little. It will work the same way, but wont crash on inappropriate user input
https://dotnetfiddle.net/CMQugr
using System;
using System.Collections.Generic;
namespace Test
{
public class Program
{
public static Dictionary<string, double> Elements = new Dictionary<string, double>
{
{"Na",22.990},
{"Cl",35.453}
};
public static void Main()
{
double result = 0;
int elemenCountInput;
do
{
Console.WriteLine("How many elements are in the compound?");
} while (!Int32.TryParse(Console.ReadLine(), out elemenCountInput));
for (int i = 0; i < elemenCountInput; i++)
{
string element;
do
{
Console.WriteLine("What is the {0} element", (i + 1));
element = Console.ReadLine();
} while (!Elements.ContainsKey(element));
int amount;
do
{
Console.WriteLine("How many");
} while (!Int32.TryParse(Console.ReadLine(), out amount));
result += Elements[element] * amount;
}
Console.Write("The Mass is {0}g/mol", result);
Console.ReadLine();
}
}
}
There is problem in the code when elements are two. You are assigning the first element value to "myMassCalculation' object and second element value to "myTwoMassCalculation". When you call "DoCalculation()" "myTwoMassCalculation.Element1' and "myTwoMassCalculation.Amount1" have no values. That's why it is giving wrong answer. Make the following changes and try:
if (Enum.TryParse<Elements>(userInput1, true, out element))
{
switch (element)
{
case Elements.Na:
myTwoMassCalculation.Element1 = 22.990;
break;
case Elements.Cl:
myTwoMassCalculation.Element1 = 35.453;
break;
default:
break;
}
}
Console.WriteLine("How many?");
string userAmount1 = Console.ReadLine();
int myAmount1 = int.Parse(userAmount1);
myTwoMassCalculation.Amount1 = myAmount1;
I'd do something like:
Create a class for elements (name (string), whatever that number is (double/decimal)
Create a static dictionary of them of them indexed by name.
Loop through the parameters to Main (or loop around an input command) looking up each entry in the dictionary then perform the calculation.
Convert to LINQ if desired.
This is be a great approach and should teach you a lot. I'm not going to write it for you (time and desire), but I may come back later with an example.
I'm practicing building a dummy application and I'm trying to take pass in an enum as a parameter and have an integer output parameter that will be used to set a member variable (BillsOwed). I have two specific questions: Why is it that ComputeRetirementBenefitCost tells me that my variable must be assigned to before control leaves the current method and why isn't ComputeRetirementBenefitCost accessible where I have marked in my comments? Any and all prudent future design recommendations are welcome :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GivePromotions
{
//Different Employees in the EmpType enum extend this class
public abstract class Employee
{
public int EmployeeId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime DateHired { get; set; }
public RetirementPackage.RetirementPackageType PackageType { get; set; }
public EmpType Employeetype { get; set; }
public int NetSalary { get; set; }
//I'd like to set the output of ComputeRetirementBenefitCost
//in the member below but Intellisense doesn't pick it up
//on two lines for space, commented because it doesn't work
//int billsOwed = Employee.RetirementPackage
//.ComputeRetirementBenefitCost(Employee.EmpType,out benefitCost)
public virtual void DisplayInformation()
{
Console.WriteLine("Employee information: ");
Console.WriteLine("EmployeeId = {0} ", EmployeeId);
Console.WriteLine("Last name = {0} ", LastName);
Console.WriteLine("First name = {0} ", FirstName);
Console.WriteLine("Date hired = {0} ", DateHired);
Console.WriteLine("Salary = {0} ", NetSalary);
}
public enum EmpType
{
Janitor,
President,
Manager
}
//an Employee 'has-a' Retirement package
public class RetirementPackage
{
public enum RetirementPackageType
{
Basic,
Gold,
Silver,
Platinum,
Black
}
//method of choice here
//ERROR: Output parameter 'benefitCost' must be assigned to before control
//leaves the current method
public void ComputeRetirementBenefitCost(Employee.EmpType e, out int benefitCost)
{
switch (e)
{
case Employee.EmpType.President:
benefitCost = 100000;
break;
case Employee.EmpType.Manager:
benefitCost = 5000;
break;
case Employee.EmpType.Janitor:
benefitCost = 1000;
break;
}
}
}
}
}
ERROR: Output parameter 'benefitCost' must be assigned to before control:
You have to add a "default:" to your switch or set a value outside your switch as it can be that none of your three cases is hit.
and why isn't ComputeRetirementBenefitCost accessible
Fixing the first will fix this one.
Use crtl+shif+B to build and read the errors and warnings in the error list view. Usually they tell you exactly what is wrong.
In the method ComputeRetirementBenefitCost it looks like you handled all possible code flows, because enum Employee.EmpType has three different definitions and you covered all three all them. But this is not true. From MSDN Enumeration Types (C# Programming Guide):
It is possible to assign any arbitrary integer value to meetingDay. For example, this line of code does not produce an error: meetingDay = (Days) 42.
In your case I can call your method like this
int benefitCost = 0;
ComputeRetirementBenefitCost( (Employee.EmpType) 500, out benefitCost);
And in this case I will be out of your code flow, so benefitCost will not be initialized. You can change implementation like this:
public void ComputeRetirementBenefitCost(Employee.EmpType e, out int benefitCost)
{
switch (e)
{
case Employee.EmpType.President:
benefitCost = 100000;
break;
case Employee.EmpType.Manager:
benefitCost = 5000;
break;
case Employee.EmpType.Janitor:
benefitCost = 1000;
break;
default:
throw new ArgumentOutOfRangeException("e");
}
}
The second problem - it looks like you are trying to invoke ComputeRetirementBenefitCost as a static method, but it is not static, so just add a static keyword to it.