Solved

Public Class creates System.StackOverflowException

Posted on 2004-03-26
4
662 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
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 500 points refunded.
ee_ai_construct
Community Support Moderator
(re-order part number xm34)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

9 Experts available now in Live!

Get 1:1 Help Now