對於又一個愚蠢的問題,我很抱歉。我幾乎完成了我的設置保存winform,非常感謝StackOverflow的人,當然,但我堅持最後一件事。請不要僅僅因爲我是初學者而將其標記。我該如何解決這個問題?非靜態字段需要對象引用 - C#
我得到如下錯誤:
一個對象引用是所必需的非靜態字段,方法或屬性「ShovelShovel.WindowSize.Width.get」
的對象引用是所需的非靜態字段,方法或屬性 'ShovelShovel.WindowSize.Height.get'
這裏:
套裝tings.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();
}
}
都到:
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
那裏你可以找到我的附件中項目的全部文件,以防以上不足。
這個錯誤很自我解釋。爲什麼所有的代碼?只顯示導致錯誤的行。 –
可能的重複http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi –