Solved

recreating an object model

Posted on 2004-09-01
5
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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