structure problem in c#
-
what is the equivalent of following C structure in c# struct mystructure { int number; char name[256]; };
Rajesh
Hi, it depends on what you want it for. If it's just data being shared between two pieces of C# code, then use:
struct myStructure {
int number;
string name;
}and it will hold your data, whatever the length of the name string will be. If on the other hand you want to share the data with code in another programming language, or with a hardware device, then it will take more effort (e.g.with Marshaling attributes or code) to match the requirements. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, it depends on what you want it for. If it's just data being shared between two pieces of C# code, then use:
struct myStructure {
int number;
string name;
}and it will hold your data, whatever the length of the name string will be. If on the other hand you want to share the data with code in another programming language, or with a hardware device, then it will take more effort (e.g.with Marshaling attributes or code) to match the requirements. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
actually i am writing some fields from webpages to file using structure. then I read the file using same structure in the c++ programme . in c++ no string data type so I cannot use string in my structure
Rajesh
No string data type? But then what this: http://en.wikipedia.org/wiki/String_(C%2B%2B)[^] Thats assuming you can include <string>
My current favourite word is: Bacon!
-SK Genius
-
actually i am writing some fields from webpages to file using structure. then I read the file using same structure in the c++ programme . in c++ no string data type so I cannot use string in my structure
Rajesh
Three comments then: so it is a binary file (because of the int) although probably most of the data is text? I would make it all text then (hence use int.ToString and/or int.TryParse) are you aware of character sizes? in C/C++ it could be 8-bit or 16-bit, on C# it will be 16-bit. Some people would recommend using XML for something like this. I'm still undecided. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
No string data type? But then what this: http://en.wikipedia.org/wiki/String_(C%2B%2B)[^] Thats assuming you can include <string>
My current favourite word is: Bacon!
-SK Genius
-
I am c# programmer and c++ site cannot change .it is already running and cannot change huge code there.
Rajesh
Fair enough then.
My current favourite word is: Bacon!
-SK Genius
-
actually i am writing some fields from webpages to file using structure. then I read the file using same structure in the c++ programme . in c++ no string data type so I cannot use string in my structure
Rajesh
You can use a BinaryWriter to write the data to the file. The .NET equivalent of a C++ char is a byte. If you have text to put in the file, you have to know what encoding to use to turn it into an array of bytes.
Despite everything, the person most likely to be fooling you next is yourself.
-
what is the equivalent of following C structure in c# struct mystructure { int number; char name[256]; };
Rajesh
structure mystructure { int number; string name; } The default for those variables in C# will be private (I believe), you will have to create properties to expose the variables.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]