What is the C # equivalent of get expression in VB6?

Simple question. What would an equally functioning conversion in C # look like?

VB6:

Dim rec As String * 200 If rs!cJobNum <> "" Then Open PathFintest & Mid(rs!cJobNum, 2, 5) & ".dat" For Random As #1 Len = 200 s = Val(Mid(rs!cJobNum, 7, 4)) Get #1, Val(Mid(rs!cJobNum, 7, 4)) + 1, rec Close #1 TestRec = rec Fail = FindFailure(TestRec) End If 

This was my attempt in C # (does not return similar results):

 FileStream tempFile = File.OpenRead(tempPath); var tempBuf = new byte[200]; var tempOffset = Int32.Parse(StringHelper.Mid(rs.Fields["cJobnum"].Value, 7, 4)) + 1; tempFile.Seek(tempOffset , SeekOrigin.Begin); tempFile.Read(tempBuf, 0, 200); rec.Value = new string(System.Text.Encoding.Default.GetChars(tempBuf)); tempFile.Close(); TestRec = rec.Value; Fail = (string)FindFailure(ref TestRec); 
+6
source share
1 answer

In VB6, strings are saved as Unicode. In memory, VB6 strings store 4 bytes of overhead plus 2 bytes per character, so your Dim rec As String * 200 statement actually allocates 4 + 200 * 2 bytes of memory, which is 404 bytes. Since VB6 strings and C # strings are Unicode, nothing needs to be changed here.

The Get command in VB6 extracts bytes from a file. Format Get [#]filenumber, [byte position], variableName . This will retrieve, however, how many bytes of variableName are there, starting at the offset byte position . Byte 1 position is based on VB6.

So now, to translate the code, it should look something like this:

 int pos = (rs.Fields["cJobnum"].Value).SubString(6, 4); tempFile.Read(tempBuf, pos - 1, 200); 

Note that SubString based on 0 and Mid is based on 1, so instead of 7 I used 6 . Also, the offset in the Read method is based on 0. Get based on 1 in VB6, so we subtract it.

+5
source

Source: https://habr.com/ru/post/979874/


All Articles