8 Replies - 404 Views - Last Post: 16 March 2011 - 12:14 PM Rate Topic: -----

#1 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Calling a Function from a Class

Posted 16 March 2011 - 11:44 AM

Guys,

Is it possible to create a class and then call a function from within it?

Take a look at the code below:

namespace ClassesTaskFour
{
    class myCart
    {

        static void AverageNumbers()                            // Average 5 numbers
        {
            double number = 0.0;
            double total = 0.0;
            double count = 0.0;

            for (count = 1; count < 6; count++)
            {
                Console.WriteLine("Enter Numbers to Average: " + count);
                number = double.Parse(Console.ReadLine());

                total = (number * number) / 5;
            }

            Console.WriteLine("Total is: " + total);

            DisplayMenu();
            

        }
        
    }
}



Say for example on my main screen, would I be able to call this? Or is there a way around this?

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a Function from a Class

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Calling a Function from a Class

Posted 16 March 2011 - 11:49 AM

Since it is static, you can call myCart.AverageNumbers() directly. If the method wasn't static, you'd have to create an object first:
myCart myCartObject = new myCart();
myCartObject.AverageNumbers();

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 11:59 AM

Let's talk about design archetecture a moment though.

Sure what you have done is legal. But is is good design?
In C# and OOP we try to make little, descreete methods that can be re-used and have only one purpose inlife. Then string them together with some code to give the methods a purpose. In other words, we write Lego blocks for methods, and the main program decides how to make them plug into each other.

If you method is "AverageNumbers()" then that is all it should do. It shouldn't be responsible for writing to the console or for calling the menu. Those behaviors are part of the logic of your application.
        public decimal AverageNumbers(List<decimal> NumberList)
        {
            decimal total = 0m;
            foreach (var number in NumberList)
            {
                total += number;
            }
            return total / NumberList.Count;
        }



So the logic of your program should be:
            List<decimal> numbers = GetUserNumberList();
            decimal theAverage = AverageNumbers(numbers);
            Console.WriteLine(string.Format("The average is: {0}", theAverage));
            DisplayMenu();

Rather than lump all of your functionality into one method. If you were going to do that, then what would be the point of the methods at all.
Was This Post Helpful? 0
  • +
  • -

#4 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:02 PM

I'm new to programming so would never have thought of doing something like that! :blink: I'll take on board what you've said though and use it in my program. Cheers guys, all help is very much appreciated.. One day I'll understand this..
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:07 PM

These tutorials might help then. It should give you some concepts as well as code to tear apart for design hints.
Bulding an application - Part 1
Building an application - Part 2

Just remember: Plan first. Build second. Everyone is always so anxious to start typing but without a plan. You would never hammer together a bunch of boards just because you know how to hammer, later get a blueprint for the house.

This post has been edited by tlhIn`toq: 16 March 2011 - 12:13 PM

Was This Post Helpful? 2
  • +
  • -

#6 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:08 PM

I really appreciate the help!

Sorry but next question..

Say I've written this code:

 public void AddPrice()
        {
            double price = 0.00;
            double runningTotal = 0.00;
            

            Console.WriteLine("Enter Initial Price: ");
            runningTotal = double.Parse(Console.ReadLine());

            Console.WriteLine("Enter new price: ");
            price = double.Parse(Console.ReadLine());

            runningTotal = runningTotal + price;
            Console.WriteLine(runningTotal;
                     
        }


How I would I go about getting the runningTotal variable from this:
Console.WriteLine(runningTotal)


And use it as a variable in another function?
The reason for this is that I want to take the runningTotal and be able to transfer it to another function.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:11 PM

Right now AddPrice() has a return type of void.
You change that to double, then add an ending line of
return runningTotal();
Was This Post Helpful? 0
  • +
  • -

#8 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 18-October 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:13 PM

Ah, I guess if I'd looked at it better then the void would have given it away. I must sound so dull.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Calling a Function from a Class

Posted 16 March 2011 - 12:14 PM



All of this is usually covered in the first couple chapter of any intro to C# book. So I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library.

C# resources. Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables


Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1