Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 07-21-2012, 12:48 PM   #1
Solid State Member
 
Join Date: May 2012
Location: United States
Posts: 10
Default An object reference is required for the non-static field, method, or property

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:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MoazOS_KernelBoot
{
public partial class krnlboot : Form
{
public krnlboot()
{
InitializeComponent();
}
private void krnlboot_Load(object sender, EventArgs e)
{
Rectangle resolution = Screen.PrimaryScreen.Bounds;
krnlboot.Size = new Size(resolution.Width, resolution.Height);
}
}
}
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.
__________________

moaz786 is offline   Reply With Quote
Old 07-22-2012, 10:24 AM   #2
Solid State Member
 
Join Date: May 2012
Location: United States
Posts: 10
Smile [Solved]

Never mind. I used this.Size instead of krnlboot.Size, and it ran perfectly.
__________________

moaz786 is offline   Reply With Quote
Old 07-26-2012, 02:03 PM   #3
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: An object reference is required for the non-static field, method, or property

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.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 07:44 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0