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. :)

Recommended Answers

All 2 Replies

Can you post your code..

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. :*)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.