.dll 中发生类型为“System.StackOverflowException"的未处理异常 [英] An unhandled exception of type 'System.StackOverflowException' occurred in .dll

查看:29
本文介绍了.dll 中发生类型为“System.StackOverflowException"的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发 Windows 应用程序.在我的应用程序中,我使用了一个静态类.

I'm developing a windows application in C#. In my application I've used one static class.

代码如下:

public static class clsNumber
{
     private static object vValue;

     public static object Value
     {
         get
         {
              return Value;
         }
         set
         {
              Value = value;
         }
     }

     public static string HexValue
     {
         get
         {
              try
              {
                   return Microsoft.VisualBasic.Conversion.Hex(vValue);
              }
              catch
              {
                   return Convert.ToString(vValue);
              }
         }
         set
         {
              Value = Microsoft.VisualBasic.Conversion.Val("&H" + value);
         }
     }
}

正在从另一个类设置上述类的HexValue"值.下面是代码行:

From another class the value of "HexValue" of the above mentioned class is being set. Below is the line of code:

iStick = sOutPut.Substring(0, 8);
clsNumber.HexValue = iStick;

在执行上面的代码行时,出现了下面提到的错误:

While executing the above line of code the below mentioned error is coming:

"An unhandled exception of type 'System.StackOverflowException' occurred in <.....>.dll"

谁能帮我解决这个问题?提前致谢.

Can anyone please help me with the resolution to this? Thanks in advance.

推荐答案

这里有一个非终止递归:

You have an non-terminating recursion here:

public static object Value  
{
     get
     {
          return Value;
     }
     set
     {
           Value = value;      
     }
 }

getter 中的 return Value 行递归调用 getter 并导致堆栈溢出.对于 setter 也是如此.

The line return Value in the getter calls the getter recursively and that leads to stack overflow. And similarly for the setter.

我想你的意思是:

public static object Value  
{
     get
     {
          return vValue;
     }
     set
     {
           vValue = value;      
     }
 }

我不知道在这里使用显式命名的字段返回到属性有什么好处.我会删除 vValue 并像这样声明属性:

i don't see what's to be gained from using an explicitly named field here to back to property. I'd remove vValue and declare the property like this:

public static object Value { get; set; }

这篇关于.dll 中发生类型为“System.StackOverflowException"的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