member references

C

colin

Hi,

I have a lot of places where I need to read and write to members of a class
given an index as a reference,
and have a display title, this would be used in places like datagridview
etc. the members may be nested,
eg Line contains 2 Point(s) a and b, wich both contain x,y,z,
they would be displayed as Xa,Ya,Za,Xb,Yb etc ..

using switch statements or get{}/set{} it would seperate code for
read/write/title etc.
and so it doesnt garantee the same index aplies to both read/write/title.

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 ?

or is data binding worth looking at,
ive not got into this yet it seems to use
xml wich is like yet another language.

thanks
Colin =^.^=
 
P

Peter Duniho

[...]
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
 
C

colin

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
 
P

Peter Duniho

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.

Well, even if you assume that those of us who have replied to past threads
have perfect memory (and believe me, we don't), why restrict the pool of
potential answerers of this most recent question to those who have read
your previous questions?

If you want help, it makes no sense to do things that artificially
restrict the number of people who might answer the question. Just treat
each new question as a completely new topic. Make no assumptions about
what people might know from previous questions.

As far as the specific question goes, it seems to me that just as you
suggested at the end of your post, it's possible that data binding will
work for you here. I don't write much code myself that does data binding,
so I couldn't answer the specifics. But as near as I can tell, you really
just want to map data in your data structures to specific cells in a given
row of your DataGridView. As long as those data are exposed via a
property or some other binding-friendly mechanism (I think properties are
the easiest, but if I recall there are other ways), then you ought to be
able to bind your overall data to the table somehow.

Hopefully someone with more experience in data binding can fill in the
details for you. In the meantime, your time probably would not be wasted
if you spend some of it browsing the topics in MSDN related to data
binding.

Pete
 

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