Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I found this strange in C#

I found this strange in C#

Scheduled Pinned Locked Moved C#
csharphelplearning
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brian_TheLion
    wrote on last edited by
    #1

    I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian

    L D 4 Replies Last reply
    0
    • B Brian_TheLion

      I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A class should be a black box, where you don't have to know about the inside, and can't muck up the internal state from the outside. The parts where you find you need access to a variable, you'll often need a property.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      B 1 Reply Last reply
      0
      • L Lost User

        A class should be a black box, where you don't have to know about the inside, and can't muck up the internal state from the outside. The parts where you find you need access to a variable, you'll often need a property.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        B Offline
        B Offline
        Brian_TheLion
        wrote on last edited by
        #3

        Thanks Eddy for giving me another way of understanding things in C#. Correct me if I'm wrong but I'm also thinking that an instance variable is a reference to a variable. Brian

        L 1 Reply Last reply
        0
        • B Brian_TheLion

          I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          It sounds like you're not using OOP at all and just declaring all of your variable and classes as static. That's not good practice. But, that's just a guess as we can't see you're code and you haven't posted any.

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          B 1 Reply Last reply
          0
          • D Dave Kreskowiak

            It sounds like you're not using OOP at all and just declaring all of your variable and classes as static. That's not good practice. But, that's just a guess as we can't see you're code and you haven't posted any.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            B Offline
            B Offline
            Brian_TheLion
            wrote on last edited by
            #5

            Hi Dave. Sorry but my code is a bit messy as I'm experimenting while learning C#. At the bottom of the code you'll find num=i; then near the top I have numberOfVerbs = num; I could not use numberOfVerbs = i; I have found out since that I could have used the .long command to find out how many CollectText's created. I use to be able to run the code without errors but something has changed to cause some errors.

            namespace Lads_in_CSharp
            {
            public class Program
            {
            int i = 0;
            int num;
            int numberOfVerbs;
            int y=0;
            int a = 0;
            //String sr;
            String Text1="";
            String[] CollectText = new String[1000];
            String [] TT = new String[3] ;
            public static void Main(string[] args)
            {
            Program prog = new Program();
            StreamReader sr = new StreamReader("demo.adv");

                   Console.WriteLine("Start of Program");
                   prog.y = 4;
                   prog.ReadHeader(sr);
                   Console.WriteLine(prog.i);
                   prog.ReadVerbs(sr);
                   prog.ReadNouns(sr);
                   prog.ReadObjects(sr);
                   prog.ReadStartRoom(sr);
                   prog.ReadRooms(sr);
                   prog.ReadMessages(sr);
                   prog.ReadAutoActions(sr);
                   prog.ReadActions(sr);
                   sr.Close(); 
                   Console.Read();
            
                }
                public void ReadHeader(StreamReader sr)
                {
                    
                    Console.WriteLine("Read Header");
                  
                        ReadLineOfText(sr);
                        Console.WriteLine("Text1 is: " + sr);
                              
                 //   Console.WriteLine(y);
                  //  i = 6;
                }
            
                public void ReadVerbs(StreamReader sr) //                                      VERBS
                {
                    Console.WriteLine("Read Verbs++++++++++");
                    // Verb1 = "Paint";
                    int i=0;
                    ReadLineOfText(sr);
                    numberOfVerbs = num;
                    Console.WriteLine("Text1 is:: " + Text1);
                    Console.WriteLine("Collect Text 2 = "+CollectText\[2\]);   // TEST
                    Console.WriteLine("Number of Verbs is "+num);
            
                    // string\[\] authorsList = Text1.Split(",");
            
            
                    Console.WriteLine("Comma separated strings");
                    // String of authors  
                    //  string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";
                    // Split authors separated by a comma followed by space  
                    //string\[\] author
            
            D 1 Reply Last reply
            0
            • B Brian_TheLion

              Hi Dave. Sorry but my code is a bit messy as I'm experimenting while learning C#. At the bottom of the code you'll find num=i; then near the top I have numberOfVerbs = num; I could not use numberOfVerbs = i; I have found out since that I could have used the .long command to find out how many CollectText's created. I use to be able to run the code without errors but something has changed to cause some errors.

              namespace Lads_in_CSharp
              {
              public class Program
              {
              int i = 0;
              int num;
              int numberOfVerbs;
              int y=0;
              int a = 0;
              //String sr;
              String Text1="";
              String[] CollectText = new String[1000];
              String [] TT = new String[3] ;
              public static void Main(string[] args)
              {
              Program prog = new Program();
              StreamReader sr = new StreamReader("demo.adv");

                     Console.WriteLine("Start of Program");
                     prog.y = 4;
                     prog.ReadHeader(sr);
                     Console.WriteLine(prog.i);
                     prog.ReadVerbs(sr);
                     prog.ReadNouns(sr);
                     prog.ReadObjects(sr);
                     prog.ReadStartRoom(sr);
                     prog.ReadRooms(sr);
                     prog.ReadMessages(sr);
                     prog.ReadAutoActions(sr);
                     prog.ReadActions(sr);
                     sr.Close(); 
                     Console.Read();
              
                  }
                  public void ReadHeader(StreamReader sr)
                  {
                      
                      Console.WriteLine("Read Header");
                    
                          ReadLineOfText(sr);
                          Console.WriteLine("Text1 is: " + sr);
                                
                   //   Console.WriteLine(y);
                    //  i = 6;
                  }
              
                  public void ReadVerbs(StreamReader sr) //                                      VERBS
                  {
                      Console.WriteLine("Read Verbs++++++++++");
                      // Verb1 = "Paint";
                      int i=0;
                      ReadLineOfText(sr);
                      numberOfVerbs = num;
                      Console.WriteLine("Text1 is:: " + Text1);
                      Console.WriteLine("Collect Text 2 = "+CollectText\[2\]);   // TEST
                      Console.WriteLine("Number of Verbs is "+num);
              
                      // string\[\] authorsList = Text1.Split(",");
              
              
                      Console.WriteLine("Comma separated strings");
                      // String of authors  
                      //  string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";
                      // Split authors separated by a comma followed by space  
                      //string\[\] author
              
              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Yeah, you've thrown all the variables at the class level. That's bad. You have no reason to do that at all. Rewrite the thing from scratch, but think about what each method is going to handle, what it's going to need to do that job, and what it's going to return. Each method is going to need its own variables, so declare them in that method. In this code, you've got no reason to have ANY class level variables at all.

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              B 1 Reply Last reply
              0
              • B Brian_TheLion

                I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I would strongly urge you to stop trying to write code until you have worked through the two books that I have suggested. Unless and until you fully understand the basics you will be falling over simple issues like this. There are no shortcuts to becoming a developer, it requires time, effort and practice, practice, practice.

                B 1 Reply Last reply
                0
                • L Lost User

                  I would strongly urge you to stop trying to write code until you have worked through the two books that I have suggested. Unless and until you fully understand the basics you will be falling over simple issues like this. There are no shortcuts to becoming a developer, it requires time, effort and practice, practice, practice.

                  B Offline
                  B Offline
                  Brian_TheLion
                  wrote on last edited by
                  #8

                  I understand what your saying Richard and like you said it takes practice which is what I'm doing in writing some code. I see it like trying to drive a car. You can read a lot but you need to get behind the wheel and start driving to get the practice. I only submitted code because I was asked to do so by someone giving me some feedback. If I can write a simple example program from something I've learnt from a book and it successfully works then I move on to learn more of the C# programming. Brian

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Yeah, you've thrown all the variables at the class level. That's bad. You have no reason to do that at all. Rewrite the thing from scratch, but think about what each method is going to handle, what it's going to need to do that job, and what it's going to return. Each method is going to need its own variables, so declare them in that method. In this code, you've got no reason to have ANY class level variables at all.

                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                    Dave Kreskowiak

                    B Offline
                    B Offline
                    Brian_TheLion
                    wrote on last edited by
                    #9

                    Thanks David for your feedback. Well it started as a old program I wanted to convert to C# and I decided toturn methods into classes to try and separate the code and give me some practice on using classes. The program collects information from a script file then compiles and writes the compiled data to file which is loaded into the main program. While I started to write the program code to create a suitable program I'm now using the code to experiment with and try out things I have learnt. The code is likely to be re-wriiten. Brian

                    1 Reply Last reply
                    0
                    • B Brian_TheLion

                      Thanks Eddy for giving me another way of understanding things in C#. Correct me if I'm wrong but I'm also thinking that an instance variable is a reference to a variable. Brian

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Doesn't have to be; you can also have a value-type as an instance variable. If it is not a value-type, then it is indeed a reference.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      1 Reply Last reply
                      0
                      • B Brian_TheLion

                        I had all my classes made public so that I could use variables in any part of the code. I found that if I used the variable 'i' like this Name[i] where 'i' had been declared a integer then I could not use 'i' elsewhere in my program. To get over this problem I had to add the code maxcount=i; then I could refer to the variable maxcount elsewhere in my code. There must be a good reason for this but as I'm new to C# I found this strange. I also find that sometimes you need an instance of a variable (Students student = new Students) and other times you don't need to do this. I'm still trying to get my head around reasons for this when learning C#. Brian Brian

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        You have discovered "references" (very early). Once you understand them (and public, private and static), you will achieve enlightenment.

                        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups