C#, why static?

From: Markus (markussteen_at_chello.se)
Date: 08/30/04


Date: 30 Aug 2004 06:14:31 -0700

Hi, I'm trying to learn some C#. Can somebody help me with explaining
why the values Frank and Sinatra has to be declered static, (check
code below)? If they aren't, It wont compile correctly:

(24,41): error CS0120: An object reference is required for the
nonstatic field, method, or property 'MyOwn.MyClass.value'
(24,62): error CS0120: An object reference is required for the
nonstatic field, method, or property
'MyOwn.AnotherOwn.AnotherClass.value'

Thanks for taking your time helping me with this!
Markus

using System;
using System.Threading;

namespace MyOwn
{
        public class MyClass
        {
                public static string value = "Frank";
        }

        namespace AnotherOwn
        {
                public class AnotherClass
                {
                        public static string value = "Sinatra";
                }
        }
}

class MyApp
{
        public static void Main()
        {
                Console.WriteLine("My name is {0}",
                    MyOwn.MyClass.value
,MyOwn.AnotherOwn.AnotherClass.value);
 
                Thread.Sleep(100000);
        }
}



Relevant Pages

  • Re: Register script block from app_code
    ... Is it possible to register client script block from app_code function? ... I get an error message: ... Error 2 An object reference is required for the nonstatic field, method, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: problem with static member
    ... > An object reference is required for the nonstatic field, method, or ... > I must do a static member that return a Obj instance. ... what value are you expecting to use for the right hand side here? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dataset question
    ... that one i allready tried, I dont understand still get the same ... An object reference is required for the nonstatic field, method, or ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: creating an object on a from ... little problem ... newbie...
    ... On 28/12/2004 Grisha0 wrote: ... > object reference is required for the nonstatic field, method, or ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ursache gefunden
    ... -> COM Object Reference Zähler. ... Dipl.Inf. Frank Dzaebel ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)