• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Property help

Don't ask me why - but I still struggle with GETs and SETs.
Anyone care to walk me through this - preferrably using semi-real-world scenarios?
0
sirbounty
Asked:
sirbounty
3 Solutions
 
Fernando SotoCommented:
What question do you have properties, Set and Get?
0
 
sirbountyAuthor Commented:
I get them confused.  I can't seem to get it straight which does what...
0
 
Carl TawnSystems and Integration DeveloperCommented:
Set is used to assign a value to a property, Get is used to retrieve it:


    Public Property FirstName As String
        Get
            Return _firstName
        End Get
        Set
            _firstName = value             '// value is a keyword implicitly defined for properties
        End Set
    End Property
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sirbountyAuthor Commented:
Let's say I have a textbox control.
If I want to pass the user entered value to that textbox UserText property - would that be a set?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Yes.
0
 
sirbountyAuthor Commented:
And I always "get" my property's value from the "set"?

So, if I've "set" UserText to be "John Doe" and I later want to retrieve the value, it runs through the "Get" which says
Return mstrUserName

but that process of 'returning' the value gets it from where?  Simply from the module-declared mstrUserName?

Am I kind of close?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Yup.

    Public Class Person

        Private _firstName As String

        Public Property FirstName As String
            Get
                Return _firstName
            End Get
            Set
                _firstName = value             '// value is a keyword implicitly defined for properties
            End Set
        End Property

    End Class
0
 
Shakti109Commented:

Public Class MyNewClass
    Private MyString as string

    Public Property TheString As string
        Get
            Return MyString
        End Get
        Set(ByVal Value As String)
            MyString = Value
        End Set
    End Property
end class

Ok, basically this has set up :

You have an object that is "internal" to this class : "MyString".
 You have an object that is "externally readable and set-able"  : "TheString"
   This object "TheString" is a reference to "MyString". It allows you to set the internal object with a publicly accessible   method.

In the "GET" portion of the class, you are simply returning the internal object "MyString" through the access property "TheString".

As an example of "getting" this item out of the class you would use :

Dim NewClass as MyNewClass
Textbox1.Text = NewClass.TheString

In the "SET" portion of the class, you are actually setting the internal object "MyString" by using the publicly accessible item "TheString", example :

Dim NewClass as MyNewClass
NewClass.TheString = "SomeValue"

When you go back and "Get" this, you will get that new value : "SomeValue".

Basically, you are turning the class into a "black box", for which the internal properties can only be modifed or viewed by the functions (get / set) you provide.

0
 
sirbountyAuthor Commented:
I just hope it sticks.
Thanx for taking the time to explain it. :^)
0
 
Carl TawnSystems and Integration DeveloperCommented:
You can also drop either the Set or Get to create read/write only properties. But if you do you need to explicitly tag them as such:

    Public WriteOnly Property Something() As String
        Set(ByVal Value As String)
            _something = Value
        End Set
    End Property

    Public ReadOnly Property SomethingElse() As String
        Get
            _somethingElse
        End Get
    End Property


Forgot the (ByVal value As String) bit in my last posts....to used to C# :o)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now