c#stackoverflow在基本代码中有一些奇怪的原因

时间:2013-01-31 18:32:30

标签: c# sql exception stack-overflow

  • 我有一个RoundedSum对象,它基本上会检查产品的价格和数据库中的整数和。
  • 我有一个UnRoundedSum对象,它基本上继承了RoundedSum并覆盖了checkinsum函数,并将差异检查到最接近的整数为unaccounted
  • UnRoundedSum调用base的checkinsum(productid,int)来检查 价格值进入DB这是我的命名空间SumRounding有2 类

namespace DatabasePricing.SumRounding
{
    public class Roundedsum
    {
        public void checkinsum(int productid,int sum)
        {
            //Checks in price in the price table
            //dbobject("price",productid,sum);
            int temp_int = sum;    
        }
    }

    public class UnRoundedSum : Roundedsum
    {
        public void checkinsum(int productid,float sum)
        {
            //Since the sum is a float it will check the difference 
            //into unroundedsum table in the database 
            int intsum = (int)sum;
            float tempfloat = sum - intsum;
            //Check this remaining float into the database under unaccounted
            // dbobject("unroundedsum",productid,tempfloat);                
            //Now call the integer checksum with the integer value
            checkinsum(productid,intsum);
        }
    }
}

这是让我们假设我现在为测试rite创建的一个主要函数,因为它在我的项目中不起作用。这就像上面类的测试对象。<​​/ strong>

using DatabasePricing.SumRounding;
    namespace DatabasePricing
    {
        class testingrounding
        {
            static void Main() { 
            int product_id = 1;
            float float_value = 1.1f;
            UnRoundedSum obj1 = new UnRoundedSum();
            //This call produces StackOverflow Exception
            obj1.checkinsum(1, float_value);
            int price = 200;
            //I tried with integer value to test RoundedSum object
            //it is still throwing an exception
            //This call also produces StackOverflow Exception
            obj1.checkinsum(1, price);        
            }
        }
    }

Stack Overflow Exception Image


当我尝试调试时,它始终在checkinsum()中被捕获,然后才会抛出StackOverflow错误。当我尝试调试时,即使执行它也会返回到checkinsum()。它因某种原因不断回归。我不知道会出现什么问题。

3 个答案:

答案 0 :(得分:4)

    checkinsum(productid,intsum);

应该是

    base.checkinsum(productid,intsum);

在UnRoundedSum类

编辑:Explination,没有基础。 (这是去基类,然后在那里调用方法)它会在UnRoundedSum中调用自己,所以它将是一个无限循环,这将导致stackoverflow

EDIT2:

阅读你的评论后,我想你想要这个:

public class sum
{
    public void checkinsum(int productid, float sum)
    {
        //Since the sum is a float it will check the difference 
        //into unroundedsum table in the database 
        int intsum = (int)sum;
        float tempfloat = sum - intsum;
        //Check this remaining float into the database under unaccounted
        // dbobject("unroundedsum",productid,tempfloat);                
        //Now call the integer checksum with the integer value
    }
    public void checkinsum(int productid, int sum)
    {
        //Checks in price in the price table
        //dbobject("price",productid,sum);
        int temp_int = sum;
    }
}

然后它将执行您想要的方法,或者它是int int或int float。

答案 1 :(得分:1)

您在checkinsum中进行了无限递归调用。

您可以在base.checkinsum

中致电UnRoundedSum.checkinsum
public void checkinsum(int productid,float sum)
{
    //Since the sum is a float it will check the difference 
    //into unroundedsum table in the database 
    int intsum = (int)sum;
    float tempfloat = sum - intsum;
    //Check this remaining float into the database under unaccounted
    // dbobject("unroundedsum",productid,tempfloat);                
    //Now call the integer checksum with the integer value
    base.checkinsum(productid,intsum);
}

答案 2 :(得分:1)

C#标准规定“如果派生类中的任何方法适用,则基类中的方法不是候选者”。换句话说,checkinsum(int,float)在调用base.checkinsum(int,int)时始终优先于checkinsum(1,1),因为前者位于派生类中,C#允许int隐式强制转换为{ {1}}。

请参阅:http://blogs.msdn.com/b/ericlippert/archive/2007/09/04/future-breaking-changes-part-three.aspx