1

Is there a way to retrieve the name of an object stored in a list?

What I want to do is to add an object name - the name of matrix in this particular case - prior to printing out properties of that matrix.

internal class Program
{
    private static void Main(string[] args)
    {
        //Create a collection to help iterate later
        List<Matrix> list_matrix = new List<Matrix>();

        //Test if all overloads work as they should.
        Matrix mat01 = new Matrix();
        list_matrix.Add(mat01);

        Matrix mat02 = new Matrix(3);
        list_matrix.Add(mat02);

        Matrix mat03 = new Matrix(2, 3);
        list_matrix.Add(mat03);

        Matrix mat04 = new Matrix(new[,] { { 1, 1, 3, }, { 4, 5, 6 } });
        list_matrix.Add(mat04);

        //Test if all methods work as they should.     
        foreach (Matrix mat in list_matrix) 
        {
            //Invoking counter of rows & columns
            //HERE IS what I need - instead of XXXX there should be mat01, mat02...
            Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns", mat.countRows(), mat.countColumns()); 
        }
    }
}

In short I need here

Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns",
                  mat.countRows(),
                  mat.countColumns());

a method to write out name of that particular object - matrix.

joce
  • 9,624
  • 19
  • 56
  • 74
Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
  • Why not just create a string name property and give it a meaningful name instead of mat01, mat02 etc? – Bryan Crosby Mar 16 '13 at 19:18
  • Thank you for your comment, but I don't think that this is duplicate of question that you imply to. From my point of view getting variable name and object name is not the same, and I am not sure if their methods could be applied to mine question at all. – Nenad Bulatović Mar 16 '13 at 19:37
  • 1
    Hmm, you might want to.give the link a try though. Seems to be a way. Ill try in a bit (baby needs bottle :)) – bas Mar 16 '13 at 19:42

2 Answers2

3

Variable name as property

You can't retrieve the object reference name you 'once used' to declare the matrix. The best alternative I can think of is adding a string property Name to the Matrix and set it with the appropriate value.

    Matrix mat01 = new Matrix();
    mat01.Name = "mat01";
    list_matrix.Add(mat01);

    Matrix mat02 = new Matrix(3);
    mat02.Name = "mat02";
    list_matrix.Add(mat02);

That way you'd be able to output the names of the matrices

foreach (Matrix mat in list_matrix)
{
    Console.WriteLine("Matrix {0} has {1} Rows and {2} Columns", 
        mat.Name, 
        mat.countRows(), 
        mat.countColumns());
}

Alternative using Lambda expression

As mentioned by Bryan Crosby, there IS a way to get the variable name in code using lambda expressions, explained in this post. Here a small unit test that shows how you could apply it in your code.

    [Test]
    public void CreateMatrix()
    {
        var matrixVariableName = new Matrix(new [,] {{1, 2, 3,}, {1, 2, 3}});
        Assert.AreEqual("matrixVariableName", GetVariableName(() => matrixVariableName));
    }

    static string GetVariableName<T>(Expression<Func<T>> expr)
    {
        var body = (MemberExpression)expr.Body;

        return body.Member.Name;
    }

PS: do note his warning about performance penalties.

Community
  • 1
  • 1
bas
  • 13,550
  • 20
  • 69
  • 146
  • Thank you, as it seems it's the only proper way I'll do it like that. – Nenad Bulatović Mar 16 '13 at 19:39
  • @nenad I updated the answer, there is another way using lambda expressions. Works like a charm. Quite cool :). – bas Mar 16 '13 at 20:27
  • I will try it tomorrow, to tired right now. But I do not understand this sentence "PS: do not his warning about performance penalties." – Nenad Bulatović Mar 16 '13 at 20:44
  • 1
    @nenad typo :) not = note. this kind of constructions should not be used if performance is of high importance (according to his explanation) – bas Mar 16 '13 at 20:51
  • Also if, you are hesitant about creating a new class, you could use anonymous types. http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx – Tejas Sharma Mar 17 '13 at 00:42
2
int i=0;
foreach (Matrix mat in list_matrix) 
{
 i++;
Console.WriteLine("Matrix mat{0} has {1} Rows and {2} Columns", i, mat.countRows(), mat.countColumns()); 
 }
Ivan Marinin
  • 128
  • 3
  • 9