Solved

recreating an object model

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

21 Experts available now in Live!

Get 1:1 Help Now