Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 14:36
Show Gist options
  • Save ChrisMoney/3cf47ce03e024a1f6001 to your computer and use it in GitHub Desktop.
Save ChrisMoney/3cf47ce03e024a1f6001 to your computer and use it in GitHub Desktop.
C# --Mixing functions and methods
// MixingFunctionsAndMethods – mixing class functions and object methods can class cause problems
using Systems;
namespace MixingFunctionsAndMethods
{
public class Students
{
public string sFirstName;
public string sLastName;
// InitStudent – initialize the student object
public void InitStudent (string sFirstName, string sLastName)
{
this.sFirstName = sFirstName;
this.sLastName = sLastName;
}
// OutputBanner = output the introduction
public static void OutputBanner()
{
Console.Writeline(“Aren’t we clever:”);
// Console.WriteLine(? what student do we use?);
}
public void OutputBannerAndName()
{
// the class student is implied but no this
// object is passed to the static function
OutputBanner(); // a static function, here the current student object is passed explicitly
Output(this);
}
// OutputName – output the students name
public static void OutputName(Student student)
{
// here the Student object is referenced explicitly
Console.WriteLine(“Students name is {0}”, student.ToNameString());
}
// ToNameString – Fetch the student’s name
public string ToNameString()
{
// here the current object is implicit –
// this could have been written:
// return this.sFirstName + “” + this.sLastName;
return sFirstName + “ “ + sLastName;
}
}
public class Program
{
public static void Main(string[] args)
{
Student student = new Student ();
student.InitStudent(“Madeleine”, “Cather”);
// output the banner and name
Student.OutputBanner();
Student.OutputName(student);
Console.WriteLine();
// output the banner and name again
student.OutputBannerAndName();
// wait for user to acknowledge
Console.WriteLine(“Press Enter to terminate…”);
Console.Read();
}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment