-3

I'm new to C#, and I'm new to the idea of "partial" classes.

I wish to access the "grid" variable outside of this "MainWindow" class. How would I go about doing that?

enter image description here

MrDysprosium
  • 489
  • 9
  • 20
  • outside where ? Another class ? How are they connected ? If you are calling a method of that class, why not pass it as a parameter ? If not a Static class which has this property ? – Shyju Aug 17 '16 at 16:53
  • @Shyju I have anotherclass, "Server", and it receives data that I wish to add to grid.ItemSource. When I change my public partial class MainWindow : Window to public static partial class MainWindow : Window, I get the following error: Static class "MainWindow" cannot derive from type 'Window'. Static classes must derive from object. – MrDysprosium Aug 17 '16 at 16:55
  • You cannot do it, and it has nothing to do with the class being partial. Variable `grid` is a local variable inside a method of your class. – Sergey Kalinichenko Aug 17 '16 at 16:56
  • @dasblinkenlight Ok, that makes sense. But creating "grid" requires the "sender" which comes from the dataGrid_Loaded arguments... I can't think of a work around... – MrDysprosium Aug 17 '16 at 16:58
  • 2
    Please *do not* post code as image. Also make sure to narrow down your question to one problem at a time. It is a good idea to understand if your problem actually related to all conditions you have - i.e. for this question it is unclear why you can't apply your understanding how to access properties of non-partial classes to this case. – Alexei Levenkov Aug 17 '16 at 16:59
  • Could you check if you have the `MainWindow.Designer.cs` file? – Sergey Kalinichenko Aug 17 '16 at 17:04

3 Answers3

3

Partial means that your class is split among different files, it has nothing to do with the exposure of variables to other classes.

Your grid is a local variable in your current method, so it's not accessible by others. If you want to make it accessible, define it as a property instead.

public DataGrid Grid { get; set; }
Norbert Huurnink
  • 1,326
  • 10
  • 18
  • Awesome, that's what I needed. Now I have a new issue! When I try to access a property of grid (grid.Items.Refresh()), I receive the following error. System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it. – MrDysprosium Aug 17 '16 at 17:10
1

Even though it is technically possible, you should not make your data grid accessible outside the class. The grid is part of the view managed by your class, so making the grid accessible to other classes breaks encapsulation by making implementation details of your form visible.

I have another class, Server, and it receives data that I wish to add to grid.ItemSource.

Then your Server class should provide a data source to which your form should bind the grid. In other words, the access should go in the other direction.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Oh! That is interesting.... I'm trying to think of how to implement this... EDIT: Ok, just kidding, that is what I'm doing. I just wasn't thinking of it the right way. – MrDysprosium Aug 17 '16 at 17:14
0

You need to declare the variable as a public member of the class like this

public partial class MainWindow ...
{
   public DataGrid grid;

   public MainWindow()
   {
      ...
   }

   public void DataGrid_Loaded(...)
   {
     ...
     grid = sender as DataGrid;
     ...
   }
}

Now you can access to the variable in this way

var x = MainWindow.grid;
vcRobe
  • 1,671
  • 3
  • 17
  • 35