0

A basic question: I am writing a C# app which has a menu, each menu item is going to open a new window. The windows will all be instances of the Window class.

instead of writing: (EditSettings being the desired name of the target window)

Window EditSettings = new Window();
EditSettings.Show();

for each one, could I not write a method something like the below, to create the instance?

private void OpenSelectedWindow(Window n)
{
    n = new Window();
    n.Show();
}

I can't call the method though - I tried:

OpenSelectedWindow(EditSettings);

Which doesn't work ("The name EditSettings does not exist in the current context'), OR

OpenSelectedWindow(Window EditSettings);

which doesn't work either

I am so rusty with C# and feel like a twit for asking this, but I can't seem to find examples of this on the internet. Can you create an instance of a class using a method?? What am I missing? Thank you.

Edit

The code all happens in the main Namespace:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    // Attempting to write a method that instances the class
    private void OpenSelectedWindow(Window n)
    {
        n = new Window();
        n.Show();

    }

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        //Calling the method
        OpenSelectedWindow(EditSettings);
    }
}

I have two .xaml files - EditSettings.xaml (empty) and MainWindow.xaml.

7
  • It's not clear where your method is declared or where the code trying to use it is. Also, why take a parameter if you're going to ignore its value? You can absolutely create an instance of a class within a method, but I suspect that's not really what you're interested in. Unfortunately it's hard to see what you're trying to do with your question in its current form. Commented Mar 31, 2014 at 8:32
  • Sorry I didn't realise you needed more code, I will edit and add more. Commented Mar 31, 2014 at 8:34
  • Well it's more clarity than anything else. For example, what do you mean exactly by "EditSettings being the desired name of the target window"? Are you actually talking about creating instances of different types derived from Window, and you've got a class called EditSettings? Commented Mar 31, 2014 at 8:37
  • Basically, if I took the OpenSelectedWindow method, and passed EditSettings as the parameter, wherever it has "n" in the method, I would replace that with "EditSettings" (like a string) and, if all worked, it would create a new window called EditSettings. Commented Mar 31, 2014 at 8:47
  • An object isn't "called" anything. Variables have names, but it's not clear whether that's what you're after either... the variable name within the method is irrelevant elsewhere. If you're interesting in setting the Name property of a window, then just set that property via a string. Commented Mar 31, 2014 at 8:50

1 Answer 1

1

You can use generics for this one:

private T OpenSelectedWindow<T>() where T : Window, new()
{
    T n = new T();
    n.Show();
    return n;
}

Use the method like this:

EditSettings editSettingsWindow = OpenSelectedWindow<EditSettings>();
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect this may be what the OP is after, yes. The question still seems fairly unclear to me, but...
Agreed, but the question looks so much like an attempt to do exactly that without the OP knowing about generics and/or Reflection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.