0

I have a MainWindow and a DetailedBookView . i want use Method and field from DetailedBookView in MainWindow class whit out using new object... MainWindows isn't parent of DetailedBook ... please help me to write code in MainWindow to use DataContaxt in MainWindow.

public partial class MainWindow : Window
{
    //use DataContaxt method here without using new object
    enter code here

}


public partial class DetailedBookView : UserControl
{
    int DataContaxt = 10;
}
  • buddy have you ever coded in C#, C++ before? firstly in order to access the instance level variable/method/property outside the class it must be public and instance has to be created. If you dont want it to be instance level, make it public static. if you know it wont change ever, make it const – Nitin Jun 10 '14 at 09:06

2 Answers2

0

Maybe just set DataContext field static

ivamax9
  • 2,601
  • 24
  • 33
0

Make Method and fields which you want to use in another class of DetailedBookView static. Doing so, you can use these methods and fields with the class name directly.

public partial class DetailedBookView : UserControl
{
    static int DataContaxt = 10;
}

Now you can use DataContaxt directly as:

public partial class MainWindow : Window
{    
    int a = DetailedBookView.DataContaxt;

}
Ricky
  • 2,323
  • 6
  • 22
  • 22
  • `MainWindow class whit out using new object...` According to your requirement it is the best suitable method. How can you call non static class variable/methods without its object? There are other methods also but you need to instantiate the class for this, like using `Singleton` design pattern in which you have only one instance of a class. – Ricky Jun 10 '14 at 09:45