View Single Post
Old Jul 10th, 2005, 5:37 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I really don't know what you're doing here, as those things aren't a part of typical C# code. I realize that if one has to interface with old code, dll's, and things like that, one might have to resort to interfacing, unmanaged code, and unsafe objects. Maybe that's what you're doing. At any rate, it's too much work to mess with for an example.

Even if you're in the wrong forum, and meant for it to be C++ code, you have some problems. When you're dealing with wide characters, you can't just go dropping literals here and there. I've include here some source code. You can see that your approach to a functional cross reference will work (you can't return a pointer to a local variable and get away with it, of course). It's just far less effective than other ways to accomplish the same thing. I've shown the method of using an array with the building number as an index into the array. For that part, I didn't bother to make the strings wide characters. You can see from the example how to do that.
#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void NameBuilding (int bNum, TCHAR *bName);

int _tmain (int argc, _TCHAR* argv[])
{
    TCHAR temp [80];
    char bldgText [][80] = {"Metal Mine", "Crystal Mine", "Deuterium Mine", "Solar Plant",
                     "Robotic Plant", "Shipyard", "Metal Storage", "Crystal Storage",
                     "Deuterium Tank", "Research Lab", "Rocket Silo"};
    NameBuilding (0, temp);
    wprintf (_T("%s\n"), temp);
    // Now do it the easy way
    cout << bldgText [5] << endl;
	return 0;
}
void NameBuilding (int bNum, TCHAR *bName)
{
    switch (bNum)
    {
        case 0:
        wsprintf (bName, _T("%S"), "Metal Mine");
        break;
    }
}
Again, if you're really going to use C#, and if you're NOT having to mate it with non-C# stuff, bite the bullet and learn it straight up, don't try to port other knowledge you have to it that isn't really applicable.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote