Re: An object reference is required for the non-static field, method, or property

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Jan 27, 11:25 am, Jon Skeet [C# MVP] <sk...@xxxxxxxxx> wrote:
Progress City <progressc...@xxxxxxxxx> wrote:
Compiling the code below gives me the following error

"An object reference is required for the non-static field, method, or
property"

How do I create an object reference for the array "a"?

You'd have to create a new instance of Program, e.g.:

static void Main(string[] args)
{
Program p = new Program();
for (int n = 0; n < 4; n++)
Console.WriteLine(p.a[n]);
Console.ReadKey();
}

Alternatively, make "a" static.

My guess is you're just starting out in C# - in which case a good book
would be a better place to start than newsgroups. It's not that we
dislike answering simple questions, it's just that you'll make progress
faster with a book :)

--
Jon Skeet - <sk...@xxxxxxxxx>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk

Thanks for your incredibly quick reply!


.



Relevant Pages

  • Publishing a service (.NET 4.0)
    ... static int Main(stringargs) ... install = true; break; ... static void Install ... private static void WriteToEventLog(Exception ex, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic and COM
    ...   static void Main(stringargs) ... app will never get the memory allocation as there is no else block. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# equivalent to TryCast
    ... static void Main(stringargs) ... B myclass = new B; ... if (obj is Foo) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Console Application in .NET
    ... static void Main(stringargs) ... catch (Exception ex) ...
    (microsoft.public.dotnet.general)
  • Re: Control.Invoke and ref parameter
    ... the new values in args. ... To repeat myself, x and y don't change, the values in the *array* ... Form form = new Form; ... static void Fiddle ...
    (microsoft.public.dotnet.languages.csharp)