user3837675 user3837675 - 7 months ago 19
Vb.net Question

Method to return a string function in C#

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

Answer

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;