Solved

Property help

Posted on 2006-07-11
10
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

23 Experts available now in Live!

Get 1:1 Help Now