2

I've been tasked with converting some legacy code to a new system and we've got some VB6 structures that are here. Is there a way to convert them into a C# structure easily?

I could redefine the structure in C# but there's no fixed strings in C#. (Or maybe I misunderstand)

Any prods in the right direction?

Private Type MapRec
    Name As String * NAME_LENGTH
    Revision As Long
    Moral As Byte
    Up As Integer
    Down As Integer
    Left As Integer
    Right As Integer
    Music As String
    BootMap As Integer
    BootX As Byte
    BootY As Byte
    Tile() As TileRec
    Npc(1 To MAX_MAP_NPCS) As Integer
    NpcSpawn(1 To MAX_MAP_NPCS) As SpawnRec
    TileSet As Integer
    Region As Byte
End Type
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vaughan Hilts
  • 2,839
  • 1
  • 20
  • 39
  • Added a bit, wasn't sure how neccessary it really was. – Vaughan Hilts Sep 25 '12 at 23:26
  • here are some ideas... http://stackoverflow.com/questions/179605/how-can-i-convert-vb6-code-to-c – hagensoft Sep 25 '12 at 23:30
  • Not exactly what I need - I'm mostly looking for a way to side-step the fixed-string issue and other quicks of VB6. – Vaughan Hilts Sep 25 '12 at 23:31
  • Also, be aware that VB6 types are not the same as VB.NET types. VB6 "Integer" corresponds to C# "short" and VB6 "Long" corresponds to C# "int". – Dave Doknjas Sep 25 '12 at 23:39
  • Yeah, these are exactly the kind of quirks I'm talking about. =) Is there some kind of black magic I can use to just Marshal it back in neatly without doing a byte-read? :D – Vaughan Hilts Sep 25 '12 at 23:40
  • 1
    There's plenty of support for this in the .NET framework, it lives in the Microsoft.VisualBasic namespace. An obvious way to take advantage of it is by writing a class library in vb.net code. That gets you started quickly on the documentation. The CLR doesn't care what language you use. – Hans Passant Sep 25 '12 at 23:43
  • Names of some classes would be helpful. =) – Vaughan Hilts Sep 25 '12 at 23:45
  • 1
    @MattJohnson provided a couple of them in his answer. – Cyberherbalist Sep 26 '12 at 15:41
  • I still don't see an obvious way to load 1000s of these files into these C# structs, though. Which is a requirement of the question. – Vaughan Hilts Sep 26 '12 at 21:59

2 Answers2

3

You may be interested in the VBFixedStringAttribute, and the VBFixedArrayAttribute although they are only utilized in a few places.

See also this question and this question.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
3

With respect to fixed-length strings, yikes. It ain't gonna happen because there is no equivalent construct. Unless Jon Skeet or Anders Hejlsberg know differently and can be invoked to weigh in -- I don't think even they know a way, cuz there ain't one, I am pretty certain.

On the other hand, fixed-length strings are absolutely Satanic. Which is why they didn't include them in .NET. :-)

If you were to ask me how I would convert the above MapRec object to something usable in C#, well you kind of have your choice between a struct and a class. Personally, I dislike structs. If you used a class, then you could implement a kind of bastardized fixed-string by way of your setters and getters. As seen in this example, which is how I would implement your Type MapRec:

public class MapRec
{
    private const int MAX_MAP_NPCS = 25;
    private int fixedLength1 = 10;
    private string _name;

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value.Length != fixedLength1)
            {
                if (value.Length < fixedLength1)
                {
                    _name = value.PadRight(fixedLength1);
                }
                else
                {
                    _name = value.Substring(0,fixedLength1);
                    // or alternatively throw an exception if 
                    // a 11+ length string comes in
                }
            }
            else
            {
                _name = value;
            }
        }
    }

    // Constructor
    public MapRec()
    {
        Npc = new int[MAX_MAP_NPCS];
        NpcSpawn = new SpawnRec[MAX_MAP_NPCS];
    }

    public long Revision { get; set; }
    public byte Moral { get; set; }
    public int Up { get; set; }
    public int Down { get; set; }
    public int Left { get; set; }
    public int Right { get; set; }
    public string Music { get; set; }
    public int BootMap { get; set; }
    public byte BootX { get; set; }
    public byte BootY { get; set; }
    public TileRec[] Tile { get; set; }
    public int[] Npc { get; set; }
    public SpawnRec[] NpcSpawn { get; set; }
    public int TileSet { get; set; }
    public byte Region { get; set; }
}

In the end, unless one actually needs a fixed-length string (and perhaps Microsoft.VisualBasic.VBFixedStringAttribute could do the job), I would suggest staying the heck away from them.

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • +1 I was working on a very similar block of code and you just beat me too it – psubsee2003 Sep 26 '12 at 00:25
  • Yep, they just call me "QuickCode" around these parts... :-) – Cyberherbalist Sep 26 '12 at 00:27
  • So you wouldn't use a char[] array for the fixed length string? If not, why not? – slugster Sep 26 '12 at 02:25
  • @slugster I can't speak for Cyberherbalist, but I considered and dismissed it while working on a similar answer was because the only way you can fix the size of the array (by preventing creation of a new array) is with the `readonly` keyword, then you need to loop to populate the array. – psubsee2003 Sep 26 '12 at 08:59
  • I wasn't attempting to provide a complete solution -- that's left as an exercise for the asker. They're not paying me to do his job. I just took his VB6 code and converted directly over to C#. I assumed there were reasons for using particular structures, reasons I didn't know, and left it at that. In any case, depending upon business requirements, a char[] array might be better than the thing I provided, but I don't know his situation and what he has to work with. My clairvoyance has been acting up lately. :-) – Cyberherbalist Sep 26 '12 at 15:33