473,385 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Public variable VS property ?

In a single form , I can delcare a public vairable or property.
So. What is the difference between it ?? which is better ??
Dim frmTest as myForm
frmTest.strPublicVariable = "ABC"
frmTest.strPropertyVarialbe = "ABC"

Sorry for my stupid question .
Nov 21 '05 #1
3 3832
"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
In a single form , I can delcare a public vairable or property.
So. What is the difference between it ?? which is better ??
Dim frmTest as myForm
frmTest.strPublicVariable = "ABC"
frmTest.strPropertyVarialbe = "ABC"


If it's a Property, you can have particular code execute when you set or get
the value, for instance making a calculation or validating the new value.
This can't happen when it's a public variable.
Daniel
Nov 21 '05 #2
Always use Public Properties. Follow on of the tenants of OOP.

A property allows you to not only get the value but then make sure that it
is a good one before using it.. Consider the case below..

Public Class Foo

Public mMessage As String'cannot be NULL

Public Sub PrintMessage()
Debug.WriteLine(mMessage)

End Sub

End Class

This method allows the caller to try to display a null value. You have no
control over the value at this point.

Also consider what might happen here if the message was printing and someone
changed the value at the same time. using a property allows you to check for
conditions like that.

Now consider the code below:

Public Class Foo
Private mMessage As String 'cannot be NULL
Private mBusy As Boolean = False
Public Property Message() As String
Get
Return mMessage
End Get
Set (value As String)
If mBusy = False) Then
If (not value Is Nothing) then
mMessage = value
Else
Throw New ArgumentNullException("value", "The value
cannot be null!.")
End If
Else
Throw New ArgumentException("The message value is in use!
Try again latter.")
End If
End Set
End Property

Public Sub PrintMessage()
mBusy = True
Debug.WriteLine(mMessage)
mBusy = False
End Sub

End Class

This ensures that the value provided can never be null. Always do your best
to keep data internal to the class insulated form out side of the class this
way. It's called encapsulation.
"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
In a single form , I can delcare a public vairable or property.
So. What is the difference between it ?? which is better ??
Dim frmTest as myForm
frmTest.strPublicVariable = "ABC"
frmTest.strPropertyVarialbe = "ABC"

Sorry for my stupid question .

Nov 21 '05 #3
"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl...
In a single form , I can delcare a public vairable or property.
So. What is the difference between it ??


There are no Stupid Questions - just the occasional Silly Answer
(of which this /isn't/ one, I hope) ...

A Property can be controlled, because you get to see every value
coming in or going out.

Compare these two.

Public ValueThatMUSTBeBetweenOneAndThree as Integer

If this value is used within your class to, say, index into a three-element
array and something in the Outside World does ...

YourObject.ValueThatMUSTBeBetweenOneAndThree = 7

.... your class is in trouble!

Contrast that with

Public Property VTMBBOneAndThree() as Integer
Get
Return m_iIndex
End Get
Set( ByVal Value as Integer )
Select Case Value
Case 0 to 2
m_iIndex = Value
Case Else
Throw New ArgumentException( ...
End Select
End Set
End Property
Private m_iIndex as Integer = 0

Now if something in the Outside World tries to do ...

YourObject.VTMBBOneAndThree = 7

.... /they're/ in for a nasty surprise, but /your/ object will remain
quite happy.

HTH,
Phill W.
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Laurence Nuttall | last post by:
I have a class, and a public varible defined in one of the ..VB files of that class. I also have a form in that class, but I cannot reference the variable in the class from the form, even...
2
by: DC Gringo | last post by:
How can I access a "public var1 as new string" from a .aspx page's user control? -- _____ DC G
3
by: Tor Inge Rislaa | last post by:
How to use a public variable In VB.6.0 I could in the declaration part of a form declare a public variable, then assign a value to the variable, open a new form and address the variable and read...
13
by: MJ | last post by:
as topic, if i wan to create an array where the content of the array can be edited by form1 and form2, how i going to do it? for example the content of array is {1,2,3} form2 change the content...
8
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
10
by: Phillip Vong | last post by:
Newbie learning in VB.NET 2. I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link and I want to use this querystring over and over again through out the page. Example....
11
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: ...
1
by: John Kotuby | last post by:
Hi all, In ASP.NET 2.0 I have some pages that are composed of a Master Page and several User controls. In one of the pages I declare a Public variable within the code-behind of a User Control on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.