rgb192
asked on
require parameter to be of a certain type
object oriented programming
customer{ writeGoodReview(business){ if business =='thisBusiness', thisBusiness is thankful}}
how to write without if statement
maybe have parameter business be another method or class instead of doing an if statement in the method
require parameter to be of a certain type
customer{ writeGoodReview(business){
how to write without if statement
maybe have parameter business be another method or class instead of doing an if statement in the method
require parameter to be of a certain type
Can you write it as a if statement so we can understand what the code is to be doing, I am not understanding your pseudo code.
If I understand what you are trying to do then yes you would want to write individual business classes that all extended a business class. Then each of those classes would implement their own writegoodreview method. You would then not need an if statement,because this business would have its own definition of that method
Hope this helps and is what you are looking for.
Hope this helps and is what you are looking for.
assuming that 'thankful' is a boolean property of class Business you may assign
to the property.
Sara
((business == thisBusiness) || thisBusiness.thankful)
to the property.
Sara
To put a face to what Jeffery is saying; consider the following -
Using a base class only:
-saige-
Using a base class only:
using System;
using System.Collections.Generic;
using System.Linq;
namespace EE_Q28654594
{
class Program
{
static readonly List<Business> businesses = new List<Business>();
static void Main(string[] args)
{
businesses.AddRange((from i in Enumerable.Range(1, 10) select new Business() { ID = i, Name = string.Format("Business{0}", i) }));
foreach (var item in businesses.Where(b => b.ID % 2 == 0))
Console.WriteLine(item.WriteGoodReview());
Console.ReadLine();
}
}
class Business
{
public int ID { get; set; }
public string Name { get; set; }
public virtual string WriteGoodReview()
{
return string.Format("Base business - {0}; is thankful", Name);
}
}
}
Produces the following output -Now lets add some child classes (namely Small, Medium and Large) -
using System;
using System.Collections.Generic;
using System.Linq;
namespace EE_Q28654594
{
class Program
{
static readonly List<Business> businesses = new List<Business>();
static void Main(string[] args)
{
businesses.AddRange((from i in Enumerable.Range(1, 10)
select i % 3 == 0 ? new LargeBusiness() { ID = i, Name = string.Format("Business{0}", i) }
: i % 2 == 0 ? new MediumBusiness() { ID = i, Name = string.Format("Business{0}", i) }
: (Business)(new SmallBusiness() { ID = i, Name = string.Format("Business{0}", i) })));
foreach (var item in businesses)
Console.WriteLine(item.WriteGoodReview());
Console.ReadLine();
}
}
class Business
{
public int ID { get; set; }
public string Name { get; set; }
public virtual string WriteGoodReview()
{
return string.Format("Base business - {0}; is thankful", Name);
}
}
class SmallBusiness : Business
{
public override string WriteGoodReview()
{
return string.Format("Small business - {0}; is overjoyed", Name);
}
}
class MediumBusiness : Business
{
public override string WriteGoodReview()
{
return string.Format("Medium business - {0}; is grateful", Name);
}
}
class LargeBusiness : Business
{
public override string WriteGoodReview()
{
return string.Format("Large business - {0}; is thankful", Name);
}
}
}
Now produces the following output --saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (business == thisBusiness) ...
how to write without if statement
if I understand the original question correctly, it is how to write the function WriteGoodReview(ref Business business) without using an if statement.
WriteGoodReview(ref Business business)
{
thisBusiness.WriteGoodReview(business == thisBussiness);
the above "solution" would replace the if statement in the first place, but would require an if or ?: or switch/case in the next function call what rarely is that what is required.
if you try use a virtual call for to replace an if statement the same difficulties appear. obviously the derived class object which you call via base class reference already must have the property (implicit or explicit) that meets the if condition to be replaced. but can we already have a derived class object which already would have the property (business == thisBusiness)?
BusinessMessage bm;
if (business == thisBusiness)
bm = (BusinessMessage)new ThankFulMessage;
else
bm = (BusinessMessage)new ThankLessMessage;
bm.WriteGoodReview();
since the above code has the same if statement, it again is not a solution.
we would need some kind of a virtual constructor which would create ThankFulMessage if the argument is true, and ThankLessMessage if the argument is false. a virtual constructor could be provided in c++ by implementing factory pattern what is a rather complex way. a possible alternative could be to create both objects and use only the right one:
BusinessMessage[2] bms;
bms[0] = (BusinessMessage)new ThankLessMessage;
bms[1] = (BusinessMessage)new ThankFulMessage;
bms[(int)(business == thisBusiness)].WriteGoodReview();
ThankLessMessage::WriteGoo
Sara
ASKER
https://www.experts-exchange.com/questions/28654594/require-parameter-to-be-of-a-certain-type.html?anchorAnswerId=40721030#a40721030
other experts suggested
if
switch
and I would prefer avoiding hard coded statements because I am learning object oriented programming
could you explain more about namespace example
I see that there is a return statement
other experts suggested
if
switch
and I would prefer avoiding hard coded statements because I am learning object oriented programming
could you explain more about namespace example
I see that there is a return statement
ASKER
should I clarify question?
That would be helpful.
ASKER
I think namespace example works but would have to study object oriented programming to know why it works