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 is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.