|
Here's what I've got:
public struct ItmAray
{
public string FieldName;
public int FieldIndex;
}
public struct dtlst
{
public ItmAray[] ItemArray = new ItmAray[128];
}
public dtlst[] DataList = new dtlst[128];
This is what I want:
DataList[x].ItemArray[y].FieldName = clbText;
DataList[x].ItemArray[y].FieldIndex = int.Parse(a);
This is what I'm getting:
an error: "cannot have instance field initializers in structs" from the dtlst structure:
public ItmAray[] ItemArray = new ItmAray[128];
I believe the problem is the righthand side of the line.
But I can't figure out how to fix it.
Thanks!
|
|
|
|
|
KKW_acd wrote: righthand side of the line
You are correct. As the message says, you cannot initialize a field when it is first declared in a struct, all fields assume the default value for the type which in the case of an array is null. Initialization must be done in a constructor (however you cannot have a parameterless constructor for a struct either).
When to use a struct or a class can be complicated sometimes. I think your dtlst (horrible name by the way - why not DataList ?) should most likely be a class. You will then be able to initialize the field as you wish.
Also, public fields are normally bad practice, make them private and use a property (getter only unless a setter is needed) to expose the field.
Edit: There's still a few things about this that don't feel right, but to fit your code and usage, this is a quick example of something that works. This is not meant to be doing your homework for you, but rather to point you in the correct direction and give you something to compare your code too:
public struct Item
{
private string name;
private int index;
public Item(string name, int index)
{
this.name = name;
this.index = index;
}
public string Name { get { return name; } }
public int Index { get { return index; } }
}
public class DataList
{
private Item[] items = new Item[128];
public Item[] Items { get { return items; } }
}
DataList[] dataLists = new DataList[128];
int listIndex = 0;
int itemIndex = 0;
string itemText = "Random text";
string indexText = "1";
dataLists[listIndex] = new DataList();
dataLists[listIndex].Items[itemIndex] = new Item(itemText, int.Parse(indexText));
modified 15-Nov-12 13:19pm.
|
|
|
|
|
Dave,
Thankyou!
That is exactly what I needed!
Also, I'm flattered that you think this is homework! I wish I was as young. I'm an old bit-twiddler from the DOS and Microprocessor days. I even remember watching man's first step on the moon on an Admiral B&W tv! - Live!
Thanks again!
|
|
|
|
|
No problem.
KKW_acd wrote: I wish I was as young
Ah, sorry!
KKW_acd wrote: old bit-twiddler from the DOS and Microprocessor days
Then, once you get used to C# you will find this a lot of fun, and in many ways, a lot easier. Gone are the days where variables and function names need to be short and cryptic, we can be alot more verbose now e.g. A date and time combined is a DateTime not dtm . We have a powerful framework that has the vast majority of the dirty/hard work done for us. Allocating and freeing memory is taken care of (in most situations), threading, callbacks etc are a breeze compared to the old days. Many, many other things too.
It still of course requires the same skills and dedication/perseverance but the curve is somewhat shallower.
|
|
|
|
|
KKW_acd wrote: I even remember watching man's first step on the moon on an Admiral B&W tv! - Live! Me too, although a different TV model.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
In your dtlst structure you cannot initialise the instance variable ItemArray automatically, as struct s do not have default (parameterless) constructors. You would need to initialize the variable after you have created the struct (s), i.e after you create the DataList array.
It is explained more clearly here[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The immediate error is what's in the message.
However, I suspect you didn't mean to create a struct here. A struct in C# is a value type, so wherever it is passed to a method, a copy is taken, and the initial version is not modifiable. However, the array within the struct will be modifiable and the array in the outside version of the struct will be updated! A struct containing references to reference types, like an array, is usually not what you meant to do.
I think you are trying to recreate the C++
typedef struct {
string FieldName;
int FieldIndex;
} ItmAray;
typedef struct {
ItmAray[128] ItemArray;
} DtLst;
DtLst[128] DataList;
But the arrays there are references: DtLst[100] is a reference to a DtLst, and passing it to a function will pass it by reference. In C# I think you want a class.
Also, fixed length arrays are generally not what you want in the modern world. I think what you actually want is
class Field {
string Name;
int Index;
}
class DataListItem {
List<Field> Fields;
}
List<DataListItem> DataList;
|
|
|
|
|