I am having a hell of a time getting a variable from the function of another class usable in my Game1 (main) class. Specifically, I want to take width and height from the function SaveData in SetWindowSize.cs and use it in ReadSettings in Game1.cs.
I get the error
‘ShovelShovel.SetWindowSize’ does not contain a definition for
‘height’. Same for ‘width’.
Game1.cs (the function only)
protected void ReadSettings()
{
try
{
if (File.Exists(SetWindowSize.savePath))
{
using (FileStream fileStream = new FileStream(SetWindowSize.savePath,
FileMode.Open))
{
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
SetWindowSize.width = binaryReader.ReadInt32();
SetWindowSize.height = binaryReader.ReadInt32();
}
}
}
}
catch
{
}
}
SetWindowSize.cs
namespace ShovelShovel
{
protected void ReadSettings()
{
try
{
if (File.Exists(savePath))
{
using (FileStream fileStream = new FileStream(savePath, FileMode.Open))
{
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
var windowSize = WindowSizeStorage.ReadSettings();
WindowSize.Width = windowSize.Width;
WindowSize.Height = windowSize.Height;
}
}
}
}
catch
{
}
}
Thank you so much to anyone and everyone that can help me, I really appreciate it.
This might do the trick:
Usage:
Please note that writing an answer like this (presenting all code) is not the common way; I just felt like it. I tried to show some object-oriented design principles, where each class does its own thing.
If you want to transfer compicated objects between methods (i.e. more than one primitive type), you will usually create a Data Transfer Object (DTO) like
WindowSize.The
WindowSizeStorageclass has the sole responsibility to store and retreive such aWindowSizeobject. From your code you simply tell the storage to store or retreive the settings you wish.But as I get from your question and comments, you haven’t got much experience using C# or perhaps any programming experience at all. Try to pick up on a tutorial or two so you can understand how to put your thoughts into code.