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

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

   ► KBProgrammingC#OOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
C# OOP:
C# Class..Object (class...new)
 
Posted 11 years ago on 10/24/2008 and updated 1/28/2009
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page
 Tags: C# , Class..Object

KB101399



Languages Focus: Class..Object

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

C# Class..Object

In C#, you use the class keyword to specify a class and you signify its parent with a colon and the name of the parent class. When you instantiate an object from a class, you use the new keyword.

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();
//No need to clean up with managed classes.
//The garbage collector will take care of it.

Add a Property

Now let's give our cyborg a name:

public class Cyborg : System.Object
{
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, my name is " + Name + ".");
}
 
  public String Name;
}

The following exercises our new functionality:

Cyborg T1 = new Cyborg();
T1.Name = "Number 1";
T1.IntroduceYourself();  

Add a Method

Now let's give our cyborg the ability to speak:

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

Create object from class:

Cyborg T1 = new Cyborg();
T1.Speak("Humans are imperfect.");

Complete Listing

Here is the complete code listing (a form with a button on it). I renamed the namespace to Skynet, seemed appropriate.

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 Skynet
{
public class Cyborg : System.Object
{
    //Properties:
public String Name;
 
    //Methods:
public virtual void IntroduceYourself()
{
MessageBox.Show("Hi, my name is " + Name + ".");
}
 
    public virtual void Speak(string pMsg) 
{
MessageBox.Show(pMsg);
}
}
  public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Cyborg T1 = new Cyborg();
T1.Name = "Number 1";
T1.IntroduceYourself();
T1.Speak("Humans are imperfect.");
}
}
}

More Info


Comments

0 Comments.
Share a thought or comment...
 
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 = P1212A1
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


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: Multiple Choice

Given this class:

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

Which of the following correctly creates an object instance and calls the IntroduceYourself method?

Answer:
1. 
Cyborg T1 = Cyborg.Create();
T1.IntroduceYourself();
2. 
Set T1 = new Cyborg();
T1.IntroduceYourself();
3. 
T1 As Cyborg = new Cyborg();
T1.IntroduceYourself();
4. 
T1 = new Cyborg();
T1.IntroduceYourself();
5. 
Cyborg T1 = new Cyborg();
T1.IntroduceYourself();

 KB Article #101399 Counter
11744
Since 10/24/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


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