views:

390

answers:

8

Any help would be appreciated, I'm trying to convert the code below to C#, I've never used VB.NET so ReDim is a new one to me.

Thanks

Dim inFile As System.IO.FileStream
Dim binaryData() As Byte
Dim strFileName As String

strFileName = "C:\MyPicture.jpeg"

inFile = New System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)

''//Retrive Data into a byte array variable
ReDim binaryData(inFile.Length)
Dim bytesRead As Long = inFile.Read(binaryData, 0, CInt(inFile.Length))
inFile.Close()
+10  A: 

The code could be converted verbatim, but there's a much easier way to achieve what this is doing (read all bytes from a file), i.e.

var binaryData = File.ReadAllBytes(strFileName);

Personally I'd rename strFileName to just fileName as Hungarian notation is frowned upon in .NET code... but that's another matter!

Greg Beech
+1 for the right answer.
Brian
Note: In older versions of .Net, replace "var" with "byte[]."
Brian
@Brain: No, in the pre-C#3.0 compiler replace var with byte[].
Samuel
@Brian - you mean older versions of C#; you can still use "var" with .NET 2
Marc Gravell
@Brian - yep, cheers for pointing that out. I'm so used to C# 3.0 that I forget you can't use var in older versions!
Greg Beech
Excellent example of a bad use of var.
Stephen Martin
@Stephen: How so? Even without Visual Studio I can tell that this function returns an array of bytes. What else could ***AllBytes return?
Samuel
This use of var gains nothing at the cost of clarity. ReadAllBytes may *most likely* return an array of bytes but it could easily return an IEnumerable<byte> or something else entirely. This gratuitous obfuscation has become popular with the "cool kids" but it is a mark of sloppy programming.
Stephen Martin
@Stephen - That's your opinion and I think you're wrong, I'm afraid. It doesn't matter whether this is an array or a sequence, or even that it is bytes when it comes down to it. All that matters is that it is the data in the file.
Greg Beech
Plus, if you're going to insult me by calling me a "sloppy programmer" then I'd love to see your justification. My justification for the use of var is here: http://gregbeech.com/blogs/tech/archive/2008/03/24/to-var-or-not-to-var-implicit-typing-is-the-question.aspx
Greg Beech
A: 

ReDim is used is resize arrays. (You can preserve the content if desired also. This code doesn't do that)

Booji Boy
+2  A: 

I believe that the ReDim statement is just used to initialize the array:

byte[] binaryData;

binaryData = new byte[inFile.Lenght];
Frederik Gheysels
+1  A: 

ReDim re-allocates the array. Most of the time it's a code smell: a symptom or really wanting a collection type rather than an array. This code should do what you want:

string FileName = @"C:\MyPicture.jpeg";
byte[] binaryData;
long bytesRead;

using (var inFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read) )
{
    binaryData = new byte[inFile.Length];
    bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
}
//I'm assuming you're actually doing something with each byte array here
Joel Coehoorn
@Joel - even this is a bit of a smell; it isn't safe to assume that a stream will read as much data as was requested; even for simple cases, a loop (checking the return from Read) is *technically* necessary. Most streams (including FileStream) are forgiving about this - but not all.
Marc Gravell
Yeah: fixed it so that bytesRead will still be in scope after the using statement.
Joel Coehoorn
+2  A: 

Well, the closest translation would be:

binaryData = new byte[inFile.Length];

since it hasn't been assigned, or:

Array.Resize(ref binaryData,inFile.Length);

if it had been previously assigned. However, the code itself is very unsafe (you shouldn't assume Read reads all the requested data); a much simpler approach here is:

binaryData = File.ReadAllBytes(strFileName);
Marc Gravell
+2  A: 

This very easy to convert to C#.

FileStream inFile;
byte[] binaryData;
string strFileName;

strFileName = @"C:\MyPicture.jpeg";

inFile = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

binaryData = new byte[inFile.Length];
int bytesRead = inFile.Read(binaryData, 0, binaryData.Length);
inFile.Close();

But there is a much better way to write this.

string fileName = @"C:\MyPicture.jpeg";
byte[] binaryData = File.ReadAllBytes(fileName);
Samuel
A: 

Here's a permanant solution in case you need to do this again in future sometime.

Here are links for online VB to C# code converters and vice-versa. One is here and another is here.

LinkText1: http://www.developerfusion.com/tools/convert/vb-to-csharp/ LinkText2: http://converter.telerik.com/

this. __curious_geek
+2  A: 

If you're converting a lot of VB.NET to C#, you might want to check out VBConversions conversion tool.

Richard Ev