2

I have import an unmanageable .dll to my project. It's no any document left and the original working code is in VB6. so I try to make C# code equivalent to VB6 as same as possible.

PROBLEM

I don't know how to convert following code to 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. So, I think input signature for the dllImport function might be wrong. So, I want to make it as same as VB6 did because I didn't know exactly what should be the right signature.

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)
Flexo
  • 87,323
  • 22
  • 191
  • 272
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96
  • 2
    We are not code converter. – Raptor Oct 31 '14 at 02:39
  • @Raptor oh so sorry about that – Jongz Puangput Oct 31 '14 at 02:40
  • The *10 just creates a fixed length string and I don't think that exists in c# (might be wrong). But the question is more: Do you really need a fixed length string? – Chief Wiggum Oct 31 '14 at 02:44
  • 1
    @Alexi: the linked question is not a duplicate of this. That question is about VB.NET. This one is about C#. – John Saunders Oct 31 '14 at 02:44
  • The original code example is use like this so I don't which it requires to be fixed length or not – Jongz Puangput Oct 31 '14 at 02:46
  • 1
    @MarkJ The alleged duplicate is both VB.NET specific and has the extra detail of being about a single character string. – Mark Hurd Oct 31 '14 at 13:28
  • @MarkJ Please reconsider this post as duplicate since the accept answer as you refer to is not resolve my problem and not answer to my question. I have updated question and added my answer. – Jongz Puangput Nov 06 '14 at 00:13
  • 1
    Since the question is re-opened you can post your answer as an answer properly, and not as an edit. (I've rolled it back to keep a distinction between the question/answer bit, but you can still see it in the revisions history to form the basis of your answer if you want) – Flexo Nov 06 '14 at 08:51

2 Answers2

3

You can use 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. It also sets an "uninitialised" value, like above, to nulls.

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):

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"
"Test "
"Testi"
"Test "
"Testi"

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
  • Mark, pads with spaces, or with nulls? I was told in a comment below that it's nulls. – John Saunders Oct 31 '14 at 19:22
  • @JohnSaunders Definitely spaces. It fills with nulls only in the above situation where the value is still "uninitialised". – Mark Hurd Oct 31 '14 at 23:47
  • Mark, perhaps you could add an example demonstrating the padding behavior? In fact, some examples showing how to use it would be good, in general. You might also mention that these are not only "not supported for 64-bit processes", but that they will produce compiler warnings - they are obsolete. – John Saunders Nov 01 '14 at 00:22
1
string ATQ;
string Uid;
string MultiTag;

One difference is that, in VB6, I believe the String * 10 syntax may create fixed-size strings. If that's the case, then the padding behavior may be different.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • So you mean it doesn't matter in c# ? – Jongz Puangput Oct 31 '14 at 02:47
  • @JongzPuangput he is saying that the difference may lead to some difference in output – Robert Snyder Oct 31 '14 at 02:48
  • I mean that we don't generally use fixed-size strings in C#. If the size of the string doesn't matter, then you're ok. I don't remember how VB6 handled padding. For instance, in `Dim ATQ As String * 10`, then `Let ATQ = "abc"`, is `ATQ` equal to `"abc "`? There's no automatic way to do that in C#. – John Saunders Oct 31 '14 at 02:49
  • So is there a way to do fixed length string in c# @Robert – Jongz Puangput Oct 31 '14 at 02:49
  • It depends - what are you trying to accomplish with the fixed-length strings? For instance, you could call `ATQ = ATQ.PadRight(10);` after setting `ATQ` to pad it to 10 characters, if you needed that. – John Saunders Oct 31 '14 at 02:49
  • @JongzPuangput there kinda is using one of the string constructor overloads. http://msdn.microsoft.com/en-us/library/xsa4321w(v=vs.110).aspx this shows you can specify how many characters to make a string – Robert Snyder Oct 31 '14 at 02:51
  • @RobertSnyder: no, that repeats the character "n" times. – John Saunders Oct 31 '14 at 03:00
  • Aren't all .NET strings, by definition, "fixed length strings?" They're immutable, after all, so it's not like you can make them longer or shorter. In any case, the thing I'd worry about is code that modifies those strings. That is, `ATQ(1) = 'x'`, or the like. Or am I falsely remembering that VB6 strings were mutable? – Jim Mischel Oct 31 '14 at 03:11
  • 1
    @JimMischel: an individual string instance is immutable, hence of fixed length (once it is created), but in VB6, those variables can hold only strings with a fixed number of characters. – John Saunders Oct 31 '14 at 03:12
  • That's not precisely correct, John. VB6 pads the rest of the string with null characters, if you don't use all of it. – BobRodes Oct 31 '14 at 18:11
  • @BobRodes: OK, thanks. I didn't know if it was spaces or nulls. It's been a while since I've used VB6, and even longer since I've used fixed length strings. – John Saunders Oct 31 '14 at 18:30
  • Yeah, nulls. There's confusion on this because often people pad a string with spaces instead of nulls when passing a string argument to an API in a situation where the API directly modifies the argument. [Here's](http://msdn.microsoft.com/en-us/library/aa263531(v=vs.60).aspx#feedback) something on it, in particular the last section. – BobRodes Oct 31 '14 at 19:28