1

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; } } 
4
  • Is this the full source of NetClient? You seem to be trying to use a nested class (Headers) as a property. Commented Nov 8, 2015 at 19:22
  • 1
    You just have defined class. You can only access static members. To access instance members you must create property of that class. So you must have instance of Headers in NetClient too Commented Nov 8, 2015 at 19:24
  • This is not the full code, but I don't think the rest is needed. I know that I could create a member of Headers inside NetClient, but is there any way of going around it? Commented Nov 8, 2015 at 19:25
  • Only if you make Headers static as pointed out by @m-kazem-akhgary. But no way of telling if that will help you without seeing the rest of the code. And most of the answers provided so far seem to be better solutions. Commented Nov 8, 2015 at 19:29

3 Answers 3

2

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(...).

Sign up to request clarification or add additional context in comments.

1 Comment

This helped to solve my problem in the way I wanted to solve it, I just changed a few things in it. Thanks. Now I can do it like this: netClient.Headers.Add("Name", "Value");
1

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> { } 

Comments

0

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; } } 

3 Comments

Sadly didn't work for me, still can't see the class inside.
if you do not define an access modifier the default is private
for a variable its private but for a class default is internal/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.