Flexes
Flexes

Reputation: 107

C# Visual Studio - why do I get an error "Not all code paths return a value"?

This is my code:

using Microsoft.WindowsAPICodePack.Dialogs;
using System;

namespace andyify
{
    internal class CommonOpenFileDialog
    {
        internal readonly string FileName;

        public string IntialDirectory { get; internal set; }
        public bool IsFolderPicker { get; internal set; }

        internal CommonFileDialogResult ShowDialog()
        {
        }
    }
}

And I am getting this error:

Error CS0161
'CommonOpenFileDialog.ShowDialog()': not all code paths return a value.

Does anyone know why this is? Can someone please help me? Thank you

Upvotes: 0

Views: 394

Answers (4)

TheGeneral
TheGeneral

Reputation: 81493

You are receiving the following error Compiler Error CS0161

not all code paths return a value

A method that returns a value must have a return statement in all code paths. For more information, see Methods.


Further more

Methods (Return values)

Methods with a non-void return type are required to use the return keyword to return a value.


As noted by Eric J. and to add to the documentation (which does seem to be lacking),

Methods with a non-void return type are required to use the return keyword to return a value or throw an exception

Your method has a return

internal CommonFileDialogResult ShowDialog()
{
        
}

Either set it to void,

internal void ShowDialog()
{
        
}

or return a value

internal CommonFileDialogResult ShowDialog()
{
    return null // just to get it to compile
}

If this is a required to be implemented by an interface, throw an exception

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

This method:

internal CommonFileDialogResult ShowDialog()
{
        
}

doesn't return anything. You must either return a CommonFileDialogResult or throw an exception, such as

internal CommonFileDialogResult ShowDialog()
{
    throw new NotImplementedException("Need to implement this.");
}

Upvotes: 2

wesreitz
wesreitz

Reputation: 77

  internal CommonFileDialogResult ShowDialog()
    {
        
    }

CommonFileDialogResult indicates that you expect th method to return an object of type CommonFileDialogResul. solutions:

  1. throw new NotImplementedException("Need to implement this."); as shown by Eic J.
  2. return null
  3. return new CommonFileDialogResult()
  4. change method signature from CommonFileDialogResult to void.

Upvotes: 1

raven404
raven404

Reputation: 1197

this is because your code expects a return value which isn't there in the last function

Upvotes: 0

Related Questions