Simple question. What would an equally functioning conversion look like in C#?
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# (doesn't 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);
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.
C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.
In VB6, strings are stored as Unicode. In memory, VB6 strings store 4 bytes of overhead, plus 2 bytes per character, so your statement Dim rec As String * 200
actually allocates 4 + 200 * 2
bytes of memory, which is 404 bytes. Since VB6 strings and C# strings are both Unicode, you don't need to change anything here.
The Get
command in VB6 retrieves bytes from a file. The format is Get [#]filenumber, [byte position], variableName
. This will retrieve however many bytes variableName
is, starting at an offset of byte position
. The byte position is 1-based in VB6.
So now, to translate your code, it should look similar to this:
int pos = (rs.Fields["cJobnum"].Value).SubString(6, 4);
tempFile.Read(tempBuf, pos - 1, 200);
Notice that SubString
is 0-based and Mid
is 1-based, so I used 6
instead of 7
. Also, the offset in the Read
method is 0-based. Get
is 1-based in VB6, so we subtract one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With