-3

It's a 3D ray tracing program, and I'm having trouble making an addition to the SceneObject list before beginning the render.

I have my Mainwindow, which in it has a Scene class, which then contains the SceneObject list. In a different window I allow the user to add spheres to this list via a button click event.

In my event I try to do this:

private void addASphere(object sender, RoutedEventArgs e)
{
  #Some calculation to define sphere
  MainWindow.scene.AddObject(sphere);
}

The error from this is 'MainWindow.scene is inaccessible due to it's protection level'. I have no idea where to go with this, I've tried making Scene public, and static (which didn't work out). Any help would be massively appreciated.

If it's of any use, the MainWindow and Scene are set up like this:

public partial class MainWindow : Window
{
    Scene scene; 
    #All the Mainwindow code here
}


 class Scene
{
    public void AddObject(SceneObjects newObject)
    {
        if (newObject != null)
        { Objects.Add(newObject); }
    }
    #All the Scene code here
}
doggoz
  • 61
  • 5
  • Make `scene` public, then any code in the project can get the main window *and cast it to the actual MainWindow type* and get `scene` like so: `(App.Current.MainWindow as MainWindow).scene`. You should be doing this with MVVM, but first you need to learn the difference between a **class** and an **instance of a class**. They're different. You live in *a particular instance of a house*. So do I. If I shovel my front steps, does that remove snow from yours? No. Both are houses, but *not the same house*. – 15ee8f99-57ff-4f92-890c-b56153 Jan 08 '18 at 19:04
  • `MainWindow.scene` is like saying "front steps of a house". If you want to walk up an actual physical set of steps, you need to pick a specific house that really exists, go there, and walk up those actual steps. – 15ee8f99-57ff-4f92-890c-b56153 Jan 08 '18 at 19:08
  • See also https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method – Peter Duniho Jan 08 '18 at 22:38

1 Answers1

0

You need to make the scene member public:

public partial class MainWindow : Window {
    public Scene scene; 
}

However, it is usually recommended to use public properties instead of public members:

public partial class MainWindow : Window {
    public Scene scene { get; set; }; 
}

And finally, the usual naming C# convention is to use Pascal Case for properties:

public partial class MainWindow : Window {
    public Scene Scene { get; set; };
}
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55