Solved

Passing properties

Posted on 2006-07-11
14
181 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET - Error - Object not set in Module 4 41
VB.NET 2008 Publish Error 2 34
Complex SQL statement in VB.NET 7 31
Crystal reports login after closing form 3 18
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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