Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

ReadBytes To Vb.net


terabin
 Share

Recommended Posts

I having problems…

Vb6 version

```

Public Function ReadBytes(ByVal nLength As Long, Optional MoveReadHead As Boolean = True) As Byte()

Dim Data() As Byte

If nLength = 0 Then Exit Function

If ReadHead + nLength - 1 > BufferSize Then Exit Function

ReDim Data(nLength - 1)

CopyMemory Data(0), Buffer(ReadHead), nLength

If MoveReadHead Then ReadHead = ReadHead + nLength

ReadBytes = Data

End Function

```

My Vb.net Version

```

Public Function ReadBytes(ByVal nLength As Integer, Optional ByVal MoveReadHead As Boolean = True) As Byte()

Dim Data() As Byte

If nLength = 0 Then Return Nothing

If ReadHead + nLength - 1 > BufferSize Then Return Nothing

ReDim Data(nLength - 1)

For i As Integer = 0 To ReadHead + nLength - 1

Data(i) = Buffer(i)

Next

If MoveReadHead Then ReadHead += nLength

Return Data

End Function
```

Error:

"Index was outside the bounds of the array"
Link to comment
Share on other sites

This is the wrong board. There is a programming board for this.

But here is a chunk of code that works.

```

Public Function ReadBytes(ByVal nLength As Long, Optional ByVal MoveReadHead As Boolean = True) As Byte()

Dim Data() As Byte

If nLength = 0 Then Return Nothing

If ReadHead + nLength - 1 > BufferSize Then Return Nothing

ReDim Data(nLength - 1)

CopyMemory(Data(0), Buffer(ReadHead), nLength)

If MoveReadHead Then ReadHead = ReadHead + nLength

ReadBytes = Data

End Function

```
Btw declare this```

Public Declare Auto Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Object, ByVal Source As Object, ByVal Length As Long)

```
Link to comment
Share on other sites

> Here [http://www.touchofde…et-byte-arrays/](http://www.touchofdeathforums.com/community/index.php?/topic/132165-vbnet-byte-arrays/)

I've added an open-source example and library that contains extension methods (all you'll ever need for Byte Arrays) to Xershade's topic. It's in C# 5, but you can use http://www.developerfusion.com/tools/convert/csharp-to-vb/ to convert C# to VB.NET.
Link to comment
Share on other sites

> I've added an open-source example and library that contains extension methods (all you'll ever need for Byte Arrays) to Xershade's topic. It's in C# 5, but you can use http://www.developerfusion.com/tools/convert/csharp-to-vb/ to convert C# to VB.NET.

Your code is not needed, the example I posted is already done in VB.Net, and why would he want the C# source if the doesn't know C# to begin with.
Link to comment
Share on other sites

> Your code is not needed, the example I posted is already done in VB.Net, and why would he want the C# source if the doesn't know C# to begin with.

It's going to remain there until someone removes it. Regardless of whether it is needed or not it gives individuals an option. You failed to produce the code for the library that shows them how to do what they wanted to do, so I covered for you. Don't get all pissy and throw a fit because you felt like I was stepping on your toes, and then still honestly don't have much of a clue as to what you're talking about with objects and constructors; I thought it was all pretty silly.

It's rude to bite the hand that's provided help in the absence of it. Learn something from what I provided.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...