Not Szwagi
Not Szwagi

Reputation: 81

C# Namespaces inside classes?

Not sure how to explain this, so I'll try to give as much detail as possible. I'm making a Net Library and I need to give a section to my NetClient class, such as Headers in this example:

NetClient netClient = new NetClient("host", port);
netClient.Headers.Add("Name", "Value");

I would think this would work, but it doesn't (can't see the Headers class at all in an instance of NetClient class):

namespace NetLib
{
    class NetClient
    {
        public string Host { get; }
        public int Port { get; }

        public NetClient(string host, int port)
        {
            this.Host = host;
            this.Port = port;
        }

        class Headers
        {
            class Header
            {
                public string Name { get; }
                public string Value { get; }

                internal Header(string name, string value)
                {
                    this.Name = name;
                    this.Value = value;
                }
            }



I solved my problem with the help of submitted answers, this is what my code looks like now:

   public sealed class NetClient
   {
        public string Host { get; set; }
        public int Port { get; set; }
        public Headers Headers { get; private set; }


        public NetClient(string host, int port)
        {
            this.Headers = new Headers();
            this.Host = host;
            this.Port = port;
        }
    }

    public sealed class Headers
    {
        public class Header
        {
            public string Name { get; }
            public string Value { get; }

            internal Header(string name, string value)
            {
                this.Name = name;
                this.Value = value;
            }
        }

Upvotes: 1

Views: 4380

Answers (3)

Kaushal B
Kaushal B

Reputation: 46

Try making it public, as by default classes are "internal" and members/variables inside are private.

So the basic idea is to have internal instance creation so that the class will be initialized and the object will be created at run-time. Please do not copy paste as is as I have not used VS to rectify.

I think it should be more like :

NetClient netClient = new NetClient("host", port); netClient.Headers=netclient.CreateObjHeaders("Name", "Value");

namespace NetLib { class NetClient { public string Host { get; } public int Port { get; }

    public NetClient(string host, int port)
    {
        this.Host = host;
        this.Port = port;
    } 

    public Headers CreateObjHeaders(string Name, string Value)
    { 
        Headers objHeaders=new Headers("Name", "Value");
        return objHeaders;
    }
    public class Headers
    {
       public Headers(string Name, string Value)
       { 
           Header objHeader=new Header("Name", "Value"); 
       }

       public class Header
       {
            public string Name { get; }
            public string Value { get; }

            internal Header(string name, string value)
            {
                 this.Name = name;
                 this.Value = value;
            }
       }

Upvotes: 0

Guffa
Guffa

Reputation: 700152

Putting a class inside another class doesn't make it an instance member of the class (or even a static member), it only affects the naming and scope of the class.

To get an instance member you need an actual member of the class, for example a property that is a list of header items:

namespace NetLib {

  class Header {

    public string Name { get; }
    public string Value { get; }

    public Header(string name, string value) {
      this.Name = name;
      this.Value = value;
    }

  }

  class NetClient {

    public string Host { get; private set; }
    public int Port { get; private set; }
    public List<Header> Headers { get; private set; }

    public NetClient(string host, int port) {
      this.Host = host;
      this.Port = port;
      this.Headers = new List<Header>();
    }

  }

}

Usage:

NetClient netClient = new NetClient("host", port);
netClient.Headers.Add(new Header("Name", "Value"));

You could put the Header class inside the NetClient class, but then you need to use new NetClient.Header(...) instead of new Header(...).

Upvotes: 2

Kzryzstof
Kzryzstof

Reputation: 8382

Would the following code provide you with what you need?

public sealed class NetClient
{
    public string Host { get; set; }
    public int Port { get; set; }
    public Headers Headers { get; private set; }

    public NetClient(string host, int port)
    {
        Host = host;
        Port = port;

        Headers = new Headers();
    }
}

public sealed class Headers : Dictionary<String, String>
{
}

Upvotes: 1

Related Questions