473,738 Members | 11,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.strPubl icVariable = "ABC"
frmTest.strProp ertyVarialbe = "ABC"

Sorry for my stupid question .
Nov 21 '05 #1
3 3850
"Agnes" <ag***@dynamict ech.com.hk> wrote in message
news:O8******** ******@TK2MSFTN GP14.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.strPubl icVariable = "ABC"
frmTest.strProp ertyVarialbe = "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 ArgumentNullExc eption("value", "The value
cannot be null!.")
End If
Else
Throw New ArgumentExcepti on("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***@dynamict ech.com.hk> wrote in message
news:O8******** ******@TK2MSFTN GP14.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.strPubl icVariable = "ABC"
frmTest.strProp ertyVarialbe = "ABC"

Sorry for my stupid question .

Nov 21 '05 #3
"Agnes" <ag***@dynamict ech.com.hk> wrote in message
news:O8******** ******@TK2MSFTN GP14.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 ValueThatMUSTBe BetweenOneAndTh ree 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.Valu eThatMUSTBeBetw eenOneAndThree = 7

.... your class is in trouble!

Contrast that with

Public Property VTMBBOneAndThre e() 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 ArgumentExcepti on( ...
End Select
End Set
End Property
Private m_iIndex as Integer = 0

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

YourObject.VTMB BOneAndThree = 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
1550
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 though the form is in the class. Thanks in Advance, Laurence Nuttall
2
957
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
30101
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 it's value in my new form. Form1:
13
3618
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 to {1,2,4} form1 also can see the changes in the array where i should declare the array and wat is the format? and how am i going to call the array in form1 and form2
8
2021
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: Public Class COM Private mMyProp As String Public Property MyProp() As String
10
1873
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. Public Variable Dim myVar as string = Request.querystring("MyVar") End Public Variable
11
1936
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: Public Class MyTest
1
1469
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 the page. I populate the variable during the Page_Load event of the User Control. When I reference the variable in another part of the page, specifically in Page_Load of the main ASPX or even Page_PreRender VS tells me the variable
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.