none
Static Array : Declaration and Usage : C Sharp | C# RRS feed

  • Question

  • hey there Microsoft;
    this is the updated question solved by MSDN Forum Programmers;

    namespace MSDN { sealed class SampleClass { //-- Variable Declarations ------------------------------- static System.Boolean MyBoolean = false; static System.String MyString = null; static System.Double[] MyArray = new System.Double[10]; //as required;
    static System.Double[,] MyArray2 = new System.Double[10, 10];
    static System.Windows.Point MyPoint = new System.Windows.Point();
    static System.Windows.Controls.Canvas MyCanvas = null;

    [System.STAThread] static void Main() { //-- Variables Usage --------------------------------- MyBoolean = false; MyBoolean = true; MyString = "Some String"; MyString = "now Other String"; MyArray[1] = 4; MyArray[2] = 10.3;

    MyArray2[0,1] = 7;
    MyArray2[4,3] = 100.21;

    MyPoint.X = 0;
    MyPoint.Y = 0;

    MyCanvas = someCanvas;

    } } }


    PREVIOUS ERROR:: System.NullReferenceException: 'Object reference not set to an instance of an object.'

    it where said cause of how it are needed to specify the variable initialization;
    meaning in Visual Studio the thought process are held to the Framework hence the hype for thinking it of being Open Sourced;

     SIDE NOTE :
    Visual Studio has better editor code presentation for the C Sharp language than for Visual Basic which has better IntelliSence descriptions;
    meaning in Pinhead quote: "We'll Tear Your Soul Apart";

    Tuesday, August 13, 2019 12:17 AM

Answers

  • Greetings WA.

    I am not sure you realise you do not have to initialise an array with a list like that. You can just give the array a size, and then access each element by its index.

    namespace MSDN {
    
        sealed class SampleClass {
            //-- Variable Declarations -------------------------------
            static System.Boolean MyBoolean = false;
            static System.String MyString = null;
            static System.Double[] MyArray = new System.Double[3];
    
            [System.STAThread]
            static void Main() {
    
                //-- Variables Usage ---------------------------------
                MyBoolean = false;
                MyBoolean = true;
    
                MyString = "Some String";
                MyString = "now Other String";
    
                MyArray[1] = 4;
                MyArray[2] = 10;
            }
    
        }
    
    }

    Tuesday, August 13, 2019 2:28 AM
  • Ante Meridian - MiTek Australia Ltd. illustrated the use of Arrays on MSDN Forum;  

          So Here's the Updated Content based on the accepted Answer;

    namespace MSDN {
    
        sealed class SampleClass {
            //-- Variable Declarations -------------------------------
            static System.Boolean MyBoolean = false;
            static System.String MyString = null;
            static System.Double[] MyArray = new System.Double[10];
    
            [System.STAThread]
            static void Main() {
    
                //-- Variables Usage ---------------------------------
                MyBoolean = false;
                MyBoolean = true;
    
                MyString = "Some String";
                MyString = "now Other String";
    
                MyArray[0] = 1;
                MyArray[0] = 4;
            }
    
        }
    
    }

    MyArray from Ante Meridian indicates the new keyword at the variable declaration;
    this seen very confusing when thinking of static variables - then this is how it worked;

    in the previous (Sample Code) the usage was made in wonder on Index Based: MyArray[X] = X;
    and this example works just like that;



    Tuesday, August 13, 2019 3:59 AM

All replies

  • Hello,

    Please read the documentation which has examples. Static or not the docs explain with examples.

    int[] array3;
    array3 = new int[] { 1, 3, 5, 7, 9 };   // OK
    //array3 = {1, 3, 5, 7, 9};   // Error
    int[] intArray = new int[10];



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 13, 2019 12:41 AM
    Moderator
  • Karen Payne - StackOverflow has illustrated the use of Arrays on MSDN Forum;  

          So Here's the Updated Content based on the accepted Answer;

          
    namespace MSDN {
    
        sealed class SampleClass {
            //-- Variable Declarations -------------------------------
            static System.Boolean MyBoolean = false;
            static System.String MyString = null;
            static System.Double[] MyArray = null;
    
            [System.STAThread]
            static void Main() {
    
                //-- Variables Usage ---------------------------------
                MyBoolean = false;
                MyBoolean = true;
    
                MyString = "Some String";
                MyString = "now Other String";
    
                MyArray = new System.Double[] { 0, 4, 10 };
                MyArray = new System.Double[] { 0, MyArray[1], 10 };
            }
    
        }
    
    }

    MyArray from Karen Payne illustration overwrites the previous content by Default;
    so a more complicated usability is made to preserve the Array Contents as more complicated it goes;

    in the previous (Sample Code) the usage was made in wonder on Index Based: MyArray[X] = X;
    thus preserving the rest of the Array Contents;


    Tuesday, August 13, 2019 2:03 AM
  • Greetings WA.

    I am not sure you realise you do not have to initialise an array with a list like that. You can just give the array a size, and then access each element by its index.

    namespace MSDN {
    
        sealed class SampleClass {
            //-- Variable Declarations -------------------------------
            static System.Boolean MyBoolean = false;
            static System.String MyString = null;
            static System.Double[] MyArray = new System.Double[3];
    
            [System.STAThread]
            static void Main() {
    
                //-- Variables Usage ---------------------------------
                MyBoolean = false;
                MyBoolean = true;
    
                MyString = "Some String";
                MyString = "now Other String";
    
                MyArray[1] = 4;
                MyArray[2] = 10;
            }
    
        }
    
    }

    Tuesday, August 13, 2019 2:28 AM
  • Ante Meridian - MiTek Australia Ltd. illustrated the use of Arrays on MSDN Forum;  

          So Here's the Updated Content based on the accepted Answer;

    namespace MSDN {
    
        sealed class SampleClass {
            //-- Variable Declarations -------------------------------
            static System.Boolean MyBoolean = false;
            static System.String MyString = null;
            static System.Double[] MyArray = new System.Double[10];
    
            [System.STAThread]
            static void Main() {
    
                //-- Variables Usage ---------------------------------
                MyBoolean = false;
                MyBoolean = true;
    
                MyString = "Some String";
                MyString = "now Other String";
    
                MyArray[0] = 1;
                MyArray[0] = 4;
            }
    
        }
    
    }

    MyArray from Ante Meridian indicates the new keyword at the variable declaration;
    this seen very confusing when thinking of static variables - then this is how it worked;

    in the previous (Sample Code) the usage was made in wonder on Index Based: MyArray[X] = X;
    and this example works just like that;



    Tuesday, August 13, 2019 3:59 AM
  • Greetings again WA.

    The word 'static' does not mean the same thing in c# as it does in Pascal. In c# it means the data exists only once for the class, not once for each member of the class. Here is some documentation.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static

    A lot of beginners are confused by what static means in c#, so if you have any questions, just ask and we will try to explain.

    Tuesday, August 13, 2019 4:11 AM