VB6中的get语句的C#等效项是什么?

蓝细菌

简单的问题。在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,因此您无需在此处进行任何更改。

GetVB6中命令从文件中检索字节。格式为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且基于Mid1的,因此我使用6代替7同样,该Read方法中的偏移量是从0开始的。Get在VB6中是从1开始的,因此我们减去1。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章