Solved

Property help

Posted on 2006-07-11
10
191 Views
Last Modified: 2010-04-23
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
Comment
Question by:sirbounty
10 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17083633
What question do you have properties, Set and Get?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17083650
I get them confused.  I can't seem to get it straight which does what...
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 17083652
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
 
LVL 67

Author Comment

by:sirbounty
ID: 17083658
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17083663
Yes.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 67

Author Comment

by:sirbounty
ID: 17083670
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 17083705
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
 
LVL 8

Accepted Solution

by:
Shakti109 earned 200 total points
ID: 17083713

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
 
LVL 67

Author Comment

by:sirbounty
ID: 17083738
I just hope it sticks.
Thanx for taking the time to explain it. :^)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17083755
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now