An object reference is required for the non-static field, method, or property


Solid State Member
United States
I was making an OS bootloader right now, and while testing the part that would make it fill the screen up. However, when I use the following code:
[FONT=Century Gothic][SIZE=3]using System;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Collections.Generic;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.ComponentModel;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Data;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Drawing;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Linq;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Text;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]using System.Windows.Forms;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]namespace MoazOS_KernelBoot[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]{[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]public partial class krnlboot : Form[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]{[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]public krnlboot()[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]{[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]InitializeComponent();[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]}[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]private void krnlboot_Load(object sender, EventArgs e)[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]{[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]Rectangle resolution = Screen.PrimaryScreen.Bounds;[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]krnlboot.Size = new Size(resolution.Width, resolution.Height);[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]}[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]}[/SIZE][/FONT]
[FONT=Century Gothic][SIZE=3]}[/SIZE][/FONT]
I encounter the following error:
An object reference is required for the non-static field, method, or property System.Window.Forms.Form.Size.get
I do not understand the problem, and all related threads are about textbox text, not window size.


Fully Optimized
You've already found the problem before I got to the thread, but just for a bit of further explanation:

The key here is the age old one that gets many people when they're starting out with OO programming - the difference between an object and a class. You have just one class (of a particular type, in this case krnlboot) but you can have as many objects as you like, and these objects have different fields or attributes. One instance of (or one object of type) krnlboot can have a totally different "Size" to another.

So you're getting the error here because you're referencing krnlboot.Size, the class, rather than an "instance of krnlboot".Size. In short, the compiler doesn't know *what* object of type krnlboot you want the size of. When you use *this.Size* you're explicitly telling the compiler you want the "Size" of this particular object that you're working with at the moment - hence it knows exactly what object you're talking about, and the error disappears.