Storing and reading different types to/from an array

J

Jay

I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?
 
R

Roman Wagner

Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.
 
J

Jay

Thanks for your reply Roman, but Interfaces are currently too advanced for me since I'm a relative
beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution that doesn't involve too much radical
change would be good.

Jay


Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.
 
D

DeveloperX

Thanks for your reply Roman, but Interfaces are currently too advanced for me since I'm a relative
beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution that doesn't involve too much radical
change would be good.

Jay


Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.

There are loads of ways you can convert types in C#. Here are some
examples:

double d = 1.123456;
string s;
int i = 9;
string to double:
s = "1.123456";
d = double.Parse(s); //in framework 2 you can also look at TryParse.

double to string
s = d.ToString("0.00"); //"0.00" is a format string, there are loads
of options for this.

double to int
d = double.MaxValue;
i = (int)d; // This cast will work, but double.MaxValue is much bigger
than an int can hold so it will truncate.

As you can see the types have both static and non static methods and
you can use casts.

Any use?
 
C

Claire

Hello Jay,

Look at MemoryStreams with binarywriters/textwriters

MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write((int)3);
stream.getbuffer()
 
J

Jon Skeet [C# MVP]

Thanks for your reply Roman, but Interfaces are currently too advanced
for me since I'm a relative beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution
that doesn't involve too much radical change would be good.

I would *strongly* recommend that you take the interface approach.
Interfaces aren't that hard to learn, and you'll need to know about
them anyway.

It's a short term hit to make the code much cleaner in the long term.

Jon
 
J

Jay

Thanks for your replies everyone.

John: "I would *strongly* recommend that you take the interface approach." OK, I will have another
look at the chapter on Interfaces in my C# book tonight.

Claire: "Look at MemoryStreams with binarywriters/textwriters". If Interfaces still elude me, I will
look into this.

Jay


"Jay" <nospam> wrote in message I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?
 
G

Guest

Declare an Arraylist, each location takes an object, which can be an enum, a
double, a string of characters, or anything else you can think of. Since you
will always know what you are taking out of the arraylist, you can cast the
object you get back to the right type. You will find the whole process
actually far simpler than the C equivalent.
 
J

Jay

I'm afraid that after reading my C# book, Interfaces still haven't clicked, and timescales dictate
that I must use one of the other two suggestions: ArrayList or MemoryStream.

Am I right in saying that MemoryStream is faster (since ArrayList uses objects)?

Jay



"Jay" <nospam> wrote in message I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?
 
N

Niels Ull

I'm afraid that after reading my C# book, Interfaces still haven't
clicked, and timescales dictate that I must use one of the other two
suggestions: ArrayList or MemoryStream.

Am I right in saying that MemoryStream is faster (since ArrayList uses
objects)?

No, you're not. They're quite different and the performance will very much
depend on usage.
You should probably go for a LinkedList, since you presumably just need to
traverse it in order.

Please be aware that you're setting yourself up for a lot of pain by not
learning a basic language feature like interfaces.
Make sure to submit your final code to www.thedailywtf.com - it should make
for an interesting read.

Good luck!

Niels
 
J

Jay

Thanks Niels, your link was frighteningly relevant.

"You presumably just need to traverse it in order". Mostly, but there is a looping command too (a
bit like a for loop) so there is a jump back command in the sequence. In C, I would do this jump
with a pointer that stores the location of the part of the sequence that I would jump back to. Eg:
JUMP <pointer>.

Does anyone have a good link to using Interfaces for the type of problem that I'm dealing with? The
chapter in my C# book is probably too general for me, especially since OOPs is a bit of a culture
shock for my C-background.

Jay


Niels Ull said:
I'm afraid that after reading my C# book, Interfaces still haven't
clicked, and timescales dictate that I must use one of the other two
suggestions: ArrayList or MemoryStream.

Am I right in saying that MemoryStream is faster (since ArrayList uses
objects)?

No, you're not. They're quite different and the performance will very much
depend on usage.
You should probably go for a LinkedList, since you presumably just need to
traverse it in order.

Please be aware that you're setting yourself up for a lot of pain by not
learning a basic language feature like interfaces.
Make sure to submit your final code to www.thedailywtf.com - it should make
for an interesting read.

Good luck!

Niels
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top