473,228 Members | 1,771 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,228 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 2147
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...

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.