Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

How to code an ObjectModel

Ola,

I am trying to create an object model for a chatserver. The chatserver is the root class. any class inside the root class should not be instantiable or accessible outside of a chatserver object ( an instance of the chatserver class ).

There are 2 collections ( objects that act like a collection ) inside the ChatServer root object, Channels ( ChannelsCollection class ) and Clients ( ClientsCollection class ).
The Channels collection contains Channel objects ( instances of the Channel class ).
The Channel class should only be accessible from within the Channels colletion.

The Clients collection contains Client objects ( instances of the Client class ).
The Client class should only be accessible from within the Clients colletion.

A Channel object has a collection Clients ( same ClientsCollection class as used in the ChatServer object )

Here is a diagram:

ChatServer
      |     |
      | Channels
      |     |
      | Channel
      |  |
    Clients
      |
    Client

I have made the object model code in c# and am trying to compile it but get all kinds of errors like:
inconsistent accessibility: field type 'ObjectModel.ChatServer.ChannelsCollection' is less accessible than field 'ObjectModel.ChatServer.Channels'

and

'ObjectModel.ChatServer.ChannelsCollection.this[string]' : property or indexer must have at least one accessor


Here is the code:

using System;
using System.Collections;
using System.Net.Sockets;

namespace ObjectModel
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class MainPoint
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  ChatServer lChatServer = new ChatServer();
            }
      }
      public class ChatServer
      {
            public ChatServer.ChannelsCollection Channels = new ChatServer.ChannelsCollection();
            public ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection();

            private Collection prInsCollectionClients = new Collection();

            protected sealed class ChannelsCollection
            {
                  private static Collection prStaCollectionChannels = new Collection();
                  public Channel this[ string iStrChannelName ]
                  {
                  }
                  public Channel this[ int iIntChannelOrdinal ]
                  {
                  }
                  public static void Add( string iStrChannelName )
                  {
                  }
                  protected sealed class Channel
                  {
                        private string prStrName;
                        public ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection();
                        public Channel ( string iStrChannelName )
                        {
                        }
                        public string Name
                        {
                        }
                  }
            }
            protected sealed class ClientsCollection
            {
                  private Collection prInsCollectionClients = new Collection();
                  private bool prInsBoolTopCollection;

                  public ClientsCollection( bool iBoolTopCollection )
                  {
                        prInsBoolTopCollection = iBoolTopCollection;
                  }
                  public Client this[ string iStrClientName ]
                  {
                  }
                  public Client this[ int iIntClientOrdinal ]
                  {
                  }
                  public string Respond( string iStrMessage )
                  {
                  }
                  public void Add( TcpClient iTcpClient )
                  {
                  }
                  public bool ClientExisits( string iStrClientName )
                  {
                  }
                  public void Remove( string iStrClientName )
                  {
                  }
                  public void Remove( int iIntClientOrdinal )
                  {
                  }
                  protected sealed class Client
                  {
                        private string prStrName;
                        private string prStrRequest;
                        private NetworkStream prNetworkStream;
                        private StreamWriter prStreamWriter;
                        private char[] prArrCharRequest;

                        public Client( TcpClient iTcpClient )
                        {
                        }
                        public string Name
                        {
                        }
                        public void Tell( string iStrMessage )
                        {
                        }
                        public void Remove()
                        {
                        }
                  }
            }
      }
      public class Collection : DictionaryBase
      {
            private ArrayList prArrayList = new ArrayList();
            public object this[ string iStrKey ]
            {
            }
            public object this[ int iIntOrdinal ]
            {
            }
            public void Add( string iStrKey, object iObjValue )  
            {
            }
            public bool Contains( string iStrKey )
            {
            }
            public void Remove( string iStrKey )  
            {
            }
      }
}

Please help me out how to fix this.
0
DaFou
Asked:
DaFou
  • 5
  • 5
  • 2
2 Solutions
 
Chester_M_RagelCommented:
I have done some changes to make this code buildable. Please check the comments with the code.

using System;
using System.Collections;
using System.Net.Sockets;
using System.IO;

