Resolved CS0120 An object reference is required for the non-static filled, .....

Squatcher

Member
Joined
Mar 1, 2023
Messages
5
Programming Experience
Beginner
Good morning,
I write code for my own use, so I am not a day to day coder and I have spans of time that I dont even touch Visual Studio.

But, I am getting an error that I am having difficulty figuring out why.

I started a new project, there is only one Form and in that form it has a Label that I am attempting to attach information from the Properties.Settings and got the error of CS0120. I am sure at one time I figured it out, but having issues today.

Main:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ham_Radio.Properties;


namespace Ham_Radio
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();

            // Script to size Form to half monitors size.
            StartPosition = FormStartPosition.Manual;
            Rectangle screen = Screen.FromPoint(Cursor.Position).WorkingArea;
            int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
            int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
            Location = new Point(screen.Left + (screen.Width - w) / 2, screen.Top + (screen.Height - h) / 2);
            Size = new Size(w, h);
        }
        private void Main_Load(object sender, EventArgs e)
     
        {


            lblCallSign.Text = Settings.CallSign.ToString(); //<< This is the area giving me the issues and I am not sure why.

            //MessageBox.Show("Hello");
        }
    }
}

Thank you
 
Shouldn't it be Properties.Settings.CallSign ?
 
I changed the field from Settings.CallSign.ToString(); to Properties.Settings.CallSign.ToString(); and still receiving the error.

Just a bit of additional information, the VS2019 is a fresh install because my former laptop died. So, I am not sure if I am missing any settings, or additional addons that should have been installed, but are not.
 
Please provide the full error message. Often there are clues in the error message that reveals what the compiler thinks it is having issues with.
 
Please provide the full error message. Often there are clues in the error message that reveals what the compiler thinks it is having issues with.
1677702647703.png


1677702493653.png
 
Apparently with modern WinForms, you also need to access the Default instance as part of accessing the Settings:
1677710371101.png


1677710340577.png



I vaguely recall the older version of WinForms where it didn't seem like I had to add the Default part to access settings. But that was over 20 years ago when I last played with settings so my recollection maybe wrong.
 
Apparently with modern WinForms, you also need to access the Default instance as part of accessing the Settings:
View attachment 2695

View attachment 2694


I vaguely recall the older version of WinForms where it didn't seem like I had to add the Default part to access settings. But that was over 20 years ago when I last played with settings so my recollection maybe wrong.

That was great. Seems like your correct about the older version, because I lost the older version I had when I lost the old laptop, so I had to get a more newer version.

Again, Thank you till me next issue.
 
You can set Callsign to be of string type, so the ToString is/can be unnecessary

You can bind the Text property of the label in the designer, so you don't have to set it in your code and the binding is two way so if the setting changes the label updates automatically. To do this use the (ApplicationSettings) node of the properties grid for the label
 
You can bind the Text property of the label in the designer, so you don't have to set it in your code and the binding is two way so if the setting changes the label updates automatically. To do this use the (ApplicationSettings) node of the properties grid for the label
(ApplicationSettings) in designer is only supported for .Net Framework projects. You can still create a setting binding in code.
 
(Though I have successfully used a .NET FW project to design a winforms project that runs on .NET 7)
 

Latest posts

Back
Top Bottom