user2404595
user2404595

Reputation:

Not All Code Path Return Value in Func<> Generic Type Delegate

I am new Generic Type delegates and try implement Func<> generic type delegate

I am developing winform application in which I am try to save the file my save function returns me bool (True/False). i know its very simple to implement without the generic delegate but I want to implement into Generic Delegate my code

public bool Save( string fileName, SaveFlags options)
{
    if (fileName == null) throw new ArgumentNullException("file");
    using (FileStream fs = File.Create(fileName))
    {
        Func<string, SaveFlags, bool> func2 = (fileStream, opt) => Save(fs , options);
       **// what should I need to return**  
    }
}

private bool Save(Stream iStream, SaveFlags options)
{
   **//Some operation perform and return TRUE or FALSE**

}

I know whatever the last out parameter of Func<> its become the return type of Func<> i.e. it return by Func<>.

so how I can handle the Error "Not All Code Path Return Value"

Upvotes: 0

Views: 354

Answers (4)

IMMORTAL
IMMORTAL

Reputation: 2857

Check this solution

 public bool Save(string fileName, SaveFlags options)
        {
            if (fileName == null) throw new ArgumentNullException("file");
            using (FileStream fs = File.Create(fileName))
            {
                Func<string, SaveFlags, bool> func2 = (fileStream, opt) => Save(fs, options);
                return func2(fileName, options);
            }
        }

Upvotes: 0

Ari
Ari

Reputation: 3127

You should execute your func2 with your parameters and returns its result. You also need to repair you delegate.

Func<Stream, SaveFlags, bool> func2 = (fileStream, opt) => Save(fileStream, opt);
return func2(filename, options);

If you won't repair delegate, you would create func2 which takes two args and ignores them.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283763

First up, you are looking at infinite recursion. From your lambda parameter names, you intended to attach to the overload accepting a Stream. But Func<string, SaveFlags, bool> means you're going to get the overload accepting a string instead.

Next, if your delegate takes parameters, you don't need to capture the parameters of the current function call. So the lambda isn't helpful at all.

Taking that into account:

public bool Save( string fileName, SaveFlags options)
{
    if (fileName == null) throw new ArgumentNullException("file");
    Func<Stream, SaveFlags, bool> func2 = Save;
    using (FileStream fs = File.Create(fileName))
    {
        return func2(fs, options);
    }
}

Upvotes: 4

gzaxx
gzaxx

Reputation: 17600

Invoke your func.

public bool Save( string fileName, SaveFlags options)
{
    if (fileName == null) throw new ArgumentNullException("file");
    using (FileStream fs = File.Create(fileName))
    {
        Func<string, SaveFlags, bool> func2 = (fileStream, opt) => Save(fs , options);
        return func2(fileName, options);
    }
}

Upvotes: 1

Related Questions