Search code examples
c#sqlexceptionstack-overflow

c# stackoverflow for some strange reason in elementary code


  • I have a RoundedSum object which basically checks the price of a product and an integer sum into the DB.
  • I have an UnRoundedSum object which basically inherits RoundedSum and overrides the checkinsum function and checks the difference to nearest integer into unaccounted
  • The UnRoundedSum calls the checkinsum(productid,int) of base to check the price value into DB This is my namespace SumRounding that has 2 Classes

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);
        }
    }
}

This is let us assume a main function i have created for testing rite now since it is not working in my project .Well this is like a testing object for the above classes.

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


When i try to debug it is always caught in checkinsum() before it throws the StackOverflow error.. When i tried debugging it comes back into checkinsum() even after executing it. it keeps coming back for some reason. i dont know what could go so wrong.


Solution

  • The C# standard states that "methods in a base class are not candidates if any method in a derived class is applicable". In other words, checkinsum(int,float) will always be preferred over base.checkinsum(int,int) when calling checkinsum(1,1) because the former is in the derived class and C# allows an int to be implicitly cast to a float.

    See: https://learn.microsoft.com/en-us/archive/blogs/ericlippert/future-breaking-changes-part-three