-2

I have a set of fields of different types, I must group them into any data structure. Then I have to pass it to a function and modify the fields the data structure with indexes, like array. How would I do this? Thanks to everyone.

mytype{
   int a;
   ushort b;
   string c;
}

And I would like to pass this data structure that groups all these fields, it can be class or struct. And Pass this to a function and would like to modify the fields of that instance with indexes, like this:

void function(ref mytype G)
{
    G[1] = 1;   <= Here G.a should be set to 1
}
Vikyboss
  • 940
  • 2
  • 11
  • 23
  • 1
    Could you provide some example of your fields, access etc.? – digEmAll Jul 20 '11 at 20:29
  • Vikyboss, this is your fourth time asking the same question. Please desist. Example duplicate of yours: http://stackoverflow.com/questions/6750265/how-to-access-members-of-an-struct-like-an-array-in-c – Ed Bayiates Jul 20 '11 at 20:33
  • @AresAvatar It is not actually same question. First time when I asked it people said instead of struct use array. I used array but the problem is this: http://stackoverflow.com/questions/6767555/how-to-create-an-array-of-custom-type-in-c/6767579#6767579. And some one asked me to repost with all details. This time I made it clear that it can be of any type, but grouped. – Vikyboss Jul 20 '11 at 20:38
  • Vikyboss, it is definitely the same question. Each time you are asking how you can overlay a set of properties or values with an array. – Ed Bayiates Jul 20 '11 at 20:39
  • I find problems while doing things in the way people suggested. Thats why I posted this with making things clear it can be of any type and access and modify with index. Accessing and modifying doesn't have to be array. When doing array way, creating arrays of custom types was a problem. So to find out if any thing other than can be done, I asked this question. I see your point that it's repeated but I'm advancing the question each time I ask them ignoring unneeded details and boundaries. – Vikyboss Jul 20 '11 at 20:44

3 Answers3

3

You could use a list of objects:

var list = new List<object>();
list.Add("strings are cool!");
list.Add(42);

// ...

var objByIndex = list[1];        // 42

You lose all the advantages of C#'s strong-typing if you do this, though. I would suggest using a class with well-defined, strongly typed properties instead, unless you really need something generic (where you don't know anything about the properties' types before-hand).

Cameron
  • 96,106
  • 25
  • 196
  • 225
2

To pass a parameter to a method by reference, you can use the ref keyword. So you could do something like this:

void Method1(Class1 obj1, Class2 obj2, ref Class3 obj3)
{
   obj3 = new Class3();

   // set the fields of obj3 based on values of obj1 and obj2
}

Method1(obj1, obj2, ref obj3);

As for "modify the fields the data structure with indexes, like array", you could pass those indexes as additional parameters if needed. I would have to know a little more about what you are doing to answer that though.

Note that in the above example, the only reason to pass obj3 by reference is because the method is creating the instance of the object. If the object is already created, then passing by reference isn't needed.

dcp
  • 54,410
  • 22
  • 144
  • 164
0

Modifying an object is fairly easy, as long as you are not passing it across certain boundaries (process, machine, network (variation of machine)). You can pass in using the ref keyword, but it is not necessary in most instances.

It really depends on what you mean by data structure however. Are you talking actual objects you have created or data rows in a dataset, something you created in EF?

The concept is the same. If you pass across a boundary, you are better to "reset" your object than try passing by reference.

Give some guidance on your meaning and I can follow up with more specific information.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32