[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Adding a function to a class

Posted on 2011-03-20
5
Medium Priority
?
339 Views
Last Modified: 2012-05-11
I tried to add a tax function to a class and it seems like it is not working.  here is the function and class below.  Now in the code below, I get the following errors:

      Invalid token 'double' in class, struct, or interface member declaration

and

'MarketingEmail.ShoppingCart.CalculateTax(string)': not all code paths return a value

What could be causing these errors and how do I fix this so that there is no error.  I want to be able to do the code in class so that all I have to do is call the function in my program based on my class.  Thanks!
namespace MarketingEmail
{
    public class ShoppingCart
    {

        private int shoppingcartid;
        private int shoppingcartitemid;
        private int shoppingcartitemcount;
        

        
         function double CalculateTax(string CustomerState)
        {
            double salestax;

            switch(CustomerState)
            {
                   

                case "Alabama":
                    salestax = 4.0;
                    break;

                case "Alaska":
                    salestax = 0.0;
                    break;

                case "Arizona":
                    salestax = 6.6;
                    break;

                case "Arkansas":
                    salestax = 6.0;
                    break;

                case "California":
                    salestax = 8.25;
                    break;

                case "Colorado":
                    salestax = 2.9;
                    break;

                case "Connecticut":
                    goto case "Arkansas";

                case "Delaware":
                    goto case "Alaska";
                    
                case "District of Columbia":
                    goto case "Arkansas";

                case "Florida":
                    goto case "Arkansas";

                case "Georgia":
                    goto case "Alabama";                    

                case "Hawaii":
                    goto case "Alabama";

                case "Idaho":
                    goto case "Arkansas";

                case "Illinois":
                    salestax = 6.25;
                    break;

                case "Indiana":
                    salestax = 7.0;
                    break;

                case "Iowa":
                    goto case "Arkansas";

                case "Kansas":
                    salestax = 5.3;
                    break;

                case "Kentucky":
                    goto case "Arkansas";
                    
                case "Louisiana":
                    goto case "Alabama";

                case "Maine":
                    salestax = 5.0;
                    break;

                case "Maryland":
                    goto case "Arkansas";

                case "Massachusetts":
                    salestax = 6.25;
                    break;

                case "Michigan":
                    goto case "Arkansas";

                case "Minnesota":
                    salestax = 6.875;
                    break;

                case "Mississippi":
                    goto case "Indiana";

                case "Missouri":
                    salestax = 4.225;
                    break;

                case "Montana":
                    goto case "Alaska";

                case "Nebraska":
                    salestax = 7.7;
                    break;

                case "Nevada":
                    salestax = 6.85;
                    break;

                case "New Hampshire":
                    goto case "Alaska";

                case "New Jersey":
                    goto case "Indiana";

                case "New Mexico":
                    salestax = 5.125;
                    break;                    

                case "NJ":
                    salestax = 7.0;
                    break;
            }

            return salestax;
        }

    }
}

Open in new window

0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Expert Comment

by:epichero22
ID: 35176490
What line is the first error on?
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1000 total points
ID: 35176560
Method's (or functions) in C# don't use the "Function" keyword, that's strictly a VB thing.

No:
function double CalculateTax(string CustomerState)
{
    ...
}

Open in new window


Yes:
double CalculateTax(string CustomerState)
{
    ...
}

Open in new window


Also, the C# compiler doesn't like it when you try and use a variable that never had anything assigned to it.
So, given:
double salestax;

switch(CustomerState)
{
    case "Alabama":
        salestax = 4.0;
        break;
}

return salestax;

Open in new window


It's possible, if "CustomerState" doesn't match any of the "case" statements that salestax will never have anything assigned to it, so the compiler gives you an error.  You can solve that by assigning a value to salestax when you declare it:
double salestax = 0;

switch(CustomerState)
{
    case "Alabama":
        salestax = 4.0;
        break;
}

return salestax;

Open in new window


Or by using a "default" case, which will be executed in the event no other case matches (which is probably a good idea anyway, will give you a chance to throw an exception or return a value indicating an error in case CustomerState isn't valid):
double salestax = 0;

switch(CustomerState)
{
	case "Alabama":
		salestax = 4.0;
		break;

    case "Alaska":
        salestax = 0.0;
        break;
	default:
		throw new Exception("Invalid CustomerState.");
}

return salestax;

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 total points
ID: 35177033
What about the second error - you need some default case. Like tgerbert pointed out. But probably, you may not want to rise an exception if some wrong state string is passed. Some other options are possible.

E.g., you always return 0 if no such state.
Or, you return negative value (probably, not the nicest solution but some people do so).
3. Or, you may add some boolean out parameter, like 'out canGetTaxForState.

And so on.

I'd also initialize the return velue at the beginning of a function  - in C#, unlike VB, values are not initialized by default.
0
 

Author Comment

by:VBBRett
ID: 35177410
OK, it appears that the not all code paths return a value is line 21 and it is the following line of code:

double CalculateTax(string CustomerState)
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35177455
I didn't get that error when I copy & paste your code, just the two matters I pointed out. Can you re post your code as it is now?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question