newbie: do you ever define a class within a class ( Class A { Class B {} })?
-
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ? I have never seen any of the book examples show that you ever would do this. Here is the example I was working on.... the first class Tester code (not commented out) does not compile (error: The name Temperature does not exist in the current context). But below the commented out class Tester code will work because I removed the Values class. Is there a way to get it to work without removing the Values class. ( Please keep in mind, some of this might not be a very practicle example.... i.e. it really does not make sense to have the Values class in the first place!) */ using System; using System.Collections.Generic; using System.Text; namespace EnumFreezingInRun { // this gives error "The name Temperature does not exist in the current context" class Tester { class Values { enum Temperatures { WickedCold = 0, FreezingPoint = 32, } } public void Run() { Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); } static void Main() { Tester t = new Tester(); t.Run(); } } // This does work! //class Tester //{ // enum Temperatures // { // WickedCold = 0, // FreezingPoint = 32, // } // public void Run() // { // Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); // Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); // } // static void Main() // { // Tester t = new Tester(); // t.Run(); // } //} }
-
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ? I have never seen any of the book examples show that you ever would do this. Here is the example I was working on.... the first class Tester code (not commented out) does not compile (error: The name Temperature does not exist in the current context). But below the commented out class Tester code will work because I removed the Values class. Is there a way to get it to work without removing the Values class. ( Please keep in mind, some of this might not be a very practicle example.... i.e. it really does not make sense to have the Values class in the first place!) */ using System; using System.Collections.Generic; using System.Text; namespace EnumFreezingInRun { // this gives error "The name Temperature does not exist in the current context" class Tester { class Values { enum Temperatures { WickedCold = 0, FreezingPoint = 32, } } public void Run() { Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); } static void Main() { Tester t = new Tester(); t.Run(); } } // This does work! //class Tester //{ // enum Temperatures // { // WickedCold = 0, // FreezingPoint = 32, // } // public void Run() // { // Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); // Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); // } // static void Main() // { // Tester t = new Tester(); // t.Run(); // } //} }
LuluSailor wrote:
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ?
I wouldn't say it was bad practice. Only that there are a limit number of situations that would warrant this. The only one I can think of is to have the enumerator (a class deriving from IEnumerator) as an inner class of the one that it enumerates (which will normally implement IEnumerable). ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
-
LuluSailor wrote:
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ?
I wouldn't say it was bad practice. Only that there are a limit number of situations that would warrant this. The only one I can think of is to have the enumerator (a class deriving from IEnumerator) as an inner class of the one that it enumerates (which will normally implement IEnumerable). ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Thank-you for taking the time to answer my question Colin. It did seem that this is not too common, since I did not see it done in examples (class A {class B{}} ) So is there another way to get the code to work, other than remove the pluck out (delete) the Value class from the code? I am just curious how I would get the Temperatures to become available/ or in context without removing the class. Although - my main concern you have answered, which is how common it would be to define a class within a class. I am still not sure what I am really doing when I define a class within a class. But this might be a bit too advanced for me right now. For now - I will not do it (accidentally or otherwise)!
-
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ? I have never seen any of the book examples show that you ever would do this. Here is the example I was working on.... the first class Tester code (not commented out) does not compile (error: The name Temperature does not exist in the current context). But below the commented out class Tester code will work because I removed the Values class. Is there a way to get it to work without removing the Values class. ( Please keep in mind, some of this might not be a very practicle example.... i.e. it really does not make sense to have the Values class in the first place!) */ using System; using System.Collections.Generic; using System.Text; namespace EnumFreezingInRun { // this gives error "The name Temperature does not exist in the current context" class Tester { class Values { enum Temperatures { WickedCold = 0, FreezingPoint = 32, } } public void Run() { Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); } static void Main() { Tester t = new Tester(); t.Run(); } } // This does work! //class Tester //{ // enum Temperatures // { // WickedCold = 0, // FreezingPoint = 32, // } // public void Run() // { // Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); // Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); // } // static void Main() // { // Tester t = new Tester(); // t.Run(); // } //} }
Another reason that you may want to define an inner class relies on how you will use
B
inclass A{ class B{} }
. If onlyA
usesB
and no other class should useB
, then aprivate
inner class would work well in that situation. For example, if you write a class that acts as a binary tree that supports only adding, searching, and removing, then you may never need to expose the class that implements the node in that tree structure. Thus, you could have something like the following incomplete code snippet.public class Tree
{
public Tree() { // stuff }public object Search( object obj ) { // Searches the Tree.Nodes in the Tree }
public void Add( object obj ) { // Adds a Tree.Node to the Tree }
public void Remove( object obj ) { // Removes a Tree.Node from the Tree }
private class Node
{
public Node( object o ) { // stuff }
}
}"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
Do you, or is it bad practice to do this sort of thing: Class A { Class B {} } ? I have never seen any of the book examples show that you ever would do this. Here is the example I was working on.... the first class Tester code (not commented out) does not compile (error: The name Temperature does not exist in the current context). But below the commented out class Tester code will work because I removed the Values class. Is there a way to get it to work without removing the Values class. ( Please keep in mind, some of this might not be a very practicle example.... i.e. it really does not make sense to have the Values class in the first place!) */ using System; using System.Collections.Generic; using System.Text; namespace EnumFreezingInRun { // this gives error "The name Temperature does not exist in the current context" class Tester { class Values { enum Temperatures { WickedCold = 0, FreezingPoint = 32, } } public void Run() { Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); } static void Main() { Tester t = new Tester(); t.Run(); } } // This does work! //class Tester //{ // enum Temperatures // { // WickedCold = 0, // FreezingPoint = 32, // } // public void Run() // { // Console.WriteLine("Freezing is: {0}", (int)Temperatures.FreezingPoint); // Console.WriteLine("Wicked cold is: {0}", Temperatures.WickedCold); // } // static void Main() // { // Tester t = new Tester(); // t.Run(); // } //} }
A simple resolution:
using System;
using System.Collections;
using System.Text;namespace EnumFreezingInRun
{
// this gives error "The name Temperature does not exist in the current context"
// - now does not
class Tester
{
class Values
{
public enum Temperatures // note changes here
{
WickedCold = 0,
FreezingPoint = 32,
}
}public void Run()
{
Console.WriteLine("Freezing is: {0}", (int)Values.Temperatures.FreezingPoint); // here
Console.WriteLine("Wicked cold is: {0}", Values.Temperatures.WickedCold); // and here
}static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}