Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:47:34+00:00 2026-06-16T00:47:34+00:00

I have a few dialogs in my class and I’m trying to initialize them

  • 0

I have a few dialogs in my class and I’m trying to initialize them with a function:

    private void InitializeFileDialog(ref FileDialog fileDialog)
    {
        fileDialog.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" +
         "|Office Files|*.doc;*.xls;*.ppt" +
         "|All Files|*.*";
        fileDialog.DefaultExt = "txt";
    }

The problem is when I call it:

    InitializeFileDialog(ref dialog);

error CS1503: Argument 1: cannot convert from 'ref Microsoft.Win32.OpenFileDialog' to 'ref Microsoft.Win32.FileDialog'

I tried to cast, but it couldn’t for some reason. What’s the problem? Is it because FileDialog is abstract? I tried to look up if that’s the reason but I couldn’t find anything useful.

Here are the declarations that are found in Microsoft.Win32:

public abstract class FileDialog : CommonDialog
public sealed class OpenFileDialog : FileDialog

I also tried to use generics and it didn’t work. What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:47:36+00:00Added an answer on June 16, 2026 at 12:47 am

    The best solution is to remove the ref keyword. It is really not needed in this case.

    You only need ref, if your method should be able to reassign the variable that you passed in, which should be almost never. Returning a value, or working with the object directly as you are, is usually enough.

    The compiler error however, comes from the fact that you need to pass in a variable of the exact type required by the method. Like this:

    FileDialog baseDialog = fileDialog;
    // baseDialog might be a different object when this returns
    InitializeFileDialog(ref baseDialog); 
    

    After all, the original variable that is passed as ref parameter could be reassigned by your method.

    Now what would happen if it would assign a SaveFileDialog to your original variable of type OpenFileDialog? The world as we know it would end. That’s why you need to create a temporary variable of type FileDialog. The type system allows your method to assign an object of any derived class to that variable.

    See this blog entry by Eric Lippert for more info on this interesting topic:
    Why do ref and out parameters not allow type variation?

    Anyway: Don’t use ref in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have generated few elements in my page using following method. For example, $(#button+caption).click(function(){
I have run into a few problems when trying to get the camera to
I have a few questions regarding WPF commands. Where should I put confirmation dialogs?
I have a list a simple dialog box which contains a few checkboxes, I
I have few questions about ssis transction isolation levels. consider a scenario:I have an
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
I have few basic questions on session management in GWTP. Now, we create a
I have few controls (Label, Custom Textbox, Datagridview) docked on a form. When I
I have few basic questions on session management in GWTP. Client : GWTP, Server
I have few text fields like SSN , phone number and email id. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.