namespace ObjectModel
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      ///
      class MainPoint
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  ChatServer lChatServer = new ChatServer();
            }
      }
      
      public class ChatServer
      {
            ChatServer.ChannelsCollection Channels = new ChatServer.ChannelsCollection();
            //Check the parameter
            ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection(true);

            private Collection prInsCollectionClients = new Collection();
      

            protected sealed class ChannelsCollection
            {
                  private static Collection prStaCollectionChannels = new Collection();
                  public Channel this[ string iStrChannelName ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public Channel this[ int iIntChannelOrdinal ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public static void Add( string iStrChannelName )
                  {
                  }
                  
            }
            
            protected sealed class Channel
            {
                  private string prStrName;
                  //Check the parameter
                  ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection(true);
                  public Channel ( string iStrChannelName )
                  {
                  }
                  public string Name
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
            }

            protected sealed class ClientsCollection
            {
                  private Collection prInsCollectionClients = new Collection();
                  private bool prInsBoolTopCollection;

                  public ClientsCollection( bool iBoolTopCollection )
                  {
                        prInsBoolTopCollection = iBoolTopCollection;
                  }
                  public Client this[ string iStrClientName ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public Client this[ int iIntClientOrdinal ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  //Check the return type
                  public string Respond( string iStrMessage )
                  {
                        return "";
                  }
                  public void Add( TcpClient iTcpClient )
                  {
                  }
                  //check the return type
                  public bool ClientExisits( string iStrClientName )
                  {
                        return true;
                  }
                  public void Remove( string iStrClientName )
                  {
                  }
                  public void Remove( int iIntClientOrdinal )
                  {
                  }
                  
            }
            protected sealed class Client
            {
                  private string prStrName;
                  private string prStrRequest;
                  private NetworkStream prNetworkStream;
                  private StreamWriter prStreamWriter;
                  private char[] prArrCharRequest;

                  public Client( TcpClient iTcpClient )
                  {
                  }
                  public string Name
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public void Tell( string iStrMessage )
                  {
                  }
                  public void Remove()
                  {
                  }
            }

            public class Collection : DictionaryBase
            {
                  private ArrayList prArrayList = new ArrayList();
                  public object this[ string iStrKey ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public object this[ int iIntOrdinal ]
                  {

                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }
                  }
                  public void Add( string iStrKey, object iObjValue )  
                  {
                  }
                  //Check the return type
                  public bool Contains( string iStrKey )
                  {
                        return true;
                  }
                  public void Remove( string iStrKey )  
                  {
                  }

            }
      
      }      
}
0
 
ThaloxCommented:

I'm not sure, if I understand your structure right, but in general you can't define properties that are more accessible than the class of the property.

if your class is protected and you define a public property of this class, it wouldn't be protected anymore, so it is denied.
It should work, if you change your public properties to protected.


hope that helps,

Thalox
0
 
DaFouAuthor Commented:
Chester_M_Ragel: Thx for the code it does compile now. But when i try to access the Clients or Channels collection it is not found
ChatServer lChatServer = new ChatServer();
lChatServer.Clients ( does not show up in intelisense ) and when i compile i get this error:
'ObjectModel.ChatServer.Clients' is inaccessible due to its protection level.

Thalox: How do i enable the ChatServer object to contain objects that are not accessible from the outside
So this should be IMpossible
Clients lClients = new ChatServer.Clients();

but this should be possible
ChatServer lChatServer = new ChatServer();
lChatServer.Clients

0
Independent Software Vendors: 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!

 
ThaloxCommented:

I'm pretty sure, that this isn't possible.
IChatServer.Clients will give you a Collection of Client and that is the same as being able to create a new object.

But why do you want to it this way?

If this is necessary, then you should define functions in your chatserver class to interact with the Client-objects and only access them from inside the Chatserver-class
0
 
ThaloxCommented:

... something like this


public class ChatServer
{
      private Client clt;

      public ChatServer() {
      
            clt = new Client();
      }

      public string sayClientHello()
      {
            return clt.sayHello();
      }

      private class Client
      {
            public Client() {}

            public string sayHello()
            {
                  return "Hello World";
            }
      }

}

and access it

ChatServer chat = new ChatServer();
string x = chat.sayClientHello();

0
 
Chester_M_RagelCommented:
using System;
using System.Collections;
using System.Net.Sockets;
using System.IO;

namespace ObjectModel
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      ///
      class MainPoint
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  ChatServer lChatServer = new ChatServer();
                        
            }
      }
      
      public class ChatServer
      {
            public ChatServer.ChannelsCollection Channels = new ChatServer.ChannelsCollection();
            //Check the parameter
            public ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection(true);

            private Collection prInsCollectionClients = new Collection();
      

            public sealed class ChannelsCollection
            {
                  private static Collection prStaCollectionChannels = new Collection();
                  public Channel this[ string iStrChannelName ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public Channel this[ int iIntChannelOrdinal ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public static void Add( string iStrChannelName )
                  {
                  }
                  
            }
            
            public class Channel
            {
                  private string prStrName;
                  //Check the parameter
                  ChatServer.ClientsCollection Clients = new ChatServer.ClientsCollection(true);
                  public Channel ( string iStrChannelName )
                  {
                  }
                  public string Name
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
            }

            public sealed class ClientsCollection
            {
                  private Collection prInsCollectionClients = new Collection();
                  private bool prInsBoolTopCollection;

                  public ClientsCollection( bool iBoolTopCollection )
                  {
                        prInsBoolTopCollection = iBoolTopCollection;
                  }
                  public Client this[ string iStrClientName ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public Client this[ int iIntClientOrdinal ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  //Check the return type
                  public string Respond( string iStrMessage )
                  {
                        return "";
                  }
                  public void Add( TcpClient iTcpClient )
                  {
                  }
                  //check the return type
                  public bool ClientExisits( string iStrClientName )
                  {
                        return true;
                  }
                  public void Remove( string iStrClientName )
                  {
                  }
                  public void Remove( int iIntClientOrdinal )
                  {
                  }
                  
            }
            public class Client
            {
                  private string prStrName;
                  private string prStrRequest;
                  private NetworkStream prNetworkStream;
                  private StreamWriter prStreamWriter;
                  private char[] prArrCharRequest;

                  public Client( TcpClient iTcpClient )
                  {
                  }
                  public string Name
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public void Tell( string iStrMessage )
                  {
                  }
                  public void Remove()
                  {
                  }
            }

            public class Collection : DictionaryBase
            {
                  private ArrayList prArrayList = new ArrayList();
                  public object this[ string iStrKey ]
                  {
                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }

                  }
                  public object this[ int iIntOrdinal ]
                  {

                        //Must fill this with the correct code
                        get
                        {
                              return null;
                        }
      //                  set
      //                  {
      //                  }
                  }
                  public void Add( string iStrKey, object iObjValue )  
                  {
                  }
                  //Check the return type
                  public bool Contains( string iStrKey )
                  {
                        return true;
                  }
                  public void Remove( string iStrKey )  
                  {}

            }
      
      }      
}
0
 
DaFouAuthor Commented:
wont this messup my object model?
And if I reverse engineer the code to UML do I still get the same diagram?
ChatServer
      |     |
      | Channels
      |     |
      | Channel
      |  |
    Clients
      |
    Client

Please show me how to do what you suggest
0
 
DaFouAuthor Commented:
ignore last post. it crossed your comments
0
 
DaFouAuthor Commented:
Chester_M_Ragel: your last suggestion makes the ClientsCollection class publicly available.
that means that this would work while it shoud not be possible.
ChatServer.ClientsCollection lClientsCollection = new ChatServer.ClientsCollection();

Thalox:
Ok so we create a shell around it. How do i maintain the indexing features I want, using your method, like.
ChatServer.Clients[0].SomeMethod();
Or
ChatServer.Clients.SomeMethod();
0
 
ThaloxCommented:

to use a method with an index, you must retrun an object, that implements this method.
so if you don't want this object to be public, the only way (I know) is to use Interfaces.

so create an interface-definition, that defines all the needed methods and
let the Client-objects implement these methods.
then your Collection (the public one) should not be a collection of Client-objects then of your Interface-objects.

this will prevent the creation of ClientsCollection outside Chatserver (with new), but because the interface must be public, there is still the possiblity to create another object that implements this interface and so makes it possible to create a kind of ClientCollection ouside Chatserver.

I think it is much easier to pass the index to the methods itself

ChatServer.SomeClientMethod(index, ...)

and then use the index inside the chatserver on the private client-collection
0
 
DaFouAuthor Commented:
ok thx for the info. I am going to think really hard on this.
I am going to try your ChatServer.method shell suggestion.

would you say that this limitation is ineffectife or a well thoughtof design?
0
 
ThaloxCommented:

Thanks for the points,

And if this limitation is inefective depends on what you intend to do with your object modell.
If you just use it for your own, then its a little "heavy" and more costly to implement. But its a good training for object-oriented programming ;)

on the other hand, if you want others to use your object model then its a good way to prevent them from doing wrong things with your objects.

But this all concerns only programming. The performance shouldn't be influenced by the method
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now