0

Hiya,

I've got a small problem. I have an interface called "event", which represents a networking event. I have classes implementing this interface. Now, when my server app receives a new event, it checks the class of the event and acts accordingly. This works nicely when the implementing classes are different, but the program can't distinguish two similar classes.

Let's say we have class A and class B which both implement Event interface. I want to check the classes like this:

Event event;
// Get the event from network

A a = event as A;
if(a != null) {
    // Message is of type 'A'
}

B b = event as B;
if(b != null) {
    // Message is of type 'B'
}

This works nicely, but it can't distinguish the classes (if the classes have similar properties and methods). The code snippet thinks the event is always of type 'A'. How can I make the classes distinct, thus making the server act correctly? I'd like to identify the events bases on the class. I know I could bypass this by adding more information inside the events and check that info, but identifying by class would be a lot neater option. :)

Edited by bleedi: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by bleedi
0

Can you post your code..

Not really, since it's under NDA... The abstraction is something like this:

Event e = ...; // e implements IEvent interface
AEvent a = e as AEvent;
if(a != null) {
    // Handle type 'A' message
}

BEvent b = e as BEvent;
if(b != null) {
    // Handle type 'B' message
}

The problem here is that even if the event is of type 'B', it will be handled as 'A', which isn't what I want.


EDIT:
I solved the problem. It wasn't really even connected to this. :*)

Edited by bleedi: Problem solved (wasn't caused by this)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.