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
LVL 1
rgb192Asked:
Who is Participating?
 
it_saigeDeveloperCommented:
So continuing, if you only wanted certain types of business you would change the logic to dictate the type or specify properties that differentiate the businesses:
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) })));

			Console.WriteLine("Let's write reviews for only small businesses");
			foreach (var item in businesses.Where(b => b is SmallBusiness))
				Console.WriteLine(item.WriteGoodReview());

			Console.WriteLine();
			Console.WriteLine("Now the medium businesses");
			foreach (var item in businesses.Where(b => b is MediumBusiness))
				Console.WriteLine(item.WriteGoodReview());

			Console.WriteLine();
			Console.WriteLine("Finally the large businesses");
			foreach (var item in businesses.Where(b => b is LargeBusiness))
				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);
		}
	}
}

Open in new window

Produces the following results -Capture.JPG
-saige-
0
 
Fernando SotoRetiredCommented:
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.
0
 
Jeffrey Dake Senior Director of TechnologyCommented:
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
sarabandeCommented:
assuming that 'thankful' is a boolean property of class Business you may assign

((business == thisBusiness) || thisBusiness.thankful)

Open in new window


to the property.

Sara
0
 
it_saigeDeveloperCommented:
To put a face to what Jeffery is saying; consider the following -
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);
		}
	}
}

Open in new window

Produces the following output -Capture.JPGNow 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);
		}
	}
}

Open in new window

Now produces the following output -Capture.JPG
-saige-
0
 
sarabandeCommented:
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);

Open in new window


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

Open in new window


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

Open in new window


ThankLessMessage::WriteGoodReview() would make no output, while ThankFulMessage::WriteGoodReview() does the required output.

Sara
0
 
rgb192Author Commented:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28654594.html#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
0
 
rgb192Author Commented:
should I clarify question?
0
 
Fernando SotoRetiredCommented:
That would be helpful.
0
 
rgb192Author Commented:
I think namespace example works but would have to study object oriented programming to know why it works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.