Hi

I am completely new to C#, so please bare with me ....

What I am like to do is to pass variable values between the methods below, and finally print the results in the last method. I have commented the code with explanation.

This is for test purposes and I must have a working example before my brain explode...

class Product
    {
        static void Main(string[] args)
        {
            Product objGet = new Product();
            objGet.ReadInput();

            Console.ReadLine();
        }

        private void ReadInput()
        {
            Console.Write(" Product name: ");
            string ReadName = Console.ReadLine();
            Console.Write(" Unit price: ");
            double UnitPrice = Convert.ToDouble(Console.ReadLine());
            Console.Write(" Unit count: ");
            int UnitCount = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Food item y/n: ");
            string ReadFoodItem = Console.ReadLine();
        }

        private double GetTaxRate()
        {
            // Taxrate variable should have data type double and can have 1 of 2 rates
            // if  the variable ReadFoodItem is "y" then TaxRate is 12
            // if  the variable ReadFoodItem is "n" then TaxRate is 25
            // Also a conversion of ReadFoodItem from string to boolean
         }

        private void CalculateValues()
        { 
            // This variable should have data type double
            // TotalAmount = TaxRate * TotalPrice / 100
        }

        private void PrintResults()
        {
            // Print variable ReadName
            // Print variable UnitCount
            // Print calculation of variables UnitPrice * UnitCount
            // Print variable ReadFoodItem
            // Print variable TotalAmount
        }
    }

Recommended Answers

All 21 Replies

OK, so you havent worked out the placing of parameters into functions - thats a very fundemental thing. If you do look through most of the code on this site you'll find some examples of user made functions that have parameters, and how to return values (theres some hints in there you know!)

You've done pretty well, in fact you've already used some parameters and prebuilt functions, you just need to take the next step of making your own.

I will be more than happy if you help me with one or two of the variables so I can fill out the rest and post it back here. I have trying this for days and I am not not use to C# at all. Frustrating :-)

I assume you are using Visual Studio 2005 or 2008.
Make a new Console Application, name it as you want but don't call it Product. Then add a new class Product to your solution.
Keep what you have in your Main method, it is good!(well I would do it different, but then again who am I?)
So your ReadInput method should be in the Product class you added and it should fill some fields from that class like ProductName, UnitPrice etc.

I will be more than happy if you help me with one or two of the variables so I can fill out the rest and post it back here. I have trying this for days and I am not not use to C# at all. Frustrating :-)

That sounds like someone whos coded in something else? Is this the case?

Hi

I think one of my problem for my to understand is how I can "pick" one variable and work with that one in the main method. If I can do that, It may solve a few things. Now, all variables in ReadInput is sent to the main method in a bunch.

All of the methods are placed within the product class

Those for example

static void Main(string[] args)
        {
            Product objGet = new Product();
            objGet.ReadInput();

            Console.ReadLine();
        }

        private void ReadInput()
        {
            Console.Write(" Product name: ");
            string ReadName = Console.ReadLine();
            Console.Write(" Unit price: ");
            double UnitPrice = Convert.ToDouble(Console.ReadLine());
            Console.Write(" Unit count: ");
            int UnitCount = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Food item y/n: ");
            string ReadFoodItem = Console.ReadLine();
        }

Well if you were going to do that you would need to pass product to your read input, as well as then having properties you would read it into.

Which goes back to my earlier questions

Yeah well that is the problem, how do I do that, passing the product values into another method so I can work with the value there?.

I am not use to C# syntax at all, I am old ASP programmer who try learn the basics in C#. This code is just for my own test purpose and to learn to handle those issues.

Tobbek

Maybe you're used to working with global variables?

OK, under ASP you'd most likely have used vb, did you ever make subs then?

VBScript mostly and VB. Completely different languages.

What do I need to pass for example the variable ReadName and pick just that one up into the main method. As you can see I have no problem creating new instances of the class and pass the entire method ReadInput to the main.

OK, but how would you have made a sub in vb, to send it say 2 parameters, and return the sum of them as a result?

