tags:

views:

82

answers:

2

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.

+3  A: 

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];
Daniel
You can probably remove the `(int)` cast, too. I'll be very surprised if the Length property here isn't already an int.
Joel Coehoorn
Stream.Length returns long
Daniel
@Daniel: Documentation disagrees with you: `public int Length { get; }` and `Type: System.Int32` : http://msdn.microsoft.com/en-us/library/system.string.length%28v=VS.100%29.aspx
R. Bemrose
@R. Bemrose: System.IO.Stream, not String.
Daniel
+1  A: 

Hey there try this

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/

MUG4N
DeveloperFusion is using an old version of the SharpDevelop code converter (NRefactory). Better use http://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().
Daniel
it worked! thanks.
Brono The Vibrator
thx for correction
MUG4N
+1 That is the link I was going to provide. There's another: c# to VB at http://www.developerfusion.com/tools
Will Marcouiller