-
December 16th, 2009, 12:24 AM
#1
Application Organization
Let me preface this by saying that I am new to C# so you may have to explain things explicitly...
I have an electronic device that is to be configured by an application developed in C#. The details of how this is done are not important. What is important to know about the device is that there are tables of data in FLASH memory on the device. The tables are defined with a known number of rows, and fields of fixed length. So, each table is a constant known length. These tables are transfered to the C# application and are stored for viewing and editing. Eventually they are written back to the device via a serial connection or to a file on the hard drive.
I started my application by developing a class called Device. I thought I would create some private structures for the tables and then create public methods to read and write a given table from file or serial port. Since both supply a stream of bytes I thought it would be simple to have the incoming data written directly into a structure array. That way I could simply set up a pointer to the struct and chunk data into it (write data, inc pointer, repeat). Given that the fields are of fixed length, the data would essentially be parsed for me. Meaning, once the data was written to the struct I could access a particular field in any row (table[row].field). This would be so much easier than trying to parse the data on the fly. Hopefully my objective makes sense.
I quickly found out that C# doesn't allow you to specify array size in a struct as I can in C. For example, take my simplest table, the users table:
Code:
struct User{
char[32] Name;
char[64] Email;
byte AccessLevel;
}
Given this, is there any way that I can satisfy my desire to have sequential read/write of the entire table contents while not losing the 'automatic parsing' that comes with writing into a structure?
It would be nice if I could achieve the sequential read/write as well as expose the data fields so that they could be accessed like so:
Code:
Device.User[10].Name
Thoughts?
-
December 16th, 2009, 01:09 AM
#2
Re: Application Organization
You can do fixed size arrays in C# using fixed keyword in unsafe code,
Code:
unsafe{
public struct User{
public fixed char[32] Name;
public fixed char[64] Email;
public byte AccessLevel;
};
}
Regards,
Ramkrishna Pawar
-
December 16th, 2009, 01:06 PM
#3
Re: Application Organization
Originally Posted by Krishnaa
You can do fixed size arrays in C# using fixed keyword in unsafe code,
Code:
unsafe{
public struct User{
public fixed char[32] Name;
public fixed char[64] Email;
public byte AccessLevel;
};
}
I would also add that the "fixed" keyword will pin the object in memory and prevent the GC from relocating it when it tidies the heap space.
-
December 16th, 2009, 05:00 PM
#4
Re: Application Organization
I am new to C# as well so chances are high that I am wrong... but can't you do something like:
Code:
char[] Name = new char[32];
?
-
December 16th, 2009, 05:28 PM
#5
Re: Application Organization
Originally Posted by TryingToC#
I am new to C# as well so chances are high that I am wrong... but can't you do something like:
Code:
char[] Name = new char[32];
?
No, but you can do this:
Code:
struct Foo
{
char[ ] c;
public foo( int len )
{
c = new char[ len ];
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|