O'Reilly Forums: Objects In Lab 1 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Objects In Lab 1 Should object names be hardcoded in the class?

#1 User is offline   camaro215 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 23-May 14

Posted 23 May 2014 - 04:34 AM

In Lab 1, at the top of Pg175 in HFC# 3rd Edition, it says that Guy #2 creates a new instance of Bet, using this keyword (MyBet) to tell the Bet object that he's the bettor. I don't understand how this works.

The Guy class has a MyBet object, an instantiation of the Bet Class. Why is the name of the object defined inside the Guy class?

When I run the program, there'll be three different Guy objects running, but all three of them will refer to the their bets through the MyBet reference variable. But isn't this a problem? If all 3 guys use the same reference variable, aren't they modifying the same object in the heap?? Won't Al's bets be mixed in with Joe's and bob's bets?

I think each Guy object should have its own Bet object via a different reference variable.

Please explain this to me. Thanks.
0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 23 May 2014 - 11:59 AM

I think I understand your question. Each Guy object gets his own variables. So if Guys[0] is Joe and Guys[1] is Bob, then Guys[0].MyBet and Guys[1].MyBet will refer to two different Bet objects. You can tell this is the case because each Guy created a new Bet object. If you put a breakpoint on the "new Bet()" line in the Guy class, you'll see that it gets called three different times. Make sense?
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   camaro215 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 23-May 14

Posted 23 May 2014 - 03:11 PM

Andrew, thanks for replying. I'm honored to have the author reply to me. :)

Each Guy object will generate its own MyBet object?

What's the general rule about this? If an object calls another class and instantiates an object, it will always be distinct from any other slave object that a second parent object calls even if the names are the same?

It confuses me.

How does the compiler know that Joe's MyBet is different from Al's MyBet? Is it because each MyBet is denominated as Guys[0].MyBet, Guys[1].MyBet, Guys[2].MyBet (or its CLR equivalent) by the compiler? (as opposed to an object with the reference name of 'MyBet' sitting in the heap)
0

#4 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 May 2014 - 01:34 PM

I think you may be confusing variables and fields. The Guy class has a field called MyBet, and every instance of Guy has its own field. This is different than having a method that declares a reference variable.

Take another look at the page with the heading "Multiple references and their side effects," and also the Sharpen your Pencil exercise that follows it. If you go back through the pencil and coding exercises in the second half of Chapter 4, that will help.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users