(c# while different is not as hugely different in this respect)

Perhaps it is something like this you want?
Will this clarify your mind?

class Product
{
    public string ReadName; //name of the product
    //other fields here

    public void ReadInput()
    {
        Console.Write(" Product name: ");
        string ReadName = Console.ReadLine();
        //other things here
    }
    //other methods here
}

class MyConsoleApp
{
    static void Main(string[] args)
    {
        Product objGet = new Product();
        objGet.ReadInput();

        //now here I can use
        Console.WriteLine("Name of product : {0}", objGet.ReadName);

        Console.ReadLine();
    }
}

Yes thanks!

I dont know the difference between set public variable as you did and a method. But it seem to be a good start.

I will work with this a bit and send you a feedback here!
Thanks

No errors, but no one of the public variables can be assigned to any values. No matter if I try to change data types. I dont know whats wrong. My code here should do a simple calculation and print out the results.

class Product
    {
        public string ReadName; //name of the product
        public double ReadPrice; //price of the product
        public int UnitCount; //Unit count
        public string FoodItem; //Unit count

        public void ReadInput()
        {
            Console.Write(" Product name: ");
            string ReadName = Console.ReadLine();
            Console.Write(" Product price: ");
            double ReadPrice = Convert.ToDouble(Console.ReadLine());
            Console.Write(" Unit count: ");
            int UnitCount = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Food item y/n: ");
            string FoodItem = Console.ReadLine();
        }

        static void Main(string[] args)
        {
            Product objGet = new Product();
            objGet.ReadInput();

                 double iPrice = objGet.ReadPrice;
                 int iCount = objGet.UnitCount;

            double iCalc = iPrice * iCount;

            Console.Write(" Price total: {0}", iCalc);

            Console.ReadLine();
        }
    }

That is totaly my fault! Not yours!

public void ReadInput()
        {
            Console.Write(" Product name: ");
            string ReadName = Console.ReadLine();
            Console.Write(" Product price: ");
            double ReadPrice = Convert.ToDouble(Console.ReadLine());
            Console.Write(" Unit count: ");
            int UnitCount = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Food item y/n: ");
            string FoodItem = Console.ReadLine();
        }

Can you see why?
I redefined ReadName as a local string variable of the method ReadInput!
Console.Write(" Product name: ");
string ReadName = Console.ReadLine();

should be
Console.Write(" Product name: ");
ReadName = Console.ReadLine();

do the same for the rest of your variables.
Sorry for the trouble, but do you know the definition of an expert?
An expert is a person who has made all the possible mistakes...

hmm, well then ddanbe I am fast on my way to becoming an expert in everything!

commented: At least I am not alone! +4

Thanks again...
That was it !

I agree, and I must do a lot of mistakes myself to learn this. It is fun language but a little bit hard sometimes.

I am sure I will be back here soon..

best regards
Torbjorn

if that fixed your problem then mark the thread as solved for him

Torbjorn, feel free to come back with your questions. Mark this thread as solved if you want.

commented: Kind and helpful while sharing his deep knowledge +1

What am I missing here....

I try assign 2 public variables (TaxRate and ItemType) with values from the method GetTaxrate.

class Product
    {

        static void Main(string[] args)
        {
            Product objGetReceipt = new Product();
            objGetReceipt.PrintReceipt();
            Console.ReadLine();
        }
        
            // Public variables
            public string ReadName;  // Name of the product
            public double ReadPrice; // Price of the product
            public int UnitCount;    // Unit count
            public string FoodItem;  // FoodItem y/n
            public double TaxRate;   // Cannot be assigned any other value than 0
            public bool ItemType;    // Cannot be assigned any other value than false
          
        public void ReadInput()
        {
            Console.Write(" Product name: ");
            ReadName = Console.ReadLine();
            Console.Write(" Product price: ");
            ReadPrice = Convert.ToDouble(Console.ReadLine());
            Console.Write(" Unit count: ");
            UnitCount = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Food item y/n: ");
            FoodItem = Console.ReadLine();
        }

        private void GetTaxrate()
        {
            // Cannot GET or PASS any of the values to or from any public variables

            if (FoodItem.Equals("y"))
                TaxRate = 0.12;
                ItemType.Equals(Boolean.TrueString);
            if (FoodItem.Equals("n"))
                TaxRate = 0.25;
                ItemType.Equals(Boolean.FalseString);
        }

        private void CalculateValues()
        {
            Product objGetReadInput = new Product();
            objGetReadInput.ReadInput();

            string iName = objGetReadInput.ReadName;
            double iPrice = objGetReadInput.ReadPrice;
            int iCount = objGetReadInput.UnitCount;

            // Total price without VAT
            double iTotalPrice = iCount * iPrice;
   
            // Static VAT rate - THIS IS SET TO STATIC BECOURSE I FIND NO OTHER WAY
            double iRate = 0.25;

            // Total VAT
            double iTax = iRate * iTotalPrice;

            // Total amount with VAT
            double iTotalAmount = iTax + iTotalPrice;

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(" ------------------------------------------- ");
            Console.WriteLine(" Product name: \t\t\t" + iName);
            Console.WriteLine(" Total price: \t\t\t" + iTotalPrice);
            Console.WriteLine(" Item count: \t\t\t" + iCount);
            Console.WriteLine();
            Console.WriteLine(" Food item: \t\t\t" + ItemType);
            Console.WriteLine();
            Console.WriteLine(" Total VAT: \t\t\t" + iTax);
            Console.WriteLine(" Total amount: \t\t\t" + iTotalAmount);
        }

        private void PrintReceipt()
        {
            Product objGetCalcVal = new Product();
            objGetCalcVal.CalculateValues();  
            Console.WriteLine(" ------------------------------------------- ");
        }
    }

This is a new question, so post it again in a new thread, not a thread that has been marked as solved.
To you it may seem like the same(same program), but it really isn't. LizR may have given you a hint here to the solution(look back at the answers)
In a new thread you will get more response, solved threads are not watched so often anymore.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.