0

I am a little programmer in C # , VB.NET do however have to code a lot of time . Now I was in doubt if the code was doing well . I need your help to create a function that makes the return of a string , already tested the following code , but the compiler gives an error :

public class Main
{
    private System.Text.UTF8Encoding enc;
    private ICryptoTransform encryptor;
    private ICryptoTransform decryptor;

    public string utf16_encrypt(string input)
    {
        string sPlainText = input;
        string output;
        if (!string.IsNullOrEmpty(sPlainText))
        {
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, this.encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(enc.GetBytes(sPlainText), 0, sPlainText.Length);
            cryptoStream.FlushFinalBlock();
            output = Convert.ToBase64String(memoryStream.ToArray());
            memoryStream.Close();
            cryptoStream.Close();
            return output;
        }
    }
}

The error given by the compiler is :

http://i.imgur.com/cJeNran.png

1
  • Use a using statement where you make a new MemmoryStream and a new CryptoStream. Not related to your question, but strongly encouraged. Commented Jun 12, 2015 at 12:42

7 Answers 7

2

User, your method will not return anything if sPlainText is null. You have to keep this in mind and make sure all execution paths return code. You could change the code to be like this:

    if (!string.IsNullOrEmpty(sPlainText))
                {
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, this.encryptor, CryptoStreamMode.Write);
                    cryptoStream.Write(enc.GetBytes(sPlainText), 0, sPlainText.Length);
                    cryptoStream.FlushFinalBlock();
                    output = Convert.ToBase64String(memoryStream.ToArray());
                    memoryStream.Close();
                    cryptoStream.Close();
                    return output;
                }

    return "Invalid Input"; //Or whatever message you want to pass back to the user/code. 
Sign up to request clarification or add additional context in comments.

Comments

1

Put this : return output; out of the } brace like this :

if (!string.IsNullOrEmpty(sPlainText))
            {
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, this.encryptor, CryptoStreamMode.Write);
                cryptoStream.Write(enc.GetBytes(sPlainText), 0, sPlainText.Length);
                cryptoStream.FlushFinalBlock();
                output = Convert.ToBase64String(memoryStream.ToArray());
                memoryStream.Close();
                cryptoStream.Close();

            }
  return output;

5 Comments

New error given by compiler: Error 1 Use of unassigned local variable 'output' C:\Users\luisferreira\Organizados\Workspace\Projects\totalLibrary\totalLibrary\totalLibrary\main.cs 31 20 totalLibrary
@user3837675 assign that variable with default value of null or string.Empty.
Put string output = ""; in your declaration.
Thank you, the compiler no longer returns any error .
Not a problem. You can do it as per your convinience. :)
1

You should think what your function should do if it doesn't enter the if statement.

You need a default return statement. Right now if the condition is not true you return nothing. Instead write at the end something like :

if (!string.IsNullOrEmpty(sPlainText))
            {
                ...
                return output;
            }
  return String.Empty;

Comments

0

The really short version of your code is

    public string utf16_encrypt(string input)
    {
        string output;
        if (somecondition)
        {
           return output;
        }
    }

The compiler knows what to do if some condition is true, but if it's false, it has no idea. It's looking for something like

    public string utf16_encrypt(string input)
    {
        string output;
        if (somecondition)
        {
           return output;
        }
        else {
           return "banana"; //when in doubt, return bananas
        }
    }

Comments

0

The compiler is telling you that every logical path through the code must return a value. Since the method declares a return type:

public string utf16_encrypt(string input)

it must return something of that type (string). So the question becomes...

What happens if sPlainText is null or empty?

If it isn't null or empty, the code returns something:

return output;

But if it isn't null or empty, then that if block is never executed and there is no return statement. So you need to add one:

if (!string.IsNullOrEmpty(sPlainText))
{
    // your current code
}
return output;

You don't have to return output specifically, I just guessed on what you might want to return. You may return a default value instead, something like:

return string.Empty;

It's up to you. The point is that logically the method must always return something (assuming it doesn't terminate in an exception condition instead).

Comments

0

Not all code parths return a value is meaningful, isn't it?

You have to ensure that a value is returned from the method in any case. You return only from inside the if, so nothing is returned if sPlainText==null. In that case you could return null:

public string utf16_encrypt(string input)
{
    string sPlainText = input;
    string output;
    if (!string.IsNullOrEmpty(sPlainText))
    {
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, this.encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(enc.GetBytes(sPlainText), 0, sPlainText.Length);
        cryptoStream.FlushFinalBlock();
        output = Convert.ToBase64String(memoryStream.ToArray());
        memoryStream.Close();
        cryptoStream.Close();
        return output;
    }
    else
        return null;
}

by the way, you also get a compiler warning in VB.NET if you comment out Return Nothing

Public Function utf16_encrypt(input As String) As String
    Dim sPlainText As String = input
    Dim output As String
    If Not String.IsNullOrEmpty(sPlainText) Then
        ' ... '
        Return output
    Else
        Return Nothing
    End If
End Function

Comments

0

better to do like this, looks a lot simpler and easier

 public string utf16_encrypt(string input)
    {

        if (!string.IsNullOrEmpty(input)) return null;
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, this.encryptor, CryptoStreamMode.Write))
        {
            cryptoStream.Write(enc.GetBytes(input), 0, input.Length);
            cryptoStream.FlushFinalBlock();
            cryptoStream.FlushFinalBlock();
            return Convert.ToBase64String(memoryStream.ToArray());
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.