Error cs0106 the modifier static is not valid for this item

This repository contains .NET Documentation. Contribute to dotnet/docs development by creating an account on GitHub.

Permalink

Cannot retrieve contributors at this time

description title ms.date f1_keywords helpviewer_keywords ms.assetid

Compiler Error CS0106

Compiler Error CS0106

06/15/2017

CS0106

CS0106

8dec906a-ed69-4ed5-aa61-c8600d138200

Compiler Error CS0106

The modifier ‘modifier’ is not valid for this item

A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers:

  • The static modifier is not permitted on a local function. The static local function feature is supported starting with C# 8.0. A compiler that doesn’t support C# 8.0 produces CS0106 when you try to use this feature. However, a compiler that supports C# 8.0 but the set language version is prior to C# 8.0 will produce a diagnostic suggesting that you use C# 8.0 or later.

  • The public keyword is not allowed on an explicit interface declaration. In this case, remove the public keyword from the explicit interface declaration.

  • The abstract keyword is not allowed on an explicit interface declaration because an explicit interface implementation can never be overridden.

  • Access modifiers are not allowed on a local function. Local functions are always private.

In prior releases of Visual Studio, the static modifier was not permitted on a class, but static classes are allowed starting with Visual Studio 2005.

For more information, see Interfaces.

Example

The following sample generates CS0106:

// CS0106.cs
namespace MyNamespace
{
   interface I
   {
      void M();
   }

   public class MyClass : I
   {
      public void I.M() {}   // CS0106
      public static void Main() {}
   }
}

How can we implement static methods in interface…?

public interface ICache
{
  //Get item from cache
  static object Get(string pName);

  //Check an item exist in cache
  static bool Contains(string pName); 

  //Add an item to cache
  static void Add(string pName, object pValue);

  //Remove an item from cache
  static void Remove(string pName);
}

The above interface throws error: The modifier ‘static’ is not valid for this item

asked Apr 19, 2013 at 10:12

Sunil's user avatar

SunilSunil

2,8655 gold badges34 silver badges41 bronze badges

3

You can’t do it. It should be

   public interface ICache
    {
      //Get item from cache
      object Get(string pName);
      //Check an item exist in cache
      bool Contains(string pName);
      //Add an item to cache
      void Add(string pName, object pValue);
      //Remove an item from cache
      void Remove(string pName);
    }

Check out Why Doesn’t C# Allow Static Methods to Implement an Interface?

Also Eric Lippert wrote a cool article series called

  • Calling static methods on type parameters is illegal, part one, part two

Community's user avatar

answered Apr 19, 2013 at 10:14

Soner Gönül's user avatar

Soner GönülSoner Gönül

96k102 gold badges205 silver badges356 bronze badges

And it’s absolutely right. You can’t specify static members in an interface. It has to be:

public interface ICache
{
  //Get item from cache
  object Get(string pName);

  //Check an item exist in cache
  bool Contains(string pName);

  //Add an item to cache
  void Add(string pName, object pValue);

  //Remove an item from cache
  void Remove(string pName);
}

(Your comments should be XML documentation comments, by the way — that would make them much more useful. I’ve added some whitespace between members to make the code easier to read. You should also consider making the interface generic.)

Why did you try to make the members static in the first place? What did you hope to achieve?

answered Apr 19, 2013 at 10:13

Jon Skeet's user avatar

Jon SkeetJon Skeet

1.4m851 gold badges9045 silver badges9133 bronze badges

No, you can’t… static methods/variables refer to the class itself not the instance of that class and since the purpose of interfaces is to be implemented by classes you cannot have static methods in a interface… it doesn’t make sense

answered Apr 19, 2013 at 10:14

Stephan's user avatar

StephanStephan

7,9323 gold badges35 silver badges42 bronze badges

