473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB Properties - private data member optional?

I want to use a property without a private data member. This is
supposed to be legal, except that I get an Stack Overflow Exception
when I implicity use the set method. Here's the code:

Option Explicit
Option Strict On

Imports System
Public Class PropertyT

' Private LocalAge as Integer = 24

Public Property LocalAge() As Integer

Get
Return LocalAge
End Get

Set (ByVal value As Integer)
LocalAge = value
End Set

End Property

End Class
Public Class PropertyConsume r

Public Shared Sub Main()

Try
Dim t As New PropertyT()
Console.WriteLi ne("t's Age is {0}", t.LocalAge)
t.LocalAge = 25
Console.WriteLi ne("t's Age is {0}", t.LocalAge)
Catch e As StackOverflowEx ception
Console.WriteLi ne(e.Message)
End Try

End Sub

End Class

=========

And here's the output.

t's Age is 0
Exception of type System.StackOve rflowException was thrown.
Jul 19 '05 #1
5 3988
You are recursively calling your property until the StackOverflow exception
is thrown. I'm pretty sure that is not what you want to happen.
"YellowDog" <Ye*******@volc anomail.com> wrote in message
news:e5******** *************** ***@posting.goo gle.com...
I want to use a property without a private data member. This is
supposed to be legal, except that I get an Stack Overflow Exception
when I implicity use the set method. Here's the code:

Option Explicit
Option Strict On

Imports System
Public Class PropertyT

' Private LocalAge as Integer = 24

Public Property LocalAge() As Integer

Get
Return LocalAge
End Get

Set (ByVal value As Integer)
LocalAge = value
End Set

End Property

End Class
Public Class PropertyConsume r

Public Shared Sub Main()

Try
Dim t As New PropertyT()
Console.WriteLi ne("t's Age is {0}", t.LocalAge)
t.LocalAge = 25
Console.WriteLi ne("t's Age is {0}", t.LocalAge)
Catch e As StackOverflowEx ception
Console.WriteLi ne(e.Message)
End Try

End Sub

End Class

=========

And here's the output.

t's Age is 0
Exception of type System.StackOve rflowException was thrown.

Jul 19 '05 #2
YellowDog <Ye*******@volc anomail.com> wrote:
I want to use a property without a private data member.
That's fine - although you'll need *something* to back the member, if
you want to be able to set it.
This is
supposed to be legal, except that I get an Stack Overflow Exception
when I implicity use the set method. Here's the code:
Public Property LocalAge() As Integer

Get
Return LocalAge
End Get

Set (ByVal value As Integer)
LocalAge = value
End Set


That's effectively declaring two methods, each of which call themselves
recursively. Where were you expecting the data to be stored, without a
field of some description backing it?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #3
from the ole class builder in VB6 I always follow the form
of a private local copy of the property. While you do
need this if its calculated, most of the time this is the
way I accomplish this. (You are calling the property from
within the property itself. Infinite recursion = stack
overflow)

Option Explicit
Option Strict On

Imports System
Public Class PropertyT

Public Class PropertyConsume r
'************** ***
Private mvarLocalAge As Integer
'************** ***

Get
Return mvarLocalAge '*******
End Get

Set (ByVal value As Integer)
mvarLocalAge = value '******
End Set
-----Original Message-----
I want to use a property without a private data member. This issupposed to be legal, except that I get an Stack Overflow Exceptionwhen I implicity use the set method. Here's the code:

Option Explicit
Option Strict On

Imports System
Public Class PropertyT

' Private LocalAge as Integer = 24

Public Property LocalAge() As Integer

Get
Return mvarLocalAge '*******
End Get

Set (ByVal value As Integer)
mvarLocalAge = value '******
End Set

End Property

End Class
Public Class PropertyConsume r

Public Shared Sub Main()

Try
Dim t As New PropertyT()
Console.WriteLi ne("t's Age is {0}", t.LocalAge) t.LocalAge = 25
Console.WriteLi ne("t's Age is {0}", t.LocalAge) Catch e As StackOverflowEx ception
Console.WriteLi ne(e.Message)
End Try

End Sub

End Class

=========

And here's the output.

t's Age is 0
Exception of type System.StackOve rflowException was thrown..

