简体   繁体   English

等效于C#中VB6中的字符串* 10

[英]Equivalent to string * 10 from VB6 in C#

I have import an unmanageable .dll to my project. 我已经将无法管理的.dll导入到我的项目中。 It's no any document left and the original working code is in VB6. 剩下的文件都没有,原始的工作代码在VB6中。 so I try to make C# code equivalent to VB6 as same as possible. 所以我尝试使C#代码尽可能等同于VB6。

PROBLEM 问题

I don't know how to convert following code to C#... 我不知道如何将以下代码转换为C#...

Dim ATQ As String * 10
Dim Uid As String * 10
Dim MultiTag As String * 10

NOTE 注意

Q: some users ask me that do you really need string fixed length? 问:有些用户问我,您真的需要固定长度的字符串吗?

A: I already try string in c# but there are no result update to these variable. 答:我已经在c#中尝试过string ,但是没有更新这些变量的结果。 So, I think input signature for the dllImport function might be wrong. 因此,我认为dllImport函数的输入签名可能是错误的。 So, I want to make it as same as VB6 did because I didn't know exactly what should be the right signature. 因此,我想使其与VB6相同,因为我不知道确切的签名是什么。

TRIAL & ERROR 试用与错误

I tried all of this but it's not working (still no result update to these variable) 我尝试了所有这些方法,但是没有用(仍然没有结果更新到这些变量)

Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString ATQ = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString Uid = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString MultiTag = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)

You can use Microsoft.VisualBasic.Compatibility : 您可以使用Microsoft.VisualBasic.Compatibility

using Microsoft.VisualBasic.Compatibility;

var ATQ = new VB6.FixedLengthString(10);
var Uid = new VB6.FixedLengthString(10);
var MultiTag = new VB6.FixedLengthString(10);

But it's marked as obsolete and specifically not supported for 64-bit processes , so write your own that replicates the functionality, which is to truncate on setting long values and padding right with spaces for short values. 但是它被标记为已过时,并且特别不支持64位进程 ,因此请编写您自己的文件来复制该功能,即在设置长值时截断并在短值处添加空格。 It also sets an "uninitialised" value, like above, to nulls. 像上面一样,它还将“未初始化”值设置为null。

Sample code from LinqPad (which I can't get to allow using Microsoft.VisualBasic.Compatibility I think because it is marked obsolete, but I have no proof of that): LinqPad中的示例代码(我认为不允许using Microsoft.VisualBasic.Compatibility因为它已被标记为过时,但我没有证明):

var U = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5);
var S = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Test");
var L = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Testing");
Func<string,string> p0=(s)=>"\""+s.Replace("\0","\\0")+"\"";
p0(U.Value).Dump();
p0(S.Value).Dump();
p0(L.Value).Dump();
U.Value="Test";
p0(U.Value).Dump();
U.Value="Testing";
p0(U.Value).Dump();

which has this output: 具有以下输出:

"\\0\\0\\0\\0\\0" “\\ 0 \\ 0 \\ 0 \\ 0 \\ 0”
"Test " “测试”
"Testi" “TESTI”
"Test " “测试”
"Testi" “TESTI”

string ATQ;
string Uid;
string MultiTag;

One difference is that, in VB6, I believe the String * 10 syntax may create fixed-size strings. 一个区别是,在VB6中,我相信String * 10语法可以创建固定大小的字符串。 If that's the case, then the padding behavior may be different. 如果是这种情况,则填充行为可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM