IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#OOP   Print This     
  From the November 2015 Issue of Prestwood eMag
 
C# OOP:
C# Member Method
 
Posted 16 years ago on 12/20/2008 and updated 8/1/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page
 Tags: C# , Member Method

KB101744

General Info: Member Method

Also known as a Class Method.

A code routine that belongs to the class or an object instance (an instance of the class). Methods that belong to the class are called class methods or static methods. Methods that belong to an object instance are called instance methods, or simply methods.

When a method returns a value, it is a function method. When no value is returned (or void), it is a procedure method.

Methods frequently use method parameters to transfer data. When one object instance calls another object instance using a method with parameters, you call that messaging.

C# Member Method

In C#, you indicate a method with parens, no parens indicates a member property. Use the void return type for methods that do not return a value.

Syntax Example:

Define class:

public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, I do not have a name yet.");
}
}

Create object from class:

Cyborg T1 = new Cyborg();
T1.IntroduceYourself();

A Simple Example

The following example uses our class above. Notice for the static�member, we do not use a variable, instead we use the class name directly. That's why static members are sometimes referred to as class members. Static members belong to the class�and are referenced by the class name.

Note: The public member fields cyborgName and cyborgAge are used here for demonstration only. You normally want to make them private and access them via a member property. Also not that cyborgName and cyborgAge start with a lowercase "c" and SeriesID starts with an uppercase "S". The reason I did this is normally you would make use of read-write public member fields. You would make them private. It is common to make member fields lowercase and properities uppercase.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace CR_MemberFields
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
    private void button1_Click(object sender, EventArgs e)
{
//Read static member field BEFORE we create our object.
//Notice the use of the class name, not a variable.
MessageBox.Show("We will now build a series " + Cyborg.SeriesID + " robot.");
 
    Cyborg MyRobot = new Cyborg();
 
    MyRobot.cyborgName = "John";
MyRobot.cyborgAge = 34;
 
    MessageBox.Show("We created a " + MyRobot.cyborgAge + " year old robot named " + MyRobot.cyborgName + "."); 

//You cannot refer to static members using an instance reference.

//MessageBox.Show("A series " + MyRobot.SeriesID + " robot.");

//Use a type name instead.

MessageBox.Show("A series " + Cyborg.SeriesID + " robot.");

}
}
 
  public class Cyborg : System.Object
{
private string serialNumber = "A100";
 
    public string cyborgName; 
public int cyborgAge = 0;
 
    public static readonly int SeriesID = 100;
}

}

More Info

Definition:  Member Method

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

Member choose a method for showing some good material. It is be good for some people who are waiting for always achieve https://toptenwritingservices.com/essays-studymoose-com-review/. But we still waiting for new and easy method for solving some troubles which they are showing us here.

Posted 51 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1116A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101744 Counter
16371
Since 12/20/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]