954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# beginner need some pro advice

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
        }
    }
TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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 :-)

TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
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?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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();
        }
TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

Tobbek

Maybe you're used to working with global variables?

rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 155
Solved Threads: 41
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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.

TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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)

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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();
    }
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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();
        }
    }
TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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 alocal 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...

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 155
Solved Threads: 41
 

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

TobbeK
Junior Poster
190 posts since Feb 2008
Reputation Points: 10
Solved Threads: 3
 

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

rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 155
Solved Threads: 41
 

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

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You