471,339 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 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 2075
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tom L | last post: by
16 posts views Thread by Edward Diener | last post: by
5 posts views Thread by Mike | last post: by
3 posts views Thread by whornak | last post: by
6 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by

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.