Need help. Hit learning barrier. [modified]
-
:^) I am trying to learn C# in a classroom setting. I have hit a barrier in trying to understand something. In the following code sample under where it says
weeklySalesAmount
I get the message "The name 'weeklySalesAmount' does not exist in the current context".
And under the mutators section where it saysSetGrossPay
it tells me "'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
public Deduction(string empName, double weeklySales) { name = empName; weeklySalesAmount = weeklySales; } //MUTATORS: public double SetGrossPay(double weeklySales) { const double GROSS_WEEKLY_PAY = .07; grossPay = weeklySales * GROSS_WEEKLY_PAY; }
In the next section where it says.netPay
I'm told that "'Exercise_9_Chapter_4.Deduction.netPay' is inaccessible due to its protection level
",and that SetNetPay is "The name 'SetNetPay' does not exist in the current context".Deduction employee = new Deduction(empName, wkSales); employee.netPay = SetNetPay();
I thought that I had everything done correctly according to the book. I have no idea as how to correct these problems. If anyone can help me, or at least steer me in the right direction, it would be muchly appreciated. Thank you.:rose::confused::-O -- modified at 17:04 Monday 26th February, 2007 -
:^) I am trying to learn C# in a classroom setting. I have hit a barrier in trying to understand something. In the following code sample under where it says
weeklySalesAmount
I get the message "The name 'weeklySalesAmount' does not exist in the current context".
And under the mutators section where it saysSetGrossPay
it tells me "'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
public Deduction(string empName, double weeklySales) { name = empName; weeklySalesAmount = weeklySales; } //MUTATORS: public double SetGrossPay(double weeklySales) { const double GROSS_WEEKLY_PAY = .07; grossPay = weeklySales * GROSS_WEEKLY_PAY; }
In the next section where it says.netPay
I'm told that "'Exercise_9_Chapter_4.Deduction.netPay' is inaccessible due to its protection level
",and that SetNetPay is "The name 'SetNetPay' does not exist in the current context".Deduction employee = new Deduction(empName, wkSales); employee.netPay = SetNetPay();
I thought that I had everything done correctly according to the book. I have no idea as how to correct these problems. If anyone can help me, or at least steer me in the right direction, it would be muchly appreciated. Thank you.:rose::confused::-O -- modified at 17:04 Monday 26th February, 2007Don't post programming questions in the lounge. I moved it for you.
JMOdom wrote:
"'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
In fact, none of them return a value. That's what the message is telling you. You wrote a function that returns double, and you didn't return anything. Add return grossPage; at the end of the function and it will work. Also, if you have const values, it's a good idea to put them at the top, the whole point is that you can easily change them. Putting them in the function, you may as well hard code the value.
JMOdom wrote:
'Exercise_9_Chapter_4.Deduction.netPay' is inaccessible due to its protection level",
This means that netPay is private or protected. You should use public properties to expose private or protected members. You should expose only a get method if at all possible. Oh - if you didn't put an access modifier, it's private by default. What I mean is: int n; is private. public int n; is public, but you should instead do this: private int _n; public int N { { get { return _n; } // set { _n = value; } } and only uncomment/add the set if you have to.
JMOdom wrote:
and that SetNetPay is "The name 'SetNetPay' does not exist in the current context".
You've not shown us any code which involves a method called SetNetPay, so I can only assume this error is correct ( the method does not exist )
JMOdom wrote:
I have no idea as how to correct these problems
I'm happy to help, but in each case, the compiler has told you what is wrong. As I help you through these issues, take note of what the terminology is, so you can learn how to use the error messages to work out how to fix your code :-)
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
:^) I am trying to learn C# in a classroom setting. I have hit a barrier in trying to understand something. In the following code sample under where it says
weeklySalesAmount
I get the message "The name 'weeklySalesAmount' does not exist in the current context".
And under the mutators section where it saysSetGrossPay
it tells me "'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
public Deduction(string empName, double weeklySales) { name = empName; weeklySalesAmount = weeklySales; } //MUTATORS: public double SetGrossPay(double weeklySales) { const double GROSS_WEEKLY_PAY = .07; grossPay = weeklySales * GROSS_WEEKLY_PAY; }
In the next section where it says.netPay
I'm told that "'Exercise_9_Chapter_4.Deduction.netPay' is inaccessible due to its protection level
",and that SetNetPay is "The name 'SetNetPay' does not exist in the current context".Deduction employee = new Deduction(empName, wkSales); employee.netPay = SetNetPay();
I thought that I had everything done correctly according to the book. I have no idea as how to correct these problems. If anyone can help me, or at least steer me in the right direction, it would be muchly appreciated. Thank you.:rose::confused::-O -- modified at 17:04 Monday 26th February, 2007JMOdom wrote:
"The name 'weeklySalesAmount' does not exist in the current context"
JMOdom wrote:
public Deduction(string empName, double weeklySales) { name = empName; weeklySalesAmount = weeklySales; }
You need to declare variable "weeklySalesAmount". Probably at same place as variable "name".
JMOdom wrote:
'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
JMOdom wrote:
public double SetGrossPay(double weeklySales) { const double GROSS_WEEKLY_PAY = .07; grossPay = weeklySales * GROSS_WEEKLY_PAY; }
And that is correct, SetGrossPay has return type of double, you should have
return grossPay;
before closing bracket. Or better, change return type from double to void. It sucks if examples in your book don't even compile. :doh: Not that surprising tho.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
JMOdom wrote:
"The name 'weeklySalesAmount' does not exist in the current context"
JMOdom wrote:
public Deduction(string empName, double weeklySales) { name = empName; weeklySalesAmount = weeklySales; }
You need to declare variable "weeklySalesAmount". Probably at same place as variable "name".
JMOdom wrote:
'Exercise_9_Chapter_4.Deduction.SetGrossPay(double)': not all code paths return a value"
JMOdom wrote:
public double SetGrossPay(double weeklySales) { const double GROSS_WEEKLY_PAY = .07; grossPay = weeklySales * GROSS_WEEKLY_PAY; }
And that is correct, SetGrossPay has return type of double, you should have
return grossPay;
before closing bracket. Or better, change return type from double to void. It sucks if examples in your book don't even compile. :doh: Not that surprising tho.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus