简单的问题。在 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
这是我在 C# 中的尝试(没有返回类似的结果):
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);
最佳答案
在 VB6 中,字符串以 Unicode 格式存储。在内存中,VB6 字符串存储 4 个字节的开销,加上每个字符 2 个字节,所以你的语句 Dim rec As String * 200
实际上分配了 4 + 200 * 2
字节的内存,即 404 字节。由于 VB6 字符串和 C# 字符串都是 Unicode,因此您无需在此处进行任何更改。
VB6 中的Get
命令从文件中检索字节。格式为 Get [#]filenumber, [byte position], variableName
。这将检索 variableName
的字节数,从 byte position
的偏移量开始。在 VB6 中,字节位置从 1 开始。
现在,要翻译您的代码,它应该看起来类似于:
int pos = (rs.Fields["cJobnum"].Value).SubString(6, 4);
tempFile.Read(tempBuf, pos - 1, 200);
请注意 SubString
是从 0 开始的,而 Mid
是从 1 开始的,所以我使用了 6
而不是 7
。此外,Read
方法中的偏移量是从 0 开始的。 Get
在 VB6 中是从 1 开始的,所以我们减一。
关于c# - VB6 中的 get 语句在 C# 中的等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553221/