Solved

Question object object creation

Posted on 2006-10-25
1
190 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

22 Experts available now in Live!

Get 1:1 Help Now