Reputation: 637
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);
}
}
}
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.
Upvotes: 0
Views: 187
Reputation: 126
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
Upvotes: 1
Reputation: 1762
checkinsum(productid,intsum);
should be
base.checkinsum(productid,intsum);
In UnRoundedSum class
EDIT: Explination, without the base. (which is go to Base Class, and then call the method there) it will call itself in UnRoundedSum, so it will be an endless loop which will cause the stackoverflow
EDIT2:
After reading your comments I think you want this:
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;
}
}
Then it will do the method you want or it is int int or int float.
Upvotes: 4
Reputation: 13022
You have an infinite recursive call in checkinsum
.
You may want to call base.checkinsum
in 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);
}
Upvotes: 1