Solved

Public Class creates System.StackOverflowException

Posted on 2004-03-26
4
664 Views
Last Modified: 2013-11-23
I've created a public class with some shared properties that I'll set-up initially in my solution and then use the values in different parts of the solution.

It's possible, if the app.config file isn't set correctly that a blank value could propogate to the shared value.  To prevent this, I've gone for a 'belt & braces' approach where the property has a default value, but also check the value that's sent to make sure it's not blank.

A subset of the  class below;

  Public Class dbInfo
    Private Const dbPathDef As String = "C:\Program Files\Database"

    <System.ComponentModel.Description("The directory path to the database."), _
      System.ComponentModel.DefaultValue(dbPathDef)> _
    Public Shared Property dbPath() As String
      Get
        Return dbPath
      End Get
      Set(ByVal Value As String)
        If Len(Value) = 0 Then
          dbPath = dbPathDef
        Else
          dbPath = Value
        End If
      End Set
    End Property

Although the code compiles ok, it throws the following exception at the 'If Len(Value) = 0 Then' point

An unhandled exception of type 'System.StackOverflowException' occurred in microsoft.visualbasic.dll

I've tried other string comparison methods (Value = "", Value Is Nothing, Value <> "") but still get the same exception thrown

The exception is still produced if I remove the attribute settings.
0
Comment
Question by:MarkNethercott
4 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 10688194
Represents the error that occurs when the execution stack overflows due to too many method calls.

Description
[Note: StackOverflowException is thrown for execution stack overflow errors, typically in the case of a very deep or unbounded recursion.
The localloc IL instruction throws StackOverflowException.

I guess its too complicated ??????
0
 

Author Comment

by:MarkNethercott
ID: 10694857
Fixed it.  Bit daft really, the System.StackOverflowException was being thrown because of deep recursion, which (I think) was caused because I was setting a value directly to a shared property and for some reason caused the code to go round in a circle - setting the value must set up a call to the property (I guess).

I fixed the problem by defining a Private Shared variable and when the property is called for a set or get then the private shared variable gets changed by the public shared property - actually sensible when you think about it.  A good reference article is;

Microsoft Knowledge Base Article - 308371
HOW TO: Create and Use Shared Members by Using Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308371&ID=kb;en-us;Q308371&SD=MSDN

0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 10724593
Closed, 500 points refunded.
ee_ai_construct
Community Support Moderator
(re-order part number xm34)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now