Solved

Question object object creation

Posted on 2006-10-25
1
189 Views
Last Modified: 2010-04-16
I have a couple of classes listed below



public class House
{
   public House()
   {

   }

  // attributes
  int houseID = 0;
            

  // Get HouseIDID
  public int HouseID{
        get { return houseID; }
        set { houseID = value; }
  }

}

-----------------
public class HouseServices
{

      public static House GetHouseByID(int houseID)
      {
          House result;

            // data access stuff

            result = ConvertHouseInfoDataTableToHouse(HouseInfo);
            HouseInfo = null;
          return result;
      }


      public bool UpdateHouse(House House)
      {
            bool result = true;

            // data access stuff

              return result;
      }
}




---------------------------------
code calling these classes:
---------------------------------
#1
House myHouse;
myHouse = HouseServices.GetHouseByID(3);

myHouse.ID.ToString()
etc...


#2

HouseServices houseServices = new HouseServices();
houseServices.UpdateHouse(house)

---------------------------
Questions:
---------------------------
A. Why do I need to create an object for the updatehosue method for #2?

B. House myHouse. What is myHouse called? Is it a Class variable? Its not an object as it is.

C. Why dont I need to create a HouseServices object for #1?


0
Comment
Question by:tmueller18
1 Comment
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 17808231
A. Why do I need to create an object for the updatehosue method for #2?

The UpdateHouse method has signature:
      public bool UpdateHouse(House House).

The method is NOT static, i.e. it means it can be called only on an instance of HouseServices class.


B. House myHouse. What is myHouse called? Is it a Class variable? Its not an object as it is.

myHouse is an instance of an object. When you write "House myHouse;" you are not creating an object, you just create a reference to an object, and this reference points to "null". You may call it null object.
So the instance of the House is sayed to be null.

The above is not 100% correct however. Just think about the "myHouse" as a some kind of a "pointer" to the object.

C. Why dont I need to create a HouseServices object for #1?

The GetHouseByIDmethod has signature:
public static House GetHouseByID(int houseID), i.e it is static, in other words it is CLASS method, not instance method and therefeore needs not an instance to be created.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now