3

I have 12 columns in a datagridview (they're 12 properties going from v1 - v12). Is it possible to create a dynamic system that does something like this:

int i = 5;
var variablename = "v" + i;
String content = product.variablename;

This would be a generic function of

if(i == 5) {
    content = product.v5
}

Yes, I know that naming my properties v1-v12 isn't good practice, but it has its uses. I could write 12 if clauses, but I'm just wondering if it's possible or not.

EDIT:

In the specific example I have an array of 8000 products with each v1-v12 properties. I want to dynamically get the values of specific cells, so product[row].(v+column) should become products[23].v5 for example.

It's more of an example case than it is actually needed, just want to figure out if it can be done.

Lonefish
  • 647
  • 2
  • 11
  • 32
  • 4
    You can, with reflection. It’s a terrible idea. Use a dictionary or something. – Ry- Dec 03 '15 at 09:00
  • Are you just trying to iterate to the 5th (4th?) column in a grid view? – Sayse Dec 03 '15 at 09:01
  • 2
    It would be easier if you use `Arrays` instead. – Mohit S Dec 03 '15 at 09:02
  • I'm a bit lost. What are you trying to do? You never write to `product.variablename` –  Dec 03 '15 at 09:02
  • Possible duplicate of [Calling a function from a string in C#](http://stackoverflow.com/questions/540066/calling-a-function-from-a-string-in-c-sharp) – MakePeaceGreatAgain Dec 03 '15 at 09:04
  • 3
    I think you're missing the point of *why* naming your properties `v1`-`v12` isn't a good practice. If there's no specific name you can give those properties, use an array (or a list) - if there is, use a good name. It's that simple. – Luaan Dec 03 '15 at 09:04
  • In response to your comment on Yahya's answer... [How to: Get the Selected Cells.... in the Windows Forms DataGridView Control](https://msdn.microsoft.com/en-us/library/x8x9zk5a%28v=vs.110%29.aspx) – Sayse Dec 03 '15 at 09:06
  • Duplicate, yes, in the sense that it has the same solution apparently. No, in the sense that it's variables/properties instead of functions. And sure, I could give them meaningful names, but as stated in the original question, it's just an example. I want to know whether or not it is possible. And it is, with reflection. – Lonefish Dec 03 '15 at 09:10
  • 2
    @Lonefish: reflection should be the very last resort, it's inefficient, difficult to understand and to maintain, has no compile time safety, could break in the future (f.e. if you access properties by name which don't exist anymore in later versions). Most times the need of reflection just indicates a wrong approach(f.e. using 12 properties with similar name instead of one list or array). – Tim Schmelter Dec 03 '15 at 09:14
  • @TimSchmelter Thanks, will keep that in mind ;) – Lonefish Dec 03 '15 at 09:15

3 Answers3

5

Dictionary will give you flexibility and control to store as many as you like and retrieve them without using Reflection etc.

var values = new Dictionary<string, string>();
values.Add("v" + i, "somevalue");

and to retrieve it:

var storedValue = values["v" + i];

On a side note, if you are using Data Grid View to have multiple lines. You can also use DataTable to store information. Or if you have a fixed structure, why not make a class that will represent that information and use List<T>

Yahya
  • 3,386
  • 3
  • 22
  • 40
  • Doesn't really fit the purpose. I have an array of products (12 fields) and 8000 products long. So I have a datagrid, I just want to access the data generically. I get the row and column when I click a cell, so wanted to make a generic system to get the data. – Lonefish Dec 03 '15 at 09:04
  • @Lonefish Your code doesn't really show that intent at all. Realized after posting that you might be storing more than one record. – Yahya Dec 03 '15 at 09:06
  • My bad, my code example was a bit short, but I just wanted to know if it was possible. – Lonefish Dec 03 '15 at 09:08
1
var variablename = "v" + i;
MethodInfo method = product.GetType().GetMethod(variablename);
object result = method.Invoke(product, new object[] {}); // pass in the parameters if you need to

You can use reflection for that purpose

Kien Chu
  • 4,735
  • 1
  • 17
  • 31
  • IMO this is just horrible for what the op wants to Achieve. You would not really create a Class with 12 Properties called V1 .. V12 instead of an Array, and then access them using reflection instead of a Indexer would you? – quadroid Dec 03 '15 at 09:23
  • @Console: it really depends on what situation you are in. I agreed that using list of array is a safer choice here, but in some specific case (for example: you might want to save the methods inside database, load and execute them in C# code), you had to use reflection. Reflection is not bad at all, it has it own use case – Kien Chu Dec 03 '15 at 09:45
0

So you have a class Product that has 12 properties that got no Usefull name? In that case i would just create a class Product that has an Array of Properties.

public class Product
{
   public string[] Properties {get;set;}
}

Then to access the 5t generic nameless property of the product 23 you call.

products[23].Properties[5]

While I still doubt that the products properties can not be named properly.

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • As stated in the edit "It's more of an example case than it is actually needed, just want to figure out if it can be done." They can be named decently. I just want to figure out if it can be done. And apparently it's the case. – Lonefish Dec 03 '15 at 09:28