473,386 Members | 1,674 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,386 software developers and data experts.

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 1910
"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.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.

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*******@freenet.deschreef in bericht
news:un**************@TK2MSFTNGP04.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.com...
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.nlschrieb
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
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.nlschreef in bericht
news:E9**********************************@microsof t.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.com...
>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 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.nlschreef in bericht
news:E9**********************************@microso ft.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********************************@4ax.co m...
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 17 '07 #12

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...
3
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...
3
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"...
4
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...
9
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
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....
4
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...
11
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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...
0
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,...
0
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...

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.