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

property assignment in vb.net

My first simple attempt at using a property. and I get an exception "An
unhandled exception of type 'System.StackOverflowException' occurred in
BalanceOmatic.exe" on line xxx. I have included the property code and the
button event used to set and call the property value. Any help greatly
appreciated.

What am I missing?

Thanks
Mike
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click

User.MyProperty = "Property Set"

MsgBox(User.MyProperty & " User.MyProperty")

End Sub
Public Property MyProperty() As String
Get
MyProperty = MyProperty
End Get
Set(ByVal Value As String)
Line xxx MyProperty = Value
End Set
End Property
Aug 8 '05 #1
5 2154
Your property is recursive. Instead of setting a private variable, you are
just assigning the value to the property itself - which just calls it over
and over again.

Your GET btw, could only return a boolean of whether or not the property is
equal to itself. Not only will this recursively call itself until you get a
StackOverflowException, but the return of that would actually be a Boolean
(if it was capable of ever ending), and not a string at all. Which means you
should turn Option Strict On.

You need to define your property like this:

private _myProp As String

Public Property MyProperty() As String
Get
Return _myProp
End Get
Set(ByVal Value As String)
_myProp = Value
End Set
End Property

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
My first simple attempt at using a property. and I get an exception "An
unhandled exception of type 'System.StackOverflowException' occurred in
BalanceOmatic.exe" on line xxx. I have included the property code and
the
button event used to set and call the property value. Any help greatly
appreciated.

What am I missing?

Thanks
Mike
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click

User.MyProperty = "Property Set"

MsgBox(User.MyProperty & " User.MyProperty")

End Sub
Public Property MyProperty() As String
Get
MyProperty = MyProperty
End Get
Set(ByVal Value As String)
Line xxx MyProperty = Value
End Set
End Property

Aug 8 '05 #2
The following line is causing the stack overflow because the property is
calling itself (infinite recursion):

MyProperty = MyProperty

You need to create an underlying variable for the property to hold the value
of the property. The example at the bottom of the following link shows you
exactly how to do this:
http://msdn.microsoft.com/library/de...tmproperty.asp

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk
"Mike" wrote:
My first simple attempt at using a property. and I get an exception "An
unhandled exception of type 'System.StackOverflowException' occurred in
BalanceOmatic.exe" on line xxx. I have included the property code and the
button event used to set and call the property value. Any help greatly
appreciated.

What am I missing?

Thanks
Mike
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click

User.MyProperty = "Property Set"

MsgBox(User.MyProperty & " User.MyProperty")

End Sub
Public Property MyProperty() As String
Get
MyProperty = MyProperty
End Get
Set(ByVal Value As String)
Line xxx MyProperty = Value
End Set
End Property

Aug 8 '05 #3

I presume User is a class? although its not that relevant if it isnt.

have you dim'd a private variable to hold the properties value?

The 'Get' is coded incorrectly, it should return the private variables
contents!

Hope this helps. See below for code changes

PRIVATE _myProperty as string
Public Property MyProperty() As String
Get
RETURN _myProperty
End Get Set(ByVal Value As String)
Line xxx _myProperty = Value
End Set
End Property

Aug 8 '05 #4
Thanks for the reply. That was it of course. One more quick question. Why
the underscore in _myProp is that a property convention?

Thanks
Mike

"Marina" wrote:
Your property is recursive. Instead of setting a private variable, you are
just assigning the value to the property itself - which just calls it over
and over again.

Your GET btw, could only return a boolean of whether or not the property is
equal to itself. Not only will this recursively call itself until you get a
StackOverflowException, but the return of that would actually be a Boolean
(if it was capable of ever ending), and not a string at all. Which means you
should turn Option Strict On.

You need to define your property like this:

private _myProp As String

Public Property MyProperty() As String
Get
Return _myProp
End Get
Set(ByVal Value As String)
_myProp = Value
End Set
End Property

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
My first simple attempt at using a property. and I get an exception "An
unhandled exception of type 'System.StackOverflowException' occurred in
BalanceOmatic.exe" on line xxx. I have included the property code and
the
button event used to set and call the property value. Any help greatly
appreciated.

What am I missing?

Thanks
Mike
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogin.Click

User.MyProperty = "Property Set"

MsgBox(User.MyProperty & " User.MyProperty")

End Sub
Public Property MyProperty() As String
Get
MyProperty = MyProperty
End Get
Set(ByVal Value As String)
Line xxx MyProperty = Value
End Set
End Property


Aug 8 '05 #5
The only reason I did that, is because VB is case insensitive. In C#, you
can have a private variable named 'someProperty' and a property named
'SomeProperty', and that all works.

In VB, those would be the same name, and you can't have that. So one
convention is to give private fields exposed as properties an underscore, so
you can have unique names.

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
Thanks for the reply. That was it of course. One more quick question.
Why
the underscore in _myProp is that a property convention?

Thanks
Mike

"Marina" wrote:
Your property is recursive. Instead of setting a private variable, you
are
just assigning the value to the property itself - which just calls it
over
and over again.

Your GET btw, could only return a boolean of whether or not the property
is
equal to itself. Not only will this recursively call itself until you get
a
StackOverflowException, but the return of that would actually be a
Boolean
(if it was capable of ever ending), and not a string at all. Which means
you
should turn Option Strict On.

You need to define your property like this:

private _myProp As String

Public Property MyProperty() As String
Get
Return _myProp
End Get
Set(ByVal Value As String)
_myProp = Value
End Set
End Property

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
> My first simple attempt at using a property. and I get an exception "An
> unhandled exception of type 'System.StackOverflowException' occurred in
> BalanceOmatic.exe" on line xxx. I have included the property code
> and
> the
> button event used to set and call the property value. Any help greatly
> appreciated.
>
>
>
> What am I missing?
>
> Thanks
> Mike
>
>
> Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnLogin.Click
>
> User.MyProperty = "Property Set"
>
> MsgBox(User.MyProperty & " User.MyProperty")
>
> End Sub
>
>
> Public Property MyProperty() As String
> Get
> MyProperty = MyProperty
> End Get
> Set(ByVal Value As String)
> Line xxx MyProperty = Value
> End Set
> End Property


Aug 8 '05 #6

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

Similar topics

0
by: Tom L | last post by:
I'm doing a simple property assignment: oOrder.Customer = frmcust.CustomerData frmcust is a dialog customer form - I put a stop on that line, and frmcust.customerdata.name contains the customers...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
3
by: Phil Mc | last post by:
Hi has anyone come accross the problem.... with referance to System.Web.Mail.MailMessage and System.Web.Mail.SmtpMail THIS WORKS FINE mail=new MailMessage(); mail.From =...
5
by: Mike | last post by:
My first simple attempt at using a property. and I get an exception "An unhandled exception of type 'System.StackOverflowException' occurred in BalanceOmatic.exe" on line xxx. I have included...
3
by: whornak | last post by:
I am building a class template that forces the user to override functions/subs. I need to force the developer to set the value of a property also but don't know how. How do I force the...
1
by: kristoph | last post by:
Greetings, I would like to be able to take some action (implemented in a function) when a property is assigned a new value (much a page is re- rendered when, for example, a style is changed). ...
3
by: vunet | last post by:
Hello, I've just installed ASPXMLRPC library and testing their main function: xmlRPC ("URL", "command_name", params) The function converts all parameters to XML, sends a request to third-...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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.