a field initializer cannot reference the nonstatic field method or property Error

a field initializer cannot reference the non-static field, method, or property unity
a field initializer cannot reference the non-static field c# array
an object reference is required for the non-static field method or property
dotnet an object reference is required for the non static field method or property
an object reference is required for non static method or property
an object reference is required for the non static field method or property rextester program
c# object reference is required for non static method
c# constructor

I am trying to work with .Net C# and Azure blob storage

I follow Microsoft's documentation in order to access a blob table.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    public class EmailAdress
    {
        CloudStorageAccount storageAccount = new CloudStorageAccount(
            new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                "experimentstables", "token"), true);

        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Get a reference to a table named "peopleTable"
        CloudTable pexperimentsEmailAddresses = tableClient.GetTableReference("experimentsEmailAddresses");
    }
}

in this line

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

storageAccount is marked red with the following error:

a field initializer cannot reference the nonstatic field method or property

How should I fix it?

Create a constructor and implement all your field initializations there.

public class EmailAdress
{
    CloudStorageAccount storageAccount;
    CloudTableClient tableClient;
    CloudTable pexperimentsEmailAddresses;

    public EmailAdress()
    {
        storageAccount = new CloudStorageAccount(
        new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
            "experimentstables", "token"), true);

        // Create the table client.
        tableClient = storageAccount.CreateCloudTableClient();

        // Get a reference to a table named "peopleTable"
        pexperimentsEmailAddresses = tableClient.GetTableReference("experimentsEmailAddresses");
    }
}

A field initializer cannot reference the non-static field, method, or , From C# specification: Quote: A variable initializer for an instance field cannot reference the instance being created. It means you can't initialize  A field initializer cannot reference the nonstatic field, method, or property (4 answers) Closed 5 years ago. when i compile the code below the compiler give the error:A field initializer cannot reference the non-static field, method, or property in this code (which have stars) KingPiece kingPiece = new KingPiece(***siyahsah1***,ChessColor.White);

You declared storageAccount and tableClient as class members, so storageAccount has to be static in order to use it

public class EmailAdress
{
    static CloudStorageAccount storageAccount = new CloudStorageAccount(...);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
}

Or you can put the initialization inside method.

Compiler Error CS0236, ToString()); This line is giving me the error. A field initializer cannot reference the nonstatic field, method, or property. What does this mean? CS0120: An object reference is required for the nonstatic field, method, or property 'foo' 87 A field initializer cannot reference the nonstatic field, method, or property

The c# language specification clearly states:

A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to reference any instance member through a simple_name.

You can initialize a field with respect to another field only in a constructor.

Will not compile:

class A
{
    int x = 1;
    int y = x + 1;        // Error, reference to instance member of this
}

Will compile:

class A
{
    public A() 
    {
        int x = 1;
        int y = x + 1;        // Works just fine
    }
}

Error -- A field initializer cannot reference the non-static field, method , A field initializer cannot reference the nonstatic field, method, or property 'field'. Instance fields cannot be used to initialize other instance fields  Symptoms When trying to enter play mode, I'm receiving the error below: error CS0236: A field initializer cannot reference the nonstatic field, method, or property

"A field initializer cannot reference the nonstatic field, method, or , But I am getting this error in the ViewModel section: Error -- A field initializer cannot reference the non-static field, method, or property  Due to the initialization order, instance constructors are executed last, which is why the instance is not created the moment the initializers are executed. Therefore the compiler cannot allow any instance property (or field) to be referenced before the class instance is fully constructed.

a field initializer cannot reference the non static field method or , When I start the game, the error occurs: "A field initializer cannot reference the non static field, method, or property", for each transform and  CS0236: A field initializer cannot reference the non-static field, method, or property 'add_contact2.db' [Answered] RSS 2 replies Last post May 28, 2008 12:10 AM by acbarberi

What is CS0236? – Unity, "A field initializer cannot reference the nonstatic field, method, or property". Question. ADD TO TITLE: SphereController.speed. How would I fix this error? Script: This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use. Learn more

Comments
  • Why do you write your code inside class? Make some method or constructor and write your code there.