Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

Passing properties

How would I pass one property to another completely seperate property?

Say if I had
Person.Weight

and I wanted to verify that it was within a range of
Elevator.WeightLimit

How could I compare the two?
I hope that makes sense...
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

If person.Weight < elevator.WeightLimit Then

End If

You'll also want to make sure you're comparing apples and apples (i.e. two numerics, not a numeric and a string; two weights in pounds, etc)
Avatar of sirbounty

ASKER

Hmm - I'm still learning here, as you may can tell.

I'm working on creating custom controls.
If I have ControlA and it needs 'something' (which ultimately ends up being presented in ControlB's properties) - how do I code in advance for something like that?

I guess what I'm asking is - up until now, I've used my SET to assign a value from a textbox on my form.  I'm not quite sure how I would use SET in such an instance...(again - hope that makes sense - I'm struggling a bit here! :^)
A more complicated scenario... you've added a bunch of people to the elevator... (this is VS2005 code)

Public Class Elevator
  Private _people As List(Of Person)
  Public Property People() As List(Of Person)
    Get
      Return _people
    End Get
    Set(ByVal value As List(Of Person))
      _people = value
    End Set
  End Property

  Private _weightLimit As Integer
  Public Property WeightLimit() As Integer
    Get
      Return _weightLimit
    End Get
    Set(ByVal value As Integer)
      _weightLimit = value
    End Set
  End Property

  Public Function IsUnderWeight() As Boolean
    Dim totalWeight As Integer
    For Each p As Person In People
      totalWeight += p.weight
    Next

    Return (totalWeight <= WeightLimit)
  End Function
End Class

Public Class Person
  Private _weight As Integer
  Public Property Weight() As Integer
    Get
      Return _weight
    End Get
    Set(ByVal value As Integer)
      _weight = value
    End Set
  End Property
End Class
Well, there are really two questions you need to ask:

1. What is the relationship between class 1 and class 2? (i.e. what are the dependencies)
2. Does class 1 need to talk directly to class 2? Or does the GUI need to do the talking?
1) - potentially 'nothing'?  I'm not sure on that one
2) - Class1 wouldn't know Class2 existed and vice-versa, except that Class2 would be expecting 'some' data.

How about if I explained it this way...

Let's say that I had Elevator.Occupancy
and it was 0 initially, but I had a function to test when it exceeded X.

I've got to pass # of persons to that class.

So, would I have both a get & set for this?  Or would this be a 'readonly' scenario?

Let's say that for each time Occupancy increased by 1, I had an extra label placed on my form with that person's name (or weight).  If it was Class1's job to place the labels (assuming they're custom-created labels), how would Class2 pass that value each time it changed?

Man - this is tough to understand. Ugh...
In general, object-oriented design says that you separate responsibility as much as possible.

Sounds like you want to have an ElevatorGUI class and an Elevator class. The ElevatorGUI class would be responsible for updating the display.

Public Class Elevator

End Class

Public Class ElevatorGUI
  Private _elevator As Elevator
  Public Property Elevator() As Elevator
    Get
      Return _elevator
    End Get
    Set(ByVal value As elevator )
      _elevator = value
    End Set
  End Property

Public Sub Update

End Sub
End Class

Now, any time you make a change to the Elevator class, you can call update on the ElevatorGUI to force it to redraw the control. Since it has an Elevator property, you would add people to the Elevator, and the GUI would know about the changes you made to the elevator.

Since objects are reference-type objects, you can do this:

Dim myOtis as New Elevator
myOtis.People.Add(New Person("Jill", 150))
Dim eGui AS New ElevatorGUI
eGui.Elevator = myOtis
eGui.Elevator.Add(New Person("Jack", 200))
eGui.Update

You should see 2 people added. (I've cheated a bit here, assuming that you have a person constructor that takes name and weight as arguments)
I don't have anything - I'm thinking this all up as I go - trying to fit it into a box that makes sense to me...
And I'm afraid that may have taken it a bit over the top. :$

No problem... I'm just used to people asking for sample code, then taking it very literally (even when I say things like "your connection string goes here") -- so I wanted to give you the context.

Did my example make sense, or did I confuse you?
I think I'm confused - but I was going to go to bed and read it in the morning with a fresh brain.  I've literally been working with .net all day! (that's roughly 16 hours on and off).  This has always been one of the most difficult areas for me - even in VB6...<sigh>.

I'll post back tomorrow how much I actually understand here.  Thanx.
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yuk yuk - you had me pretty good there 'till the advanced topic.
I think I'm understanding a bit more now.  Nice explanation - thanx.
I warned you :)

Interfaces are just ways to force everything that implements them to have a certain basic set of properties and methods. What's nice about them is that you can handle them interchangably by creating an object of the interface type, rather than a specific class.

There's a HeadFirst book on patterns that might help you understand some of this stuff a little better.
Still 'with' it today...I've got another small problem, but I'm trying to work through it based on how you described it here.
Thanx again for all the help.
Let me know if I can help.