1

I have the follwing VB.NET code I am trying to convert to C#.

Dim decryptedBytes(CInt(encryptedStream.Length - 1)) As Byte

I tried this:

int tempData = Convert.ToInt32(encryptedStream.Length - 1);
Byte decryptedBytes;
decryptedBytes = decryptedBytes[tempData];

But got this error message:

Cannot apply indexing with [] to an expression of type byte.

Please note that the VB.NET code works.

2 Answers 2

3

Using the SharpDevelop code converter, the output for your VB code is:

byte[] decryptedBytes = new byte[Convert.ToInt32(encryptedStream.Length - 1) + 1];

Note that VB specifies for upper bound of the array where C# specifies the length, so the converter added the "+ 1".

I would simplify that to:

byte[] decryptedBytes = new byte[(int)encryptedStream.Length];
Sign up to request clarification or add additional context in comments.

3 Comments

You can probably remove the (int) cast, too. I'll be very surprised if the Length property here isn't already an int.
Documentation disagrees with you: public int Length { get; } and Type: System.Int32 : msdn.microsoft.com/en-us/library/…
@R. Bemrose: System.IO.Stream, not String.
1

byte[] decryptedBytes = new byte[(Int32)encryptedStream.Length];

By the way if you have further problems try this:

http://www.developerfusion.com/tools/convert/vb-to-csharp/

2 Comments

DeveloperFusion is using an old version of the SharpDevelop code converter (NRefactory). Better use codeconverter.sharpdevelop.net/SnippetConverter.aspx. For example, the cast to int would have subtly different behavior than the original VB code if the Length property returned a double - this is why never versions of the converter use Convert.ToInt32().
+1 That is the link I was going to provide. There's another: c# to VB at developerfusion.com/tools

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.