Creating Functions

This is a discussion on Creating Functions within the C# Programming forums, part of the General Programming Boards category; How do you create a function in C# to use over and over....

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Creating Functions

    How do you create a function in C# to use over and over.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Long Answer

    In C# there are no functions like in C/C++, because it uses a very pure object oriented approach. Almost everything you do exists within a class (or a struct) that represents an object that has parts (Members, like 'variables') and actions (or Methods, like 'functions').

    An object uses the Methods and Members of its class to interact with another class. A good way to look at it is this:

    I am a Person object, and I have a name and age (my 'Members') and I can do actions such as eat or punch other Person objects (these actions would be my Methods).

    Some example in code:
    Code:
     public class Person
     {
     	 // My 'members'
     	 public string Name;
      	 public int Age;
      
     	 // My 'methods'
     	 public void Punch(Person personToPunch)
     	 {
     		  ... code here...
     	 }
      }
    Beautiful language isn't it?

    Now you could consider the method above as a function (though technically it's not). And this is a very good way of representing 'objects' that can do things to other objects.

    But if I just have a simple function that calculates 2*2 and prints the results, this is how we would have to do it:
    Code:
     public class SimpleMaths
     {
     	 public void Calculate()
     	 {
     		  Console.WriteLine(2*2);
     	 }
     }
      
      public class Program
     {
     	 public static void Main()
     	 {
     		  // Create a new instance of our
       		  // SimpleMaths object
     		  SimpleMaths mySimpleMaths = new SimpleMaths();
     		  // Call the SimpleMaths method
     		  mySimpleMaths.Calculate();
     	 }
     }
    Its not all that usable, first we have to create an instance of our object, then call the method. The real problem is we have to go around creating instances of a class whenever we want to use one of its methods.

    But notice how we used Console.WriteLine() in the above example? Did you know Console is a class? Scandalous! Why can they do it when we can't?

    The answer is in static methods. A static method has the keyword 'static' in front of it, and does not require an instance of the object to be called. Heres how the .NET Frameworks Console class would look (very simply):
    Code:
     public class Console
     {
     	 [....]
     	 // Note the 'static'
     	 public static void WriteLine(string stuffToWrite)
     	 {
     		  // Do stuff...
     	 }
     	 [....]
     }
    By using static methods you can create what C++ might consider a "global function", a method that is avaliable to any other class. You can also create global variables using static properties:
    Code:
     public class Console
      {
     	 [....]
     	 // Note the 'static'
     	 public static string GlobalVariable;
     	 public string NonGlobalVariable;
     	 [....]
      }
    Often you will have a few "global functions" that you use a lot in your applications. A good way to keep them all together is to create a class called something like "Globals" or "Utilities" to store any common static methods.
    Short Answer

    In summary, everything must exist in a class in C#. And every time you use a property or method, you need an instance of that class, unless the method/property is marked static.
    Hope that answered your question

    I'm thinking about writing a big tutorial about C#, to cover as much as possible (beginner to advanced)...
    Last edited by nickname_changed; 03-02-2005 at 08:16 PM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Lol wow. I guess I am too lazy to read my whole C# book. But I don't belive it covered this, if it did I wouldn't know.

    You still continue to amaze me!

    And your msn you gave me is scary. Well the message it gave me when I tried to IM you is.

    And yes that answered more than 1 question I had!

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah I'm writing an MSN listener that takes system commands and outputs them through MSN (in C# of course ). Eg., "shell ping localhost" or "list processes" or "dir C:\" and then "send C:\text1.txt" to use MSN's file transfer features. That's why you got that message

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh, well... maybe some day I talk to you? I got a lot of questions that I bet you could answer and wouldn't get mad or close my topic if I answered. Not saying anything illegal but I guess some people don't want me to destroy my computer (I am smarter than that!).

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah I'm normally on in after work (about 5 hours from now).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Creating push & pop functions... decrementing/tracking?
    By Sparrowhawk in forum C Programming
    Replies: 22
    Last Post: 12-14-2008, 09:10 PM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21