473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Public variable not the same as using a property?

dgk
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
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?
Sep 14 '07
11 1931
dgk,

My message could be read wrong, what I mean is.

Why are you asking here questions while you have already in "advance" your
"own" incorrect answers and start discussing those as true?

Cor

"Cor Ligthert[MVP]" <no************ @planet.nlschre ef in bericht
news:E9******** *************** ***********@mic rosoft.com...
dgk,

What do you want to ask when you have already your (incorrect) answers.

Cor

"dgk" <dg*@somewhere. comschreef in bericht
news:qn******** *************** *********@4ax.c om...
>On Fri, 14 Sep 2007 08:55:28 -0500, "PvdG42" <pv**@toadstool .edu>
wrote:
>>>"dgk" <dg*@somewhere. comwrote in message
news:k8***** *************** ************@4a x.com...
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
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?

From a design perspective, they are very different. And IMHO, you should
consider the difference.
If you declare the variable as public, you are giving unrestricted access
to
that variable to code outside the class, thus exposing the variable to
potential corruption and breaking encapsulation. By declaring the
variable
private, then supplying a public property in your class, you have the
opportunit y and ability to control access to that variable by either not
providing a Set block, or by adding logic in the Set block that will
disallow illogical values.

Well sure, but if you don't care what comes in or goes out, then it's
a lot cleaner to just declare a public variable. Also quicker to type.
One line instead of six or seven (even if using a snippet).

One of the things I don't like about VB is that Readonly on the
property. I mean, if there's no Set, then it's readonly. I'm sure they
did that for a reason but I can't figure out why.
Well, anyway, if you're planning on using Databind, don't use public
properties.
Sep 16 '07 #11
dgk
On Mon, 17 Sep 2007 01:46:39 +0200, "Cor Ligthert[MVP]"
<no************ @planet.nlwrote :
>dgk,

My message could be read wrong, what I mean is.

Why are you asking here questions while you have already in "advance" your
"own" incorrect answers and start discussing those as true?

Cor

"Cor Ligthert[MVP]" <no************ @planet.nlschre ef in bericht
news:E9******* *************** ************@mi crosoft.com...
I'm not sure what you mean by discussing my own incorrect answers as
being true. I spent quite some time (a few hours) puzzling out why a
generic List (of X) wouldn't work as a datasource for a gridview. It
seemed to have built in rows and columns and it seemed to me that it
should work. I thought that perhaps I could save someone else from
wasting time.
>dgk,

What do you want to ask when you have already your (incorrect) answers.

Cor

"dgk" <dg*@somewhere. comschreef in bericht
news:qn******* *************** **********@4ax. com...
>>On Fri, 14 Sep 2007 08:55:28 -0500, "PvdG42" <pv**@toadstool .edu>
wrote:

"dgk" <dg*@somewhere. comwrote in message
news:k8**** *************** *************@4 ax.com...
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
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
>
>
I always thought so, in fact, I thought that the complier converted
the first into the second.
>
Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.
>
Does anyone know why?

From a design perspective, they are very different. And IMHO, you should
consider the difference.
If you declare the variable as public, you are giving unrestricted access
to
that variable to code outside the class, thus exposing the variable to
potential corruption and breaking encapsulation. By declaring the
variable
private, then supplying a public property in your class, you have the
opportuni ty and ability to control access to that variable by either not
providing a Set block, or by adding logic in the Set block that will
disallow illogical values.

Well sure, but if you don't care what comes in or goes out, then it's
a lot cleaner to just declare a public variable. Also quicker to type.
One line instead of six or seven (even if using a snippet).

One of the things I don't like about VB is that Readonly on the
property. I mean, if there's no Set, then it's readonly. I'm sure they
did that for a reason but I can't figure out why.
Well, anyway, if you're planning on using Databind, don't use public
properties.
Sep 17 '07 #12

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

Similar topics

1
1546
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
3
10882
by: Joe Fromm | last post by:
Perhaps I'm missing something obvious, but I've been curious about one of the coding practices I see advocated. I'm a longtime C/C++ programmer trying to learn C#, and I started looking around for coding standards/best practices. Several of the documents I've read require that all members be private (or protected), and the get/set properties be provided for any elements exposed to the outside world. In C++ this makes perfect sense,...
3
3848
by: Agnes | last post by:
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 .
4
2494
by: Nick Dreyer | last post by:
Is it possible to see public class variables of a COM addin in Excel 97 VBA? I have successfully created the (Visual Basic 2003 .NET) COM and referenced it in an Excel 97 VBA project. The VBA object browser sees - and the project otherwise successfully interacts with - all the COM addin methods and properties, but none of the public variables - target, init and size in the code below - can be accessed. When I reference the exact same...
9
8873
by: Stefan De Schepper | last post by:
Should I use: Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal Value As String) m_Name = Value
8
2019
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
1871
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
4
1976
by: RP | last post by:
I have a class file (Global.cs) containing following variable: Public Int32 TotalRecords=0 I have a Windows Form from where I am assigning a value to this value as below: private void SaveRecord() { Global objGlob = new Global();
11
1675
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't see how to relate them so this will work. This user control defines the properties:
0
8680
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
8609
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
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
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
8871
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
6528
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
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.