This short post examines the error message “Cannot declare instance members in a static class” when developing in C# in Visual Studio.
This article provides useful information such as how you can reproduce the error message in order to better understand its root cause, as well as how you can resolve the issue.
Reproducing the Error
Consider the below example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { static class testClass { string sampleString = "Hello World!"; } }
If you try to compile the above code, you will get the error message:
CS0708 ‘testClass.sampleString’: cannot declare instance members in a static class
How to Resolve the Issue
If you see this error message, don’t worry. The solution is very simple.
You just need to specify as static the string declaration within the class. So, the correct code for this example would be the below:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { static class testClass { static string sampleString = "Hello World!"; } }
So, you just compile and that’s it! No errors!
Get Started with .NET Programming Fast and Easy!
Check our online course titled “.NET Programming for Beginners – Windows Forms with C#”
(special limited-time discount included in link).Learn how to implement Windows Forms projects in .NET using Visual Studio and C#, how to implement multithreading, how to create deployment packages and installers for your .NET Windows Forms apps using ClickOnce in Visual Studio, and more!
Many live demonstrations and downloadable resources included!
Featured Online Courses
- Introduction to Azure SQL Database for Beginners
- Data Management for Beginners – Main Principles
- SQL Server 2019: What’s New – New and Enhanced Features
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Administration Tips
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Development Tips for SQL Developers
- Working with Python on Windows and SQL Server Databases
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Read Also
Feel free to check our other relevant articles on SQL Server troubleshooting:
- .NET Programming for Beginners – Windows Forms (C#)
- Using ClickOnce for Deploying your .NET Windows Forms Apps
- Error converting data type varchar to float
- Error converting data type varchar to numeric
- SQLServerAgent could not be started (reason: Unable to connect to server ‘(local)’; SQLServerAgent cannot start)
- ORDER BY items must appear in the select list if SELECT DISTINCT is specified
- There is no SQL Server Failover Cluster Available to Join
- There is insufficient system memory in resource pool ‘internal’ to run this query.
- The type or namespace name ‘Office’ does not exist in the namespace ‘Microsoft’ – How to Resolve
- … all SQL Server troubleshooting articles
Featured Database Security and Administration Tools
DBA Security Advisor: Secure your SQL Server instances by scanning multiple instances against a rich set of security checks, and by getting recommendations and remediation steps.
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check out Artemakis’s eBooks!
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.