0

I'm very beginner when it comes to programming c#, there's two variables in a formula which aren't parsing as integers although i declared them within the main function. I have multiple methods achieving small tasks and it doesn't seem to be linking the main variable to the method one, the rest of the code doesn't have any errors so i'm not sure why it's not doing the formula. (I'll have to post all the code in order to show u)

 public static void Main()
    {
        int income = 0;
        int children = 0;
        int tax = 0;
        AskForIncome(income);
        NoChild(children);
        CalculateTax(tax);
        ExitProgram();
    }

    public static void AskForIncome(int income)
    {
        Console.WriteLine("What is your total income: ");
        string testNum = Console.ReadLine();
        bool dummyBool = int.TryParse(testNum, out income);
        if (dummyBool)
        {
            Console.WriteLine();
        }
        else if(income < 0)
        {
            Console.WriteLine("Your income cannot be negative."); 
        }
        else
        {
            Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
        }
    }
    public static void NoChild(int children)
    {

        Console.WriteLine("How many children do you have: ");
        string testChild = Console.ReadLine();
        bool dummyBool2 = int.TryParse(testChild, out children);
        if (dummyBool2)
        {
            Console.WriteLine();
        }
        else if(children < 0)
        {
            Console.WriteLine("You must enter a positive number."); 
        }
        else
        {
            Console.WriteLine("You must enter a valid number.");
        }

    }
    public static void CalculateTax(int tax)
    {
        tax = income - 10000 - (children * 2000);
        if (tax < 0)
        {
            Console.WriteLine("You owe no tax.");
        }
        else
        {
            Console.WriteLine("You owe a total of $" + tax + " tax.");
        }
    }
  • Since int is value type, what you are doing is passing by value so the changes will not be visible in main method. See https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value for detailed info about the concept. – Smit Aug 06 '17 at 05:21
  • In c# you need to use `AskForIncome(ref int income)` to pass by reference. – Smit Aug 06 '17 at 05:22

1 Answers1

0

c# its oop language, in your example looks like you developed in c, First of all you need to do a class that will have all the variables and functions\methods you need semething like:

public class Employ
{
  public int income {get; set;}
  public int children {get; set}
  public int tax {get; set};

          public static void AskForIncome()
        {
            Console.WriteLine("What is your total income: ");
            string testNum = Console.ReadLine();
            bool dummyBool = int.TryParse(testNum, out income);
            if (dummyBool)
            {
                Console.WriteLine();
            }
            else if (income < 0)
            {
                Console.WriteLine("Your income cannot be negative.");
            }
            else
            {
                Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
            }
        }
        public static void NoChild()
        {

            Console.WriteLine("How many children do you have: ");
            string testChild = Console.ReadLine();
            bool dummyBool2 = int.TryParse(testChild, out children);
            if (dummyBool2)
            {
                Console.WriteLine();
            }
            else if (children < 0)
            {
                Console.WriteLine("You must enter a positive number.");
            }
            else
            {
                Console.WriteLine("You must enter a valid number.");
            }

        }
        public static void CalculateTax()
        {
            tax = income - 10000 - (children * 2000);
            if (tax < 0)
            {
                Console.WriteLine("You owe no tax.");
            }
            else
            {
                Console.WriteLine("You owe a total of $" + tax + " tax.");
            }
        }
}

and from main create employ object and do all what you need but learn about oop programming it's must.

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43