سؤال

I'm learning C and C#, the question is for C#, I'm reading this programming book and this code is not compiling.

enter image description here

FileDialog is only showing two methods in intellisense(Equals and ReferenceEquals). The code is from a book so it is expected that this method and property are part of the FileDialog class right?

Here is the code:

    private void cmdBrowse_Click(object sender, EventArgs e   
    {
        if (FileDialog.ShowDialog() != DialogResult.Cancel)
        {
            txtLocation.Text = FileDialog.FileName;
            cmdWatch.Enabled = true;
            FileDialog.
        }
    }

EDIT: I found out what the problem was, I hadn't added a filedialog to the form, I didn't know what a filedialog was, now I remember. Dohh

هل كانت مفيدة؟

المحلول

The problem is you want an instance of a class, rather than using the class directly. However, FileDialog is an abstract class which means you cannot instantiate it directly, but there are a couple of implementations that you can use...

Assuming you want to select (open) a file, then you can use OpenFileDialog class:

OpenFileDialog dialog = new OpenFileDialog();
if(dialog.ShowDialog() != DialogResult.Cancel)
{
    txtLocation.Text = dialog.FileName;
    cmdWatch.Enabled = true;
}

Alternatively, if you want to choose a file location for saving then use SaveFileDialog

نصائح أخرى

FileDialog is an abstract class. You have to use one of its implementations. Either SafeFileDialog or OpenFileDialog depending on your needs.

More information about the FileDialog class is available on MSDN.

FileDialog is the class, but you need an instance of it to open it. So presuming you are using a OpenFileDialog and it's name is OpenFileDialog1:

if(OpenFileDialog1.ShowDialog() != DialogResult.Cancel)
{
    // ...
}

You normally create an instance by calling the constructor of a class, f.e:

OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

You can only call methods via classname that are static.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top