Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to create a object based on the input parameter received in a method.
there two possible objects I need to create. Both objects will be having 9 same properties & just one property will be different based on the input received.

C#
public SomeDisocuntObject GetDiscountDetails(string discountType)
{
if(discountType==PERCENTAGEDISCOUNT)
{
//create a object which has Discount% property along with other some same props. & return obj
}
if(discountType==AMOUNTDISCOUNT)
{
//create a object which has DiscountAmount property along with other some same props. & return obj

}

}


What I have tried:

How to achieve this logic, I think Inheritance will solve this problem. any code snippet will be appreciated.
Posted
Updated 20-Sep-22 1:12am
Comments
Richard MacCutchan 20-Sep-22 7:09am    
Just add the extra property, and define the values as an enum type.
Virendra S from Bangalore, Karnataka 20-Sep-22 8:06am    
What to do you mean by Just add the extra property? any reference pls. how can I remove the the existing property of a class
Richard MacCutchan 20-Sep-22 8:55am    
I assume that you have some variable in the class that contains a number that represents either the percentage or the value of the discount. If that is the case then you just need another property which identifies which discount type that value holds.

Use a switch rather than if - it's "tidier" code.
Then just use an initialiser:
C#
switch (discountType)
   {
   default: throw new ArgumentException($"Unknown discount type \"{discountType}\"");
   case PERCENTAGEDISCOUNT:
      return new Discount{Discount = myDiscPercent, ...};
   case AMOUNTDISCOUNT:
      return new Discount{DiscountAmount = myDiscAmount, ...};
   ...
   }
 
Share this answer
 
Comments
Himansh jain 20-Sep-22 7:20am    
Sir, can you please debug this code..I am not able to download the correct file.
Please refer to the question below.

https://www.codeproject.com/Questions/5342610/How-do-I-download-a-file-in-xls-format
Richard MacCutchan 20-Sep-22 7:27am    
Ignore above comment, OP thinks Excel files are text.
OriginalGriff 20-Sep-22 7:39am    
Ah. Research not his strong point then ... :sigh:
Richard MacCutchan 20-Sep-22 7:41am    
See also Solution 1.
Himansh jain 20-Sep-22 7:50am    
Sir, actually when I am directly hitting the API, I am able to view the file .
But when I am hitting it from UI

$scope.download = function (fileName) {
debugger
if (fileName != null && fileName != '' && fileName != undefined) {
URIService.GetData(URIService.GetDownloadExcelFileUrl(fileName))
.success(function (data) {
var file = new Blob([data], {
type: 'application/vnd.ms-excel'
});
FileSaver.saveAs(file, fileName, true); //I need help over this piece of code , when the data is being returned from api , while saving that data..format issue arises.

}).error(function (data, status, headers, config) {
var errorMessage = "";
if (status == "404" || data == undefined) {
errorMessage = "Menu.FileNotFound"
}
else {
errorMessage = "Menu.Error_Message"
}
});
class Discount
{
prop {get;set;} //some random properties
string PERCENTAGEDISCOUNT {get;set;}
string AMOUNTDISCOUNT {get;set;}

}

public Discount GetDiscountDetails(string discountType)
{
   Discount dis = new Discount();
if(discountType==PERCENTAGEDISCOUNT)
{
   dis.(your properties)
//create a object which has Discount% property along with other some same props. & return obj
}
if(discountType==AMOUNTDISCOUNT)
{
  dis.(your properties)
//create a object which has DiscountAmount property along with other some same props. & return obj

}
return dis

}
 
Share this answer
 
Comments
Richard MacCutchan 20-Sep-22 7:10am    
Why would you use strings for simple property values?
Himansh jain 20-Sep-22 7:12am    
anything can be used ..by default I put the strings..it can be modified according to his requirement.
Virendra S from Bangalore, Karnataka 20-Sep-22 7:34am    
@Himansh when I return dis object it will be having both props PERCENTAGEDISCOUNT & AMOUNTDISCOUNT, am I right?

Actually this return object will be passed to some other libray method to do some calculations. so that library expects single property in a object, it can be either PERCENTAGEDISCOUNT or AMOUNTDISCOUNT. not both props in the dis object.
Himansh jain 20-Sep-22 7:41am    
you can try putting null check

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900