Link to home
Start Free TrialLog in
Avatar of DaFou
DaFou

asked on

recreating an object model

Ola,

I have this class called Channel. Now I want this class to have a public member Clients ( which is an object itself that acts like a collection but with additional methods )

I want to be able to build my class in such a way that the following would be possible.

Channel.Clients( "ClientName" ).RespondTo( "Some Message" ); ( responds to only 1 client in the channel )
Channel.Clients.RespondTo( "Some Message" ); ( responds to all clients in the channel )

public class Channel
{
    // please add the nececery defenitions here to enable that I want
}

Ofcourse the actual respond to code I'll do myself.
This type of stuff is possible in the ASP Object model so I am betting it is possible to make somethign like this using C#

Regards
Avatar of DaFou
DaFou

ASKER

public class Channel
{
      public class Clients
      {
            public void RespondTo( string iStrMessage )
            {
            }
            public Client Clients( string iStrClientName )
            {
                  
            }
      }
      public Clients Clients = new Clients();
}

this works for:
Channel.Clients.RespondTo( "Some Message" );
But NOT for:
Channel.Clients( "ClientName" )

So I figured the following code:
public class Channel
{
      public class Clients
      {
            public bool Add( string iStrClientName )
            {
            }
      }
      public Client Clients( int iIntOrdinal )
      {
      }
      public Clients Clients = new Clients();
}
But now I get an error message:
This member is defined more then once.
How about this...

public class Client {
    // add fields

    // add properties

    // add methods

    public void RespondTo( string msg ) {
        // add code
    }
}

public class Clients : CollectionBase {
    // look up CollectionBase to see what to add...
    // basically this will be a strongly types collection of Client
    // so you only want to allow a Client Object to be added.

    public void RespondTo( string msg ) {
        foreach ( Client c in list ) {
            c.RespondTo( msg );
        }
    }
}

This will allow:

Channel.Clients[ "ClientName" ].RespondTo( "Some Message" ); ( responds to only 1 client in the channel )
Channel.Clients.RespondTo( "Some Message" ); ( responds to all clients in the channel )
OOPs, you will need to make Clients a field on channel and add a public property to do:

Channel.Clients( "ClientName" ).RespondTo( "Some Message" ); ( responds to only 1 client in the channel )
Channel.Clients.RespondTo( "Some Message" ); ( responds to all clients in the channel )
Avatar of DaFou

ASKER

I get an error message on compile.

Cannot apply indexing with [] to an expression of type 'CSdhtmlChatServer.Clients'
In the MSDN walkthrough on the CollectionBase I dont see anything that helps me.
Only how to add an Add, Remove method and an Item property.

Also using this information I do not see how to implement the
Clients[ "ClientName" ] to retrieve a specific Client on a reference basis.
ASKER CERTIFIED SOLUTION
Avatar of NipNFriar_Tuck
NipNFriar_Tuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial