none
Why error coming if you have non static variable if you have static method? RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace testconsoleapplication
    {
        class Program
        {

    #region File stream with IIS log testing
            private string path = @"c:\MyDir";
            static void Main(string[] args)
            {

                //Program objProgram = new Program();
                //string path1 = objProgram.path;
                try 
                {
                    // Determine whether the directory exists. 
                    if (Directory.Exists(path)) 
                    {
                        Console.WriteLine("That path exists already.");

                    }

                    // Try to create the directory.
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

                    // Delete the directory.
                    di.Delete();
                    Console.WriteLine("The directory was deleted successfully.");
                }
               catch(Exception ex)
                {
                    Console.WriteLine("Exception Massage--!--" + ex.Message + "--!--");
                }
            }
            #endregion

                       }}
    Monday, July 8, 2013 11:35 AM

Answers

  • A static member can only access other static members. This is because a static member belongs to the type itself rather than to a specific instance of the class.

    As the Main method is declared static, it can only access static fields.

    You can read more about the static keyword on MSDN here: http://msdn.microsoft.com/en-us/library/98f28cdx.aspx

    Monday, July 8, 2013 1:04 PM
  • Whenever you use the "new" keyword some memory (heap) is allocated.

    You have told the computer that this string should be in the chunk of memory that only exists when you use the new operator.

    Therefore you cannot use non-static variables in a static context.

    Monday, July 8, 2013 11:41 AM

All replies

  • I am getting build error.

    I know why its is !

    private string path = @"c:\MyDir";

    I have correct that 

    private static string path = @"c:\MyDir";

    But why we need this to make static.????

    Monday, July 8, 2013 11:38 AM
  • Whenever you use the "new" keyword some memory (heap) is allocated.

    You have told the computer that this string should be in the chunk of memory that only exists when you use the new operator.

    Therefore you cannot use non-static variables in a static context.

    Monday, July 8, 2013 11:41 AM
  • A static member can only access other static members. This is because a static member belongs to the type itself rather than to a specific instance of the class.

    As the Main method is declared static, it can only access static fields.

    You can read more about the static keyword on MSDN here: http://msdn.microsoft.com/en-us/library/98f28cdx.aspx

    Monday, July 8, 2013 1:04 PM
  • Hi,

    you can get some help from this link: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

    Tuesday, July 9, 2013 7:16 AM