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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.