SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# Code Doesn't Work

    I must be a complete moron, because I've done this a million times and for some reason it's not working this time. I'm not sure what my problem is....

    I keep getting an error message:
    myArray1 is a field, but used like a type.
    Code:
    using
     System;
    
    using
     System.Collections.Generic;
    
    using
     System.Linq;
    
    using
     System.Text;
    
    
    namespace
     WindowsFormsApplication1
    
    {
    
        
    classClass1
    
        {
    
            
    // Declare a single-dimensional array 
    
            
    int[] myArray1 = new int[1];
    
            myArray1[0] = 12; // Assign 12 to the first element.
    
        }
    
    }
    
    I don't get it.... According to MSDN... My code is correct..
    http://msdn.microsoft.com/en-us/libr...53(VS.71).aspx

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The offending line is not within a method or property. You can declare class-global variables there and set their default values (i.e. int SomeInteger = 1) but cannot put procedural code there.

    The "this." is optional but I always like to put it there so someone else reading the code knows definitively what's being accessed.

    Code:
    class Class1
        {
            // Declare a single-dimensional array 
            int[] myArray1 = new int[1];
    
            // Fails - not in a property or method
            //myArray1[0] = 12; // Assign 12 to the first element.
    
            public void SomeMethod()
            {
                this.myArray1[0] = 12;
            }
        }

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow.... I'm a complete idiot.... You think I would have known something like that... It's not like this is my first program.. I just wasn't paying attention I guess.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •