DLL: Creating Objects or Concrete Classes

DLL: Creating Objects or Concrete Classes

  • Comments 1

image First off tip of the hat to Tamer Maher:

  • Tamer Maher in Egypt suggested that I include links to the many game programming sites that I like, these will be included in the next posting of this blog. Thank you Tamer for your input! See his blog at http://TamerMaher.spaces.live.com where he discusses security!

Cy Khormaee and Bradley Jensen who will be joining their student teams to attend the Imagine Cup in Cairo, Egypt!  They will be joining my other teammates: Dan Waters, Clint Rutkas, and Diane Curtis.

Oops:

In the previous blog I said that we would go over how to use abstract classes.  I decided to discuss object instantiation from classes

How do you create or instantiate objects?

Now back to something that is entirely on track to using existing code and building code that you can share with others.

A few important points:

  • A class and an object are different things.
  • A class is a pattern that an object is built on, but it is not an object itself.
  • An object is a concrete entity based on a class, and is instance of a class.
  • The object is created through a process referred to as construction and instantiation

Objects can be created using the new keyword followed by the name of the class that the object will be based upon.

In C# you create the object from the class pattern by the code, written here where the object is given the same name as the class, in the following line of code, I show you the way that is more professional and easier to utilize:

Avatar Avatar = new Avatar();  //Can be confusing, but used in many examples

A better way to instantiate an object would be to give the instantiated object a separate but clear name:

Avatar BaldGuy = new Avatar();  //Less confusing and more descriptive

clip_image002

When an instance of a class is created (instantiate), a reference to the object is passed back to the programmer (the yellow arrow in the figure). In the example above, BaldGuy is a reference to an object based on Avatar. This reference refers to the new object, but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:

Avatar BaldGuyA;

Creating object references that do not reference an object is a bad idea because it will fail at run time which occurs after you compile the program. This is an error that will not show up until you try to use your program.

However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, like this:

Avatar BaldGuyB = new Avatar();
Avatar BaldGuyC = BaldGuyB;
  • Two object references (BaldGuyB and BaldGuyC) refer to the same object (Avatar).
  • Changes to the object made through BaldGuyB will be reflected in subsequent uses of BaldGuyC.
  • Objects based on classes that are referenced (the process described) reference types of classes
Next Blog:
  • Back to abstract classes and how to use them, differences between base classes, etc.
Going beyond:
  • What is does Override mean?
Leave a Comment
  • Please add 7 and 3 and type the answer here:
  • Post
  • This is very good news was well informed that the followers of the issue I am. Thanks...

Page 1 of 1 (1 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them