Solved

Passing properties

Posted on 2006-07-11
14
178 Views
Last Modified: 2010-04-23
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...
0
Comment
Question by:sirbounty
  • 8
  • 6
14 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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)
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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! :^)
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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?
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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...
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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)
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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. :$

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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?
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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.
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 350 total points
Comment Utility
I guess what it comes down to... if your objects are related (say, for example, you have a car.. it might have a tires collection property containing 4 tires, plus a body property plus an engine property...) then they should be grouped together in some logical construct ("car"). However, if you had a trailer, it should have it's own construct, and include tires, etc.

The car might have a towing capacity, and the trailer might have a total weight and a hitch weight.

You might then have a method in the car class called "CanTow(hitchWeight)" that returns true if the hitch weight is less than the towing capacity.

The car never really needs to know anything about the trailer -- you can find out if you can tow it by asking for CanTow(trailer.hitchWeight)

If you want to marry the two of them a little more tightly, you might add a trailer property to the car class, and change CanTow(hitchWeight) to CanTow(trailer). Of course, if you wanted to tow something other than a trailer, you might be in trouble!

** Warning: advanced topic ahead **
So (to add an advanced topic), you could create an interface called ITowable that everything you tow would implement. This interface is basically a contract that says "anything that is towable will have these attributes." You would then add HitchWeight to the ITowable interface, and change CanTow to CanTow(ITowable). This way, you can create towable trailers, waterskiers and skateboarders... all of whom would be required to have a hitch weight.
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
Yuk yuk - you had me pretty good there 'till the advanced topic.
I think I'm understanding a bit more now.  Nice explanation - thanx.
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
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.
0
 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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.
0
 
LVL 24

Expert Comment

by:Jeff Certain
Comment Utility
Let me know if I can help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

763 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

6 Experts available now in Live!

Get 1:1 Help Now