C# Compiler Error

CS0176 – Static member ‘member’ cannot be accessed with an instance reference; qualify it with a type name instead

Reason for the Error

You will receive this error when you try to access a static variable of a class with an instance as a qualifier.

For example, try compiling the below code snippet




You are watching: Qualify it with a type name instead

namespace DeveloperPubNamespace{ public class Employee { public static int EmployeeCount = 0; } class Program { static void Main(string<> args) { Employee emp = new Employee(); emp.EmployeeCount=1; } }}
The above code snippet will result with the error code CS0176

Error CS0176 Member ‘Employee.EmployeeCount’ cannot be accessed with an instance reference; qualify it with a type name instead ConsoleApp3 C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 13 Active

*

Solution

To fix this error, ensure that you access the static variable directly using the class name instead of the instance name as the qualifier. The above code snippet can be fixed by replacing the Main() function with the below code snippet.


Previous Article
*
C# Error CS0175 – Use of keyword ‘base’ is not valid in this context
Next Article
*
C# Error CS0177 – The out parameter ‘parameter’ must be assigned to before control leaves the current method

C# Error CS0507 – ‘function1’ : cannot change access modifiers when overriding ‘access’ inherited member ‘function2’


C# Error CS0506 – ‘function1’ : cannot override inherited member ‘function2’ because it is not marked “virtual”, “abstract”, or “override”


C# Error CS0501 – ‘member function’ must declare a body because it is not marked abstract, extern, or partial


C# Error CS0431 – Cannot use alias ‘identifier’ with ‘::’ since the alias references a type. Use ‘.’ instead


Categories

CategoriesSelect Category.NET (44)Analytics (1)Apple (1)ASP.NET (24)Azure (12)Blogging (4)Content Writing (1)CSharp (379)Delphi (14)Excel (585)General (203)Gmail (8)Google (2)Google Search (4)How To (42)JavaScript (19)Microsoft Office (2)Outlook (3)OutSystems (3)Powerpoint (69)PowerShell (4)SQL Server (60)Uncategorized (2)Visual Studio (111)Windows (383)Word (86)
c#C# Compiler ErrorsC# tips and TrickscsharpDownloadExcelExcel 2016Excel Automationexcel formulaexcel functionsexcel tipsexcel tutorialsExcel VBAFunctions in Excelhow tojavascriptLINQMicrosoftMicrosoft Excelmicrosoft powerpointmicrosoft wordMS Officems powerpointOxygenePowerPointPowerPoint 2013pptSQL ServertipstrickstutorialtutorialsVisual StudioVisual Studio 2012visual studio 2013visual studio 2015Windowswindows 10Windows Phonewindows phonewindows phone 8Word tipsWP7wp8XAML
*
CSharp
*
CSharp
*
CSharp
*
CSharp


See more: Quote By George Carlin: “ If You Try To Fail And Succeed Which Have You Done ?

C# Error CS0507 – ‘function1’ : cannot change access modifiers when overriding ‘access’ inherited member ‘function2’