VB6中的get语句的C#等效项是什么? [英] What is the C# equivalent of a get statement in VB6?
问题描述
一个简单的问题.在C#中,功能相同的转换是什么样的?
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
这是我在C#中的尝试(不会返回类似结果):
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);
推荐答案
在VB6中,字符串存储为Unicode.在内存中,VB6字符串存储4个字节的开销,每个字符加上2个字节,因此您的语句 Dim rec As String * 200
实际上分配了 4 + 200 * 2
个字节的内存,即404个字节.由于VB6字符串和C#字符串都是Unicode,因此您无需在此处进行任何更改.
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.
VB6中的 Get
命令从文件中检索字节.格式为获取[#]文件编号,[字节位置],variableName
.无论从 byte position
的偏移量开始,这将检索多少个 variableName
字节.在VB6中,字节位置从1开始.
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);
请注意, SubString
是基于0的,而 Mid
是基于1的,所以我使用了 6
而不是 7 代码>.另外,
Read
方法中的偏移量是从0开始的. Get
在VB6中是基于1的,因此我们减去一个.
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.
这篇关于VB6中的get语句的C#等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!