Solved

recreating an object model

Posted on 2004-09-01
5
245 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:DaFou
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:DaFou
ID: 11952683
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.
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11953467
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 )
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11953480
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 )
0
 
LVL 2

Author Comment

by:DaFou
ID: 11954520
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.
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 500 total points
ID: 11955123
This is directly from the overview:

   public Int16 this[ int index ]  {
      get  {
         return( (Int16) List[index] );
      }
      set  {
         List[index] = value;
      }
   }

However, what you want is to be able to access a member via a key instead of an index so your might write something like:

   public Client this[ Client c ] {
      get {
         return (Client) List[ IndexOf(c) ];
      }
   }

To then get the client by name you would add a field to your collection class:

   private Hashtable htKeys = new Hashtable();

Then when you added a client you would want to keep htKeys in sync so you would write:

            public int Add(Client c)
            {
                  int index = List.Add(c);
                        htKeys.Add( c.Name, index )
                  return index;
            }

Finally your indexer would look like:

   public Client this[ string key ] {
      get {
         return (Client) List[ ((int) htKeys[key] ) ];
      }
   }

this would ultimately allow you to do

Clients[ "ClientName" ]




You could also inherit from IDictionary instead of CollectionBase...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now