Jump to content

String Array Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
I've got some C# code I'm trying to convert to C++

In C# I can send the string array to my DLL command via


stringarray[1]="trash";

stringarray[2]="trash";

...

stringarray[22]="trash";


returnvalue = MyCommand(stringarray);


When I attempt this in C++

std::string stringarray[23];

int configlines =0;

stringarray[1]="trash";

stringarray[2]="trash";

...

stringarray[22]="trash";

returnvalue = mynamespace::myproj::MyCommand(stringarray);


I get cannot convert parameter 1 from std::string[23] to cli::array<Type,dimension>

the MyCommand shows up as
public static System.Int32 MyCommand(System.String[] stringarray)
Member of mynamespace.myproj
in the Object Browser
What am I doing wrong.

note I don't normally code C++ but use C# & VB.NET

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
std::string probably isn't the same as System.String, which would cause a problem. See if there's a version of System.String available in MSVC++.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Could you post the compiler output of your C++ program, what I see should be okay depending on how you're using MyCommand. I'm just trying to find out where the first error is.
Wow I changed my sig!

#4
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
Ug I think I hate C++ :D

But I got it. (Now I need to understand it :D )

array<System::String^>^ stringarray;
stringarray = gcnew array<System::String^>(23);
This takes care of my error

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Okay this had to do with the difference of static and dynamic memory allocation. I guess Managed C++ is a LOT different from standard C++...

Anyway, your problem then was probably that you were attempting to pass a reference to a statically declared object, and that object fell out of scope, therefore the reference was invalid. C++ doesn't automatically dynamically allocate everything like C# and Java do, instead you can choose between one or the other. However, I can't be sure of that since apparently the syntax is completely alien to what I'm used to.

I'm also assuming that "gcnew" means "garbage collected new", therefore you needn't worry about deleting it. Crazy...
Wow I changed my sig!

#6
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

ZekeDragon said:

Okay this had to do with the difference of static and dynamic memory allocation. I guess Managed C++ is a LOT different from standard C++...

Anyway, your problem then was probably that you were attempting to pass a reference to a statically declared object, and that object fell out of scope, therefore the reference was invalid. C++ doesn't automatically dynamically allocate everything like C# and Java do, instead you can choose between one or the other. However, I can't be sure of that since apparently the syntax is completely alien to what I'm used to.

I'm also assuming that "gcnew" means "garbage collected new", therefore you needn't worry about deleting it. Crazy...

I've no idea :D
I found a few code snip on the web dealing with array strings, and they used the gcnew, It compiled & worked (even sent the proper data to my DLL that sent it over Pipes to my server :thumbup: )

So I'm happy, but I'll admit I like C# over C++ right now :D

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Well, if you want to learn C++ proper, you're going to have to not utilize Microsoft's extensions. For example, in your above program I would have written everything differently using std::vector and std::string:
std::vector<std::string*>* stringarray = new std::vector<std::string*>;
while (stringarray->length() < 23)
    stringarray->push_back(new std::string("trash"));
Then you'd have to clean it like so when you're done with it:
for (int iii = 0; iii < stringarray.length(); ++iii)
    delete stringarray[iii];

delete stringarray;
That's why I was saying that was pretty different looking. :P
Wow I changed my sig!

#8
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
I'm still working on getting C# down :D

I just needed to code some form of working example in C++ so the client's programmer could see how to connect to the DLL that connects to the server :D

I had given them an example in VB.NET
using mynamespace.myproj
dim stringarray(22) As String
stringarray(0) = "first sample";
stringarray(1) = "another sample";
stringarray(2) = "another sample";
...
stringarray(22) = "final sample";
returnvalue = MyCommand(stringarray);

And was thinking they could use the DLL's XML file & that example to figure out how they want to add it to their project.

But turns out they couldn't convert the VB.NET example to a C++ example.

So that's why I needed to code something in C++ to show how.
How they handle & configure the strings is up to them, as long as they pass it to MyCommand(StringArray).

FYI.
I've normally coded in VB.NET for the last few years, it works, but to improve my skills I've started this new project in C# and got most of it working.
Still a few bugs, but this isn't even a beta version yet :D

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
How large of programs are we talking about here? It might be more practical to simply reimplement stuff in a different language rather than try converting it, because there are a lot of things that simply don't convert smoothly.

I hope you enjoy C#, it's really a good experience to get out of the Basic dialects and into the "real world" I suppose. I still use a Basic, and I don't think that'll change any time soon, but I've really found the other languages to be rewarding as well. Take a look at all the languages here on CC, if you're expanding your horizons, and maybe you'll find another one you really like. :)
Wow I changed my sig!

#10
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
How Large ?
The Source Files (*.cs) for the server are sitting at 125K right now, though there's a lot of remarks in it right now.

The Server App itself is only 55K compiled.
The DLL to talk to the server is 11K compiled.

We're supplying the DLL to talk to our server app, they're creating a whole app around the DLL that talks to both our server/ hardware & their robots / hardware / network ect :D

Fun project for real time testing of product in a production environment.