Solved

recreating an object model

Posted on 2004-09-01
5
246 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

11 Experts available now in Live!

Get 1:1 Help Now