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

    }

}
Cleaven
  • 974
  • 2
  • 9
  • 30
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is – ThePerplexedOne Mar 21 '18 at 16:03
  • 1
    Instead of `if (s == tt)` do `if (s is TechToday) { new ConsoleObserver (s as TechToday); }` – Rufus L Mar 21 '18 at 16:04
  • @RufusL the code is confusing, I think you are likely right. – maccettura Mar 21 '18 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 – Cleaven Mar 21 '18 at 16:07
  • This doesn't make much sense to me. The created objects (ConsoleObserver and ConsoleOutput) will go out of scope when `Subscribe` returns ... And what is it: Do you want to compare Types or instances ? – Fildor Mar 21 '18 at 16:14
  • @Cleaven I still think you are way better off using an overload – maccettura Mar 21 '18 at 16:33
  • @maccettura I'm following instructions of the assignment – Cleaven Mar 21 '18 at 16:34
  • What _are_ the "instructions of the assignment" ? – Fildor Mar 21 '18 at 16:34
  • 1
    @Cleaven I might suggest finding a new course then, this is not how you would normally handle this in the real world – maccettura Mar 21 '18 at 16:34
  • https://stackoverflow.com/questions/981330/instantiate-an-object-with-a-runtime-determined-type – Zaxxon Mar 21 '18 at 17:53

3 Answers3

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);
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
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.

Fildor
  • 14,510
  • 4
  • 35
  • 67
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);
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39