public static void main (Args _args)
{
TmpFrmVirtual tmpFrmVirtualVend;
PurchFormLetter_Invoice purchFormLetter;
VendPackingSlipJour vendPackingSlipJour;
SysQueryRun chooseLinesQuery;
SysQueryRun chooseLinesPendingInvoiceQuery;
container conTmpFrmVirtual;
List selectedList = new List(Types::Record);
PurchId purchId = «PO00001» ; //Purchase order number
PackingSlipId packingSlipId = «PCK0001»; //Product receipt number

select firstonly vendPackingSlipJour
where vendPackingSlipJour.PurchId == purchId
&& vendPackingSlipJour.PackingSlipId == packingSlipId;

if (vendPackingSlipJour)
{
tmpFrmVirtualVend.clear();
tmpFrmVirtualVend.TableNum = vendPackingSlipJour.TableId;
tmpFrmVirtualVend.RecordNo = vendPackingSlipJour.RecId;
tmpFrmVirtualVend.NoYes = NoYes::Yes;
tmpFrmVirtualVend.Id = vendPackingSlipJour.PurchId;
tmpFrmVirtualVend.insert();
}

chooseLinesQuery = new SysQueryRun(queryStr(PurchUpdate));
chooseLinesQuery.query().addDataSource(tableNum(VendInvoiceInfoTable)).enabled(false);

// chooseLinesPendingInvoiceQuery needs to be initialized, although it will not be used
chooseLinesPendingInvoiceQuery = new SysQueryRun(queryStr(PurchUpdatePendingInvoice));
chooseLinesPendingInvoiceQuery.query().dataSourceTable(tableNum(PurchTable)).addRange(fieldNum(PurchTable,PurchId)).value(queryValue(»));

purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.chooseLinesQuery (chooseLinesQuery);
purchFormLetter.parmQueryChooseLinesPendingInvoice(chooseLinesPendingInvoiceQuery);
purchFormLetter.purchTable (PurchTable::find(PurchId));
purchFormLetter.transDate (systemDateGet());
purchFormLetter.parmParmTableNum (strFmt(«%1»,packingSlipId)); //This is invoice number
purchFormLetter.printFormLetter (NoYes::No);
purchFormLetter.sumBy (AccountOrder::Auto);
purchFormLetter.specQty (PurchUpdate::PackingSlip);

while select tmpFrmVirtualVend
{
selectedList.addEnd(tmpFrmVirtualVend);
conTmpFrmVirtual = selectedList.pack();
}
purchFormLetter.selectFromJournal(conTmpFrmVirtual);
purchFormLetter.reArrangeNow(true);
purchFormLetter.run();
}

Hhsajfdaa

0 / 0 / 0

Регистрация: 01.09.2021

Сообщений: 1

1

01.09.2021, 21:32. Показов 1570. Ответов 2

Метки unity (Все метки)


C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Player : MonoBehaviour
{
  public ControlType controlType;
  public Joystick joystick;
  public float speed;
 
  public enum ControlType{PC, Android}
 
  private Rigidbody2D rb;
  private Vector2 moveInput;
  private Vector2 moveVelocity;
  private Animator anim;
 
  private bool facingRight = true;
 
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        if (controlType == ControlType.PC)
        {
          joystick.gameObject.SetActive(false);
        }
    }
 
    // Update is called once per frame
    void Update()
    {
      if(controlType == ControlType.PC)
      {
       moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
      }
        else if (controlType == ControlType.Android)
      {
       moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
      }
        moveVelocity = moveInput.normalized  * speed;
 
        if(moveInput.x == 0)
        {
          anim.SetBool("isRuning", false);
        }
        else
        {
          anim.SetBool("isRuning", true);
        }
 
        if(!facingRight && moveInput.x > 0)
        {
          Flip();
        }
        else if(facingRight && moveInput.x < 0)
        {
          Flip();
 
    }
 
   void FixedUpdate()
   {
     rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
   }
 
  private void Flip()
  {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
  }
}

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



1 / 1 / 0

Регистрация: 16.07.2021

Сообщений: 14

20.09.2021, 13:46

2

У тебя стоит на методе Flip доступ private и ты выше в другом методе пытаешься его получить. Убери private у метода Flip, должно заработать



0



603 / 445 / 196

Регистрация: 05.04.2015

Сообщений: 1,773

20.09.2021, 13:50

3

Hhsajfdaa,
скобки не хватает, наиди сам где



0



IT_Exp

Эксперт

87844 / 49110 / 22898

Регистрация: 17.06.2006

Сообщений: 92,604

20.09.2021, 13:50

3

Понравилась статья? Поделить с друзьями:
  • Error cs0103 unity
  • Error cs0103 the name thread does not exist in the current context
  • Error cs0103 the name scenemanager does not exist in the current context
  • Error cs0103 the name scenemanagement does not exist in the current context
  • Error cs0103 the name math does not exist in the current context