0

I'm sorry for yet another stupid question. I am almost finished with my settings saving winform, with many thanks to the people of StackOverflow of course, but I am stuck on one final thing. Please don't mark this down just because I am a beginner.

I am getting the following errors:

An object reference is required for the non-static field, method, or property 'ShovelShovel.WindowSize.Width.get'

An object reference is required for the non-static field, method, or property 'ShovelShovel.WindowSize.Height.get'

Here:

Settings.cs

public partial class Settings : Form
{
    public Settings()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        var windowSize = new WindowSize { Width = WindowSize.Width, Height = WindowSize.Height };

        WindowSizeStorage.WriteSettings(windowSize);

        Application.Exit();
    }
}

Which goes to:

WindowSize.cs

public class WindowSize
{
    public int Width { get; set; }
    public int Height { get; set; }
}

public static class WindowSizeStorage
{
    public static string savePath = "WindowSize.dat";

    public static WindowSize ReadSettings()
    {
        var result = new WindowSize();
        using (FileStream fileStream = new FileStream(savePath, FileMode.Open))
        {
            using (BinaryReader binaryReader = new BinaryReader(fileStream))
            {
                result.Width = binaryReader.ReadInt32();
                result.Height = binaryReader.ReadInt32();
            }
        }

        return result;
    }

    public static void WriteSettings(WindowSize toSave)
    {
        using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(savePath, FileMode.Create)))
        {
            binaryWriter.Write(toSave.Width);
            binaryWriter.Write(toSave.Height);
        }
    }
}

http://forums.codeguru.com/showthread.php?530631-I-m-having-trouble-with-my-code

There you can find the full files of my project in the attachments, in case the above is insufficient.

Community
  • 1
  • 1
Fiona
  • 27
  • 1
  • 6

1 Answers1

3

Perhaps you meant:

var windowSize = new WindowSize { Width = this.Width, Height = this.Height };

instead of:

var windowSize = new WindowSize { Width = WindowSize.Width, Height = WindowSize.Height };

As written, it would require Width and Height to be static properties of the WindowSize class, but I don't think that's what you intended. Instead, it makes more sense to use the form instance Width and Height properties.

jglouie
  • 12,523
  • 6
  • 48
  • 65
  • Thank you very much, that fixed it. I will mark this as the answer when it will let me. – Fiona Dec 11 '12 at 04:39