0

I'm trying to compare a class type passed through as an object to the class type on the other side where the method is.

How can I do this?

I have this so far.

The one side:

TechToday techToday = new TechToday(); SoftwareRevolution softwareRevolution = new SoftwareRevolution(); Subcriber s1 = new Subcriber(); s1.Subcribe(techToday); s1.Subcribe(softwareRevolution); 

The other side:

class Subcriber { TechToday tt = new TechToday(); SoftwareRevolution sr = new SoftwareRevolution(); public void Subcribe(Object s) { if(s==tt) new ConsoleObserver(s); else new ConsoleOutput(s); } } 
10
  • learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented Mar 21, 2018 at 16:03
  • 1
    Instead of if (s == tt) do if (s is TechToday) { new ConsoleObserver (s as TechToday); } Commented Mar 21, 2018 at 16:04
  • @RufusL the code is confusing, I think you are likely right. Commented Mar 21, 2018 at 16:05
  • @maccettura The method only handles a single subscription at a time. It must do this by receiving a single “object” as its parameter and then determining whether this object is a classic or events subject. I'm still fairly new to this pattern of code so its trial and error for me Commented Mar 21, 2018 at 16:07
  • 1
    @Cleaven I might suggest finding a new course then, this is not how you would normally handle this in the real world Commented Mar 21, 2018 at 16:34

3 Answers 3

4

You can use is operator to check if object is of that particular type like:

if(s is TechToday) new ConsoleObserver(s); 

or you can do something like:

if(s.GetType() == typeof(TechToday)) 

if you are looking to do equality of objects then you will need to check first the type of it and then cast the reference to that specific type and then check for equality something like:

if(s is TechToday) { TechToday tt2 = s as TechToday; if(tt2 == tt) new ConsoleObserver(tt2); } 

or you could also do it like:

TechToday tt2 = s as TechToday; if(tt2 == tt) new ConsoleObserver(tt2); 

Another option is using the new feature of C# 7 pattern matching :

if (s is TechToday tt2) { if(tt2 == tt) new ConsoleObserver(tt2); } 
Sign up to request clarification or add additional context in comments.

4 Comments

By the way if(s is TechToday) { TechToday tt2 = s as TechToday is overkill, you could use TechToday tt2 = s as TechToday; if (tt2 != null)... or pattern matching of course
yes @vc74 and we don't event need null check if comparison is intended, it would return false if either is null
Thank you very much for your help :)
glad it helped! :)
2

I'd suggest to use overloads if possible:

class Subcriber { public void Subcribe(TechToday s) { new ConsoleObserver(s); } public void Subcribe(SoftwareRevolution s) { new ConsoleOutput(s); } } 

If you have to stay with object in the signature of Subscribe, you probably want to use something along

if( s is TechToday ) { new ConsoleObserver(s); } 

But after all, this does not make much sense, because as is, the created objects will go out of scope immediately after leaving Subscribe.

Comments

1

You can use the is operator:

class Subcriber { public void Subcribe(Object s) { if(s is TechToday) new ConsoleObserver(s); else new ConsoleOutput(s); } } 

However using "as" might be better:

class Subcriber { public void Subcribe(Object s) { var tt = s as TechToday; if(tt!=null) // unlike above // value passed to ConsoleObserver is TechToday // casted already new ConsoleObserver(tt); else new ConsoleOutput(s); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.