?
Solved

Property help

Posted on 2006-07-11
10
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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