Вопрос

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Program
{

    class Program
    {

        public static string *GetExtension*(string cale) //GetExtension is an error
        {
            string fisier, extensie;
            cale = @"D:\dir1\dir2\";
            fisier = @"D:\dir1\dir2\fisier.txt";        

            extensie = Path.GetExtension(fisier);

            System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);

            extensie = Path.GetExtension(cale);

            System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);
        }
    }
}

I apologize for this amateur post.

I don't understand the error "Program.Program.GetExtension(string)': not all code paths return a value"

Can anyone tell me what have I done wrong, please?

(oh and fisier means file, cale mean path and extensie mean extension in my language).
Thanks in Advance.

Это было полезно?

Решение

The function is supposed to return a string but you have no return statement

add

return extensie;

before the first }

alternatively if you aren't wanting a return value and are just wanting the console.writelines simply change public static string to be public static void and then you won't need a return value

Другие советы

Your function is expected to return a string. You are not returning anything in the entire function.

Somewhere in the function you should have a statement like:

return extensie;

By defining your method like

public static string GetExtension(string cale) //GetExtension is an error

You say that your method will return a string.

Your method, doesn't return anything by just outputs something to the screen. So alter it like this:

public static void GetExtension(string cale) //GetExtension is an error

or, if you do want to return something, make sure you put this line:

return extensie;

at the end of your method (before the closing brace)

I would suggest looking into the 'Return Values' section on this page :)

you must add return to your function

public static string *GetExtension*(string cale) //GetExtension is an error
{

    string fisier, extensie;

    cale = @"D:\dir1\dir2\";

    fisier = @"D:\dir1\dir2\fisier.txt";


    extensie = Path.GetExtension(fisier);

    System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);


    extensie = Path.GetExtension(cale);

    System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);

    return extensie; //<----------------
}

Yes, you need to return a string from your function, e.g.

return extensie;

I'm guessing that your language is french, so if you like, I can give you a translation but to answer your question in english:

The best way is to replace public static String with public static void

Also, I don't see why you have stars around GetExtension

giving:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Program {
    class Program
    {
        public static void GetExtension(string cale) //GetExtension is an error
        {
        string fisier, extensie;
        cale = @"D:\dir1\dir2\";
        fisier = @"D:\dir1\dir2\fisier.txt";

        extensie = Path.GetExtension(fisier);
        System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);
        extensie = Path.GetExtension(cale);
        System.Console.WriteLine("Extensie: {0} returneaza {1}", fisier, extensie);
        }
    }
}

Your method is marked as "public static string", which means that you need a "return string;" statement. If you do not want the method to return a value then you can replace the method definition with "public static void" which means "I'm not going to return anything"

On a side note: You can use the FileInfo class to get the extension of the file in question. Its less code and has been done for you :)

For example:

        FileInfo fInfo = new FileInfo(path);
        string extension = fInfo.Extension;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top