Like Tree2Likes
  • 1 Post By Mario F.
  • 1 Post By rags_to_riches

plz help~♫~♫~ 9 class questions ~♫~♫~God Bless!

This is a discussion on plz help~♫~♫~ 9 class questions ~♫~♫~God Bless! within the C# Programming forums, part of the General Programming Boards category; I apologize in advance for dumping (again) so many wordy, incoherent questions; thus, of course, thank you for any help, ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    plz help~♫~♫~ 9 class questions ~♫~♫~God Bless!

    I apologize in advance for dumping (again) so many wordy, incoherent questions; thus, of course, thank you for any help, no matter how little! Google/wikipedia has helped me alot, but for the weirder ones, I need your expertise!º¤ø,¸¸,ø¤º°`°º¤ø,¸



    1) Why do private classes require a container or outer class? My guess is that's how they can share its members with other classes.



    2) Is it fair to think of an object as an instance of a class, much like a variable instance of a data type? (except that objects can be methods or variables)



    3) To paraphrase my textbook:
    suppose you had two separate namespaces each with a class of the same name. If the Main() method was in the first namespace and you wanted to use a method or variable in the second namespace, then the fully qualified name is required.
    But whether the class name is same or not, wouldn't you have to do so anyway, in general?



    4) Is the protected access level the same as private, except the former can include derived/sub/nested classes? I believe the usual definition is that the private class gives access to itself and other classes in the shared container class... But I'm not too sure about this, since I think you can run at least the Main() in a class nested in the private class (with private variables).



    5) Is it common that private classes contain public variables (modifiers) in practice? I wasn't sure since I have read that for properties, it is required that the restrictiveness is the other way around. I also thought it was possible to eliminate the private class itself, leaving the variables and the Main() method in a container if the variables were going to be public.



    6) To paraphrase my textbook: "public access level is used with types and members.
    Access is not restricted". By type, do they mean data type? (just making sure)



    7) I'm still not sure regarding the difference between a break and return, as they both terminate loops? Or in other words, I know break means go to the next statement, but for return, what does "return statement transfers control to the calling method" mean exactly?



    8) Not exactly a question, but I just discovered you can use different counter labels (e.g. different letters) for the same array elements (within the same method) when you want to print them. I just thought that was strange.



    9) Not exactly a question, but wanted verification of my intepretation of the following program (its actually quite quite straightforward; the program either accepts your new number input, or it keeps its old one and reject yours). Intepretation below.
    Code:
    class MyClass
    {
        private int item = 144;  //it seems that if you leave out 144, it is 0 by default 
        public int Number        //usually related to item, so pick a name like Item 
        {
            get 
            { 
                return item; 
            }
            set
            {
                if ((value >= 100) && (value <= 1000))
                item = value;                                             
                else
                Console.WriteLine("The value is not within the permitted range!");
            }
        }
        static void Main()
        {
            // Create an object:
            MyClass mc = new MyClass();
            // Read a value from the keyboard:
            Console.Write("Please enter the new value: ");
            int myInt = Int32.Parse(Console.ReadLine());        //you can parse user input
            // Assign the value to the property:
            mc.Number = myInt;
            // Display the value:
            //mc.Number declared previously as a property  
            Console.WriteLine("The current value is: {0}", mc.Number);    
            Console.ReadLine();
        }
    }
    I belive the compilation is like this, in general: After the object is created, in Main(), mc.Number = myInt sends the user input, myInt, to the property (method) area (i.e. public int Number). Get and return item means we consider or read the initialized item 144, before we let myINt equal keyword value (field?) for the if-else statement. In either case, we exit the property with the retrieved item.


    Thank you once again! Cheers!!!!

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    It's sort of cute when you do it once or twice (asking several questions in a batch). Starts to become boring after that. Especially when a good deal of answers can be reached by actually trying out the code or searching for answer. Don't be lazy.

    >> 1) Why do private classes require a container or outer class? My guess is that's how they can share its members with other classes.

    Private classes don't require a container our outer class. Don't know where you got this idea.

    >> 3) To paraphrase my textbook: suppose you had two separate namespaces each with a class of the same name. If the Main() method was in the first namespace and you wanted to use a method or variable in the second namespace, then the fully qualified name is required. But whether the class name is same or not, wouldn't you have to do so anyway, in general?

    Technically your textbook is wrong, because you won't need to fully qualify an name on another namespace if you use the using directive. As for your question, yes.

    >> 5) Is it common that private classes contain public variables (modifiers) in practice? I wasn't sure since I have read that for properties, it is required that the restrictiveness is the other way around. I also thought it was possible to eliminate the private class itself, leaving the variables and the Main() method in a container if the variables were going to be public.

    Private classes are private to the assembly they were defined in. They won't be usable from other assemblies. Still a private class may require public members so I can actually use them from within the assembly where they have been defined.

    7) I'm still not sure regarding the difference between a break and return, as they both terminate loops? Or in other words, I know break means go to the next statement, but for return, what does "return statement transfers control to the calling method" mean exactly?

    Break terminates the loop. Continue will ignore the remainder of the code and evaluate the next iteration.

    Code:
    for (int i = 0; i < 10; i++) {
        /* ... some code ...*/
        if (i == 5) continue;
        /* ... some code ... */
    }
    When i becomes 5, the if statement will evaluate to true and continue will immediately transfer control back to the for evaluation expression. What this means is that on the above loop, any remaining code after continue is ignored when i is 5. The next number to be evaluated is 6 and will process normally.
    Last edited by Mario F.; 05-31-2011 at 01:07 PM.
    rags_to_riches likes this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Regarding #3: You need to use a fully qualified name if there's ambiguity between the names. For instance:
    Code:
    using ns1;
    
    namespace ns1
    {
      public class TypicalClass
      {
      }
    }
    
    namespace ns2
    {
      public class TypicalClass
      {
      }
    
      public class Program
      {
        public static void Main()
        {
          TypicalClass tcObject = new TypicalClass();    // Which one?
        }
      }
    }
    There's no way for the computer to know which TypicalClass you're trying to instantiate in Main(), so you'll get an ambiguity error. This is where you use the fully qualified name to fix it: (ns1.TypicalClass tcObject = new ns1.TypicalClass(); )

    #7: return exits the current method. It does not need to be used in a loop. break exits the loop you're in, but continues execution after the loop in the same method.
    Last edited by itsme86; 05-31-2011 at 02:01 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    It's sort of cute when you do it once or twice (asking several questions in a batch). Starts to become boring after that. Especially when a good deal of answers can be reached by actually trying out the code or searching for answer. Don't be lazy.
    THIS.

    Spend more time figuring things out on your own and less time figuring out how to post stupid symbols in your post and subject!
    Salem likes this.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    13
    *that was a little bit harsh, rags, as I do spend hours and days testing things out and experimenting... I wouldn't like it either if someone was feeling lazy, but this is not really the case here, so I'm a bit surprised by your comment
    *nonetheless I value everyone's feedback and will be limiting my help requests... I certainly know I'm taking up someone else's time and effort, so every little bit counts for me
    *God bless
    Last edited by Minty; 06-01-2011 at 08:02 AM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Quote Originally Posted by Mario F. View Post
    >> 3) To paraphrase my textbook: suppose you had two separate namespaces each with a class of the same name. If the Main() method was in the first namespace and you wanted to use a method or variable in the second namespace, then the fully qualified name is required. But whether the class name is same or not, wouldn't you have to do so anyway, in general?

    Technically your textbook is wrong, because you won't need to fully qualify an name on another namespace if you use the using directive. As for your question, yes.
    Actually, the book is right. If you have two classes with the same name in different namespaces, you must use the fully qualified name. For example:
    Code:
    namespace NameSpace1;
    
    class A {
    }
    Code:
    namespace NameSpace2;
    
    class A {
    }
    Putting a 'using' clause at the top of your program won't help as the compiler won't know which 'class A' you want. You must use 'NameSpace1.A' and 'NameSpace2.A' to distinguish between them.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    Yes, istme86 has already corrected me, if you care to read back.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    13
    thanks, I will check it out!

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    13
    hmm, I think I'm gonna go back to studying C++ or VBA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class and template questions
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 07:25 AM
  2. Class questions!
    By Shadowwoelf in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2007, 11:30 AM
  3. Class Questions
    By shane1985 in forum C++ Programming
    Replies: 7
    Last Post: 10-29-2003, 09:34 AM
  4. Series of class questions #1
    By kippwinger in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 04:28 AM
  5. Bless Islamic peoples, Bless us all.
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-28-2001, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21