Solved

Property help

Posted on 2006-07-11
10
195 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 63

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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

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
I need help converting a bitmap to an image in VB.Net 1 42
Crystal reports vb.net 2 54
VB: Convert 2 dates to specific format 24 52
Sending receiving text messages in vb.net 15 48
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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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