6 Replies - 11605 Views - Last Post: 27 October 2012 - 08:48 AM

#1 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

is Operator?

Posted 15 November 2011 - 04:54 AM

Hey guys. Well I found out about the is operator a while back and I've just started to use it more and more. I really like it and it works well in some cases but I worry about one thing with it. If you use it in a base class to check if it's one of the classes that inherits it, it seems like really bad practice for OOP to me. I was just going to ask some people if it was. Thanks in advance and have a good day :).

Is This A Good Question/Topic? 0
  • +

Replies To: is Operator?

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,639
  • Joined: 24-September 10

Re: is Operator?

Posted 15 November 2011 - 07:52 AM

The idea smells a bit to me, just because a base class tends not to know, or shouldn't need to know any of its child classes. If some special scenario should occur in some special child class, the child class should do it itself.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: is Operator?

Posted 15 November 2011 - 07:59 AM

The base class should be concerned with itself. The child class concerned with itself. You should never have to check in a base class if it's an extended version, since the class should be able to operate without any knowledge of any classes extending it.
Was This Post Helpful? 0
  • +
  • -

#4 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: is Operator?

Posted 15 November 2011 - 09:18 AM

Thanks guys! That's why I thought but I was just checking with you guys.
Was This Post Helpful? 0
  • +
  • -

#5 barbary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 12

Re: is Operator?

Posted 13 August 2012 - 09:49 PM

View PostMATTtheSEAHAWK, on 15 November 2011 - 04:54 AM, said:

Hey guys. Well I found out about the is operator a while back and I've just started to use it more and more. I really like it and it works well in some cases but I worry about one thing with it. If you use it in a base class to check if it's one of the classes that inherits it, it seems like really bad practice for OOP to me. I was just going to ask some people if it was. Thanks in advance and have a good day :).


Post some code on how or why your using this because it isnt that it is wrong it just shouldn't be nessary.

class Program
	{
		static void Main(string[] args)
		{
			ExtendsTestBase extendsTestBase = new ExtendsTestBase();
			OwnClass ownClass = new OwnClass();

			TestBase.MethodThatTakesTestBase(extendsTestBase); //correct
			TestBase.MethodThatTakesTestBase(ownClass); //incorrect wont compile

		}

		class TestBase
		{
			public TestBase()
			{
			}

			public static void MethodThatTakesTestBase(TestBase testBase)
			{
			}
		}

		class ExtendsTestBase : TestBase
		{
			public ExtendsTestBase()
			{
			}
		}

		class OwnClass
		{
			public OwnClass()
			{
			}
		}
	}



As you can see from the code you just cant pass in anything that isnt derived from the Base Class
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: is Operator?

Posted 14 August 2012 - 06:52 AM

I maybe contributing to a necro post, but feel like I need to comment.

In general, a base class should never have to know about its derived classes. This gets to be hard to enforce though if you decide to implement the Factory Method pattern and put that factory method as a static method on the base class. For example, see the WebRequest.Create() method that will instantiate the appropriate kind of derived WebRequest class as needed.

The naive implementation (but also minimal coding required) would have the base class know all the derived class and essentially have giant switch statement that is hard coded.

A more sophisticated approach would follow a Chain of Responsibility pattern where the base class would have a list of that it iterates over until a class responds that it knows how to handle the input. Of course, now the derived classes have to register with the base class to get put on that list, or the base class will have to use reflection to find all the derived classes. If you think: "Reflection! Ick! I'll go with the registration model". I'd love to hear how you would initiate the registration process without resorting to a hardcoded list, or list of classes in a configuration file.
Was This Post Helpful? 1
  • +
  • -

#7 marty617  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 14-October 12

Re: is Operator?

Posted 27 October 2012 - 08:48 AM

I'll add 2 cents. The only time I can think that I ever use 'is' is to test types when given an object of type object and soley for the purpose of safely downcasting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1