Jump to content
Xtreme .Net Talk

(C#) How do you create a class with grouped properties/methods?


Recommended Posts

Posted

I'm not sure how to explain it. I'm sure if I knew what it was called I could look it up :D

 

When I make a class, it generally looks like this:

 

public class Warehouse
	{
		// member variables
		private int numberOfItems = 0;
		private string manifestName = string.Empty;

		// Public Properties
		public int NumberOfItems
		{
			get
			{
				return numberOfItems;
			}
			set
			{
				numberOfItems = value;
			}
		}

		// Public Methods
		public void OpenManifest()
		{
		}

		public void CloseManifest()
		{
		}

		public void ClearManifest()
		{
		}
		
		public void NewManifest()
		{
		}
			
	}

 

so to access these methods, you have to type:

 

Warehouse wHouse = new Warehouse();
wHouse.ClearManifest;

 

you can see a few methods relating to a Manifests. There might be some related to Items, Deliveries, etc. Each would be their own class and the Warehouse class would be a superclass with code tying multiple other classes together.

 

I want to do this instead:

 

Warehouse wHouse = new Warehouse();
wHouse.Manifest.Clear;

 

I'd really like to learn how to do this, so if anyone can help, I'd appreciate it :)

Posted
public class Warehouse
{
	private  Manifest _manifest = new Manifest();
                         //-Expose private instance as read-only Manifest property.
	public  Manifest WarewouseManifest{
		get {
			return _manifest;
		}
	}

	public class Manifest{
	//-Manifest is public but non-creatable from other assemblies. If this is not requirement, then make internal public.
		internal Manifest(){}
		public void Clear(){}			
	}
}
}

Posted

and the name is nested class.

 

there is a good article in the help giving guidlines as to when to use and when not to use

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Like I thought, they're different classes. But you declare a 'childClass' and expose it.

 

the 'internal' prevents it from being instantiated itself. If I'd realized this for my last project... I would have had 1000% nicer code... and probobly shaved off a few days of programming. :D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...