473,569 Members | 2,480 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 #1
11 1923
"dgk" <dg*@somewhere. comschrieb
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.
No, it doesn't convert anything. The first is a public field, the second a
property.
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?
Probably a missing feature that it can not bind to public fields.
Armin

Sep 14 '07 #2
"dgk" <dg*@somewhere. comwrote in message
news:k8******** *************** *********@4ax.c om...
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
opportunity 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.

Sep 14 '07 #3
dgk
On Fri, 14 Sep 2007 08:55:28 -0500, "PvdG42" <pv**@toadstool .edu>
wrote:
>"dgk" <dg*@somewhere. comwrote in message
news:k8******* *************** **********@4ax. 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
opportunity 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 14 '07 #4
Terry wrote:
So maybe this is one of those subtle differences. I believe that
what you said was true for VB6, but obviously has changed.
This is one of the very few things that I actually preferred in VB6.

In VB5, if you created a public field and then at a later date decided you
needed to convert it to a public property, it would break binary
compatibility.

In VB6, this was changed so that the interface was identical between public
fields and properties. You could therefore create public fields in your
code, and if at a later time you realised you needed to convert them into
properties, you could just do it. Everything kept working and everyone was
happy.

Introducing the distinction between fields and properties in .NET has always
seemed like a backwards step to me. It means that if you want to safeguard
against the possibility of ever wanting to use a property in the future, you
need to code a property right from the start. The end result is dozens of
properties that do nothing more than set or retrieve the private variable
behind them.

That's a shame IMO.

--

(O)enone
Sep 14 '07 #5
Fortunatly, with code snippets, it is an easy thing to do. I have set up my
own snippet that always has a backing variable with an underscore followed by
the property name, so I only have to supply the name and type and that makes
it as easy as declaring a public variable (field).
--
Terry
"(O)enone" wrote:
Terry wrote:
So maybe this is one of those subtle differences. I believe that
what you said was true for VB6, but obviously has changed.

This is one of the very few things that I actually preferred in VB6.

In VB5, if you created a public field and then at a later date decided you
needed to convert it to a public property, it would break binary
compatibility.

In VB6, this was changed so that the interface was identical between public
fields and properties. You could therefore create public fields in your
code, and if at a later time you realised you needed to convert them into
properties, you could just do it. Everything kept working and everyone was
happy.

Introducing the distinction between fields and properties in .NET has always
seemed like a backwards step to me. It means that if you want to safeguard
against the possibility of ever wanting to use a property in the future, you
need to code a property right from the start. The end result is dozens of
properties that do nothing more than set or retrieve the private variable
behind them.

That's a shame IMO.

--

(O)enone
Sep 14 '07 #6
dgk
On Fri, 14 Sep 2007 12:18:01 -0700, Terry <Te****@nospam. nospam>
wrote:
>Fortunatly, with code snippets, it is an easy thing to do. I have set up my
own snippet that always has a backing variable with an underscore followed by
the property name, so I only have to supply the name and type and that makes
it as easy as declaring a public variable (field).
Yes, it is easy to do, although not as easy as just Public MyVar as
String. However, properties take up a lot more screen real estate than
a one line declaration. Four "fields" expand to thirty lines pretty
quickly.

Since the vast majority of my properties are string (and the default
snippet is integer) I set up my own that at least makes a string
property.
Sep 14 '07 #7
Armin,

Probably you know this, properties are optimezed by the runtime system in a
way that they act as fields (Not all that what we see in the debugger).

Cor

"Armin Zingler" <az*******@free net.deschreef in bericht
news:un******** ******@TK2MSFTN GP04.phx.gbl...
"dgk" <dg*@somewhere. comschrieb
>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.

No, it doesn't convert anything. The first is a public field, the second a
property.
>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?

Probably a missing feature that it can not bind to public fields.
Armin
Sep 15 '07 #8
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****** *************** ***********@4ax .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
opportunity 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 15 '07 #9
"Cor Ligthert[MVP]" <no************ @planet.nlschri eb
Armin,

Probably you know this, properties are optimezed by the runtime
system in a way that they act as fields
Yes, but not in the debug version. I don't create overhead just because of
knowing that it might be optimized away or because tools ignore public
fields.

I fully agree with dgk. (his post from 17:10)
(Not all that what we see in
the debugger).
Pardon?
Armin

Sep 15 '07 #10

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

Similar topics

1
1535
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
10878
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...
3
3845
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
2479
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...
9
8861
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
2009
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
1863
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
1972
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
1670
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
7615
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...
0
7924
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. ...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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...
0
5219
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...
0
3653
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...
1
2115
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
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
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...

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.