Sorry if I was too vague for you, I assumed if someone was familiar with
doing
what I was doing they would know what I was talking about.
the index is simply an integer like would be the index into the columns of a
DataGridView,
there is also a row index of course but thats easy its just a plain index
into an array.
the column index needs to refer to a title for the colum headers,
and to the particular data field of the class
the project is involved theres so much info i could give out
its hard to know where to draw the line.
I agree whole hartedly about reflection thats why I asked first,
having used it once before.
I have however come up with a solution wich solves my
worries about making absolutly sure the title/read/write all apply to the
same field.
the refererence is 100% certain the same for read/write,
and the title is on the same line so cant go too far wrong.
I havnt figured out how to actually do the write/set part yet
at least not with a function template.
public enum Get
{
Get,
Set,
Title
}
//does the actual get/set/title
public string GetSet<T>(string title, ref T field, string value, Get get)
{
if (get == Get.Title)
return title;
if (get == Get.Get)
return field.ToString();
if (get == Get.Set)
{
//need to come up with a way to read the text value in.
throw new Exception("The method or operation is not implemented.");
}
return null;
}
//looks up the relavant field/class data member
public override String FieldGet(int index, String value, Get get)
{
switch (index)
{
case 0: return FindIndex().ToString();
case 1: return GetSet("Org.X", ref surfProp.Origin.X, value, get);
case 2: return GetSet("Org.Y", ref surfProp.Origin.Y, value, get);
case 3: return GetSet("Org.Z", ref surfProp.Origin.Z, value, get);
default: return null;
}
}
//the actual data class
public class Surface //snipet of one of many much larger classes
{
public Vector3 Origin; //struct of 3 floats x,y,z
}
//the function wich fills in the datagrid rows and columns
public void FillGrid(DataGridView grid, List<Surface> surfaces)
{
int i;
String result;
grid.Columns.Clear(); ///assume surfaces isnt empty
for (i = 0; (result= surfaces[0].FieldGet(i, null, Get.Title)) != null;
i++)
grid.Columns.Add(result, result);
String[] row = new String
;
grid.Rows.Clear();
foreach (Surface surf in surfaces)
{
for (i = 0; (result= surf.FieldGet(i, null, Get.Get)) != null; i++)
row=result;
grid.Rows.Add(row);
}
}
this worked at least before I cut out the seemingly irrelavent stuff
Peter Duniho said:
[...]
so for each item I was thinking of having an array of strings wich
contained
the title as the first string,
then the next string would be the member name and sub member names etc.
then use reflection to set/get the value.
ive used reflection before for serialisation but its quite complicated,
so id just check to see if maybe theres an easier way
or if theres some library wich already does this ?
Not only is it likely there's a better way than reflection, my experience
has been that except in cases where the goal is _specifically and
directly_ related to the data structure's characteristics in a way that is
unknowable at that point in the code, reflection is really just not
necessary and should be avoided.
As for specifically how to address this, it's hard to say because your
question is vague. You didn't post any code to demonstrate what you're
talking about, and your description isn't clear enough to evoke a
definite, unambiguous idea of what the code would actually look like.
For example:
* "given an index as a reference"...a reference to what?
* "a display title"...this is relevant how?
* "using switch statements"...why would you use a switch statement
with an index? an index implies you've got some indexed data structure
If you have a situation in which you think a switch statement does make
sense, then rather than a plain index, you should probably use an
enumeration. That way, there's a plain English name associated with each
item, making it less likely you'll run into mis-matched cases across
classes.
But is a switch statement really needed? I don't know. There's not
enough information in your question to tell.
Why not just post a code sample that illustrates the basic idea. It
doesn't have to be complicated, but it should completely illustrate all
aspects of the situation you're dealing with. (All aspects _directly_
related to the problem...don't just copy and paste some live code from
your application without trimming it down to the bare details necessary).
Pete