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

asked on

More Property help

Maybe a dumb question, but I am still trying to grasp properties...

If I'm declaring:

Private a as string
Private b as string
Private c as string

Can I use only 1 property, passing the 'name'?

Public Property MyProperty(myString as String) as String
??
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

You mean use one property to set the values for a, b and c ? And pass the name of the one to set ?
Avatar of sirbounty

ASKER

yes ( I think )
Then the answer is no.

You can set multiple values from within a single property, but you can't tell it which to set, unless you did it based on the value passed. But if you did that then you would confuse the usage of the property.

   U mean a property like this :

   Public Property MyProp(ByVal str As String) As String
        Get
            Return str
        End Get
        Set(ByVal Value As String)
            If str = "a" Then
                a = value
            ElseIf str = "b" Then
                b = value
            ElseIf str = "c" Then
                c = value
            End If
        End Set
    End Property
one more thing if u want the same values as parameters passed then u have to use the if statements for Get also

i.e.

Get
           If str = "a" Then
                return a
            ElseIf str = "b" Then
                return b
            ElseIf str = "c" Then
                return c
            End If
End Set
Great manch - I'll try this out.

What do I reference in my "get"?

If I'm using Int, for example:

Private mintBoard1 as Integer
[...]
Private mintBoard10 as Integer

Public Property BoardLength(byVal myBoard as Integer) As Integer
  Get
    Select Case myBoard
      Case ???  ' Would this be "mintBoard1" or something else?
        Return mintBoard1
>> But if you did that then you would confuse the usage of the property.<<

How so?  Remember - I'm still new to Properties...the last thing I need is more confusion! :^)
Odd - I haven't got notifications on these last two comments... :(
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland 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
Oh well - it just looked sloppy to me to have

private mVar1
private mVar2
[...]
private mVar10

But ah well...
Depending on what you are trying to achieve, there may be alternatives to using properties. If you wanted a large number of properties but not have to create variables for each one then you could opt for exposing a HashTable via a property and that would then allow your users to add Name/Value pairs through a single property.

This, of course, would require a little more validation within the property itself to make sure the user doesn't add anything they shouldn't. You could even have a method called SetProperty that takes a property name or Enum and a value:

    Private _hash As New HashTable()

    Public Sub SetProperty(ByVal name As String, ByVal value As String)
 
        _hash[name] = value

    End Sub
Beyond my 'current' understanding - but thanx.