Jul 19 '05 #4
Randy wrote:
You are recursively calling your property until the StackOverflow exception is thrown. <<
When I saw the Stack Overflow, I immediately thought "inifinite loop",
but then I immediately dismissed it (foolishly) because there are no
loops. After I posted and went and did something else, it came to mind
that I ought to check the IL. That's still an execrcise I plan to
pursue. I'm relatively new to .NET and learning how to disassemble IL
is definitely something I'll need to know.

Jon wrote:
That's effectively declaring two methods, each of which call themselves recursively. Where were you expecting the data to be
stored, without a field of some description backing it? <<

I come froma Java background, where a property is not so much a formal
language construct as it is a coding convention. C# it seems, follows
a similar pattern. Where did I expect the data to be stored? I thought
that VB, through the Property keyword, would behind-the-scenes define
a variable named and typed the same as declared in the Propetrry
statement itself. That belief was re-enforced when I read the docs and
saw that a private data member is optional. It was further re-enforced
when I tried to declare a private data member named and typed the same
as the property and the compiler complained he already had one of
those.

nospam wrote:
You are calling the property from within the property itself.

Infinite recursion = stack overflow. <<

From within the property itself ... I guess that's what I don't
understand. Then exactly what ~is~ a property; what does the Property
statement do; and where are you when you're "within the property"? I
mean can I assume any behavior other than what's coded in the get and
set methods?

I need to track down a more advanced text than the ones I have, either
that or dig into the IL.

The immediate solution is to code a private data member, or just
follow the Java convention of getter and setter methods.

Thanks folks!
Jul 19 '05 #5
>> It's exactly the same as in Java - you don't *have* to have a
private field for a property there either, but you almost always do.
If a field was provided for you by the property, why would anyone
bother including their own? <<

Exactly my thinking. Anyway, from examining the IL I can see that now.
It's just that I read way too much into the Property keyword, thinking
that it would automatically define a private member variable for me
and that that would be the real "syntactic sugar".
I suggest that you learn C# if you know Java - it's closer to Java

than VB.NET is, and the C# specification is pretty good on this kind
of thing. <<

Yeah, except that I got this certification thing going on, at least
that's the plan. I'm not totlly alien to MS technologies, having
worked in VB6 and ASP in the past. Now I'm concentrating on VB and the
Framework since C#, to me, is what Java would have been had Sun
devloped it in the late 90's instead of when they did. What I mean is
that C# seems like a natural evolution of Java, much like Java was a
natural evolution what came before it.
Jul 19 '05 #6

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

Similar topics

22
12007
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is almost never an excuse for accessor methods. Personally, I do not go that far. I do feel that they serve a useful purpose (albeit in a limited...
5
328
by: YellowDog | last post by:
I want to use a property without a private data member. This is supposed to be legal, except that I get an Stack Overflow Exception when I implicity use the set method. Here's the code: Option Explicit Option Strict On Imports System
15
1361
by: Gary Morris | last post by:
Hello all, OK, first of all I have known about properties since VB6, which I have and have used extensively. It seems that property get and set are basically the same concept in C# and VB.NET, so no problem there. In programming with VB6, I never wrote any program that used properties, and to date I still haven't ever used them in C#. What...
4
2430
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. The first class handles the form generation - (this was done using GUI form designer).
18
2077
by: Janaka | last post by:
I'm having a discussion with my colleagues here on good programming standards. One thing we haven't agreed on is the use of properties in classes vs using member variables. Now everyone knows that it is useful to use a property when it has to do some further action on the data such as private double salesAvg; public double SalesAverage {...
3
10877
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...
8
1707
by: Chuck Bowling | last post by:
Is there any justification - from an OOP perspective - for wrapping an attribute in a Property beyond the ability to restrict access? private int myInt; public int MyInt { get { return myInt; } set { myInt = value; } }
8
2732
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that expose the variables or is it OK to just access the variables directly? Keep in mind that I am talking about accessing the variables from within the...
39
1913
by: Paul Mcilreavy | last post by:
Hi, i would like to get some view points on the use of properties within classes. My belief is that properties should generally be used to return values of private members. They should not do anything that is likely to return an error and they should not instantiate or return any big objects etc. My company seems to be using them for...
0
7521
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...
0
7451
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...
1
7473
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...
0
7810
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...
0
6044
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...
0
5088
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...
1
1944
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
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
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.