简体   繁体   中英

C# console application - commission calculator - how to pass variables into Main()

I can't figure out how to pass total, sale and comm into Main().

Anybody got an idea how to get those variables into Main and display (output) them there with the names?

Right now I can just output the variables in calcComm ...

Thanks in advance

Philip

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication38
{
class Program
{

    public static void getsales()
    {
        string inputsales;
        double total = 0;
        double sale = 0;

        for (int salecount = 1; salecount <= 3; ++salecount)
        {

            Console.WriteLine("Enter sale: ");
            inputsales = Console.ReadLine();
            sale = Convert.ToDouble(inputsales);
            total = total + sale;

        }

        calcComm(total);

    }

    public static void calcComm(double total)
    {

        double comm = 0;
        comm = total * 0.2;
        Console.WriteLine(comm);

    }


    public static void Main () 
    {
        Console.WriteLine("           Sunshine Hot Tubs \n        Sales Commissions Report\n");
        char Letter;

        const string name1 = "Andreas";
        const string name2 = "Brittany";
        const string name3 = "Eric";
        string inputLetter;
        string name;
        Console.WriteLine("Please enter intial or type 'z' to quit");

        inputLetter = Console.ReadLine();
        Letter = Convert.ToChar(inputLetter);



        while (Letter != 'z')
        {

            if (Letter == 'a')
            {
                name = name1;
                getsales();
            }
            else if (Letter == 'b')
            {
                name = name2;
                getsales();
            }
            else if (Letter == 'e')
            {
                name = name3;
                getsales();
            }

                   else
                   {
                      Console.WriteLine("Invalid entry try again");
                   }



                   Console.WriteLine("Please enter intial or type z to quit");

                   inputLetter = Console.ReadLine();
                   Letter = Convert.ToChar(inputLetter);




        }
    }
 }
}

This gives an array of strings corresponding to the command line parameters.

Main(string [] args)

By the way, when dealing with monetary units, it's better to use decimal than double.

You should be using objects, then you can make those public.

class Sales
{
    public double total;
    public double sale;
    public double comm;
    ...

    public void CalcComm()
    {
       ...
    }
 }

Then you can reference them like this:

 Sales.total, Sales.sale  

Or you can make them global but that's not normally advisable.

Look into the return keyword in C#; get your functions to return the relevant data back to main and have it make use of it.

Consider this example for how to add command line arguments. If you need them to be programmatically added consider writing a wrapper program and starting the Process inside it.

using System;

class Program
{
    static void Main(string[] args)
    {
    if (args == null)
    {
        Console.WriteLine("args is null"); // Check for null array
    }
    else
    {
        Console.Write("args length is ");
        Console.WriteLine(args.Length); // Write array length
        for (int i = 0; i < args.Length; i++) // Loop through array
        {
        string argument = args[i];
        Console.Write("args index ");
        Console.Write(i); // Write index
        Console.Write(" is [");
        Console.Write(argument); // Write string
        Console.WriteLine("]");
        }
    }
    Console.ReadLine();
    }
}

either you can build up a Data Transfer Object that holds all these three variables instantiate it and then return it to your main function.

You could also make use of variables that are passed as references instead of by value and use the updated reference value. Read about pass by value type & reference type for c# and the ref keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM