Solved

Passing properties

Posted on 2006-07-11
14
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087111
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
ID: 17087131
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
ID: 17087135
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087140
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
ID: 17087160
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
ID: 17087227
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
ID: 17087275
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087277
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
ID: 17087292
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
ID: 17087303
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
ID: 17093169
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
ID: 17093248
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
ID: 17102289
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
ID: 17102410
Let me know if I can help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get month and date in a format 4 50
Consume a webservice via VB in Visual Studio 2015 3 42
GUID's in SQL Server 4 29
SSIS - Using VB.NET to parse XML file 11 39
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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