0

I'm trying to add an object to a List but whenever I go to check what's inside the List I get "SomeNamespace.SomeClass". I'm importing an Excel table into a 2D array and I'm passing its values to an object which I'm trying to add to a List. Here's the code (I'm excluding the part where I import the Excel file because I successfully retrieve everything I need, I just can't add it to a List):

            int rowNumber= xlRange.Rows.Count;
            int columnNumber= xlRange.Columns.Count;
            object[,] valueArray;

            List<SomeClass> list = new List<SomeClass>();

            valueArray = (object[,])xlRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

            for (int i = 2; i <= rowNumber; i++)
            {
                SomeClass objRequest = new SomeClass();

                objRequest.Field1 = valueArray[i, 1].ToString();
                objRequest.Field2 = valueArray[i, 2].ToString();
                objRequest.Field3 = valueArray[i, 3].ToString();
                objRequest.Field4 = valueArray[i, 4].ToString();
                objRequest.Field5 = valueArray[i, 5].ToString();
                objRequest.Field6 = valueArray[i, 6].ToString();
                objRequest.Field7 = valueArray[i, 7].ToString();
                list.Add(objRequest);
            }

            foreach (var item in list)
            {
                MessageBox.Show(item.ToString());
            }

I'm currently using VS 2010 if it can help.

4
  • * whenever I go to check what's inside the List I get "SomeNamespace.SomeClass"* is it in MessageBox? If you are seeing this string in message box then this is due to .ToString()(Which is not overridden). If you want to check specific field in message box then What is it? What is type of that field Commented Oct 16, 2019 at 7:04
  • 2
    Did you override SomeClass.ToString? Commented Oct 16, 2019 at 7:04
  • Have you overriden the ToString() function in SomeClass? If not you're using the default implementation, which gives Namespace.ClassName. Commented Oct 16, 2019 at 7:05
  • See learn.microsoft.com/en-gb/dotnet/api/… for more information on the .ToString()-Method and how to override it. Commented Oct 16, 2019 at 7:19

2 Answers 2

3

Thats because SomeClass doesnt override the ToString-method.

Try to override it inside SomeClass with the following method...

public override string ToString()
{
   return $"{Field1}, {Field2}"; //etc etc
}
Sign up to request clarification or add additional context in comments.

Comments

1

I can see you have 7 field in objRequest and then you want to toString() it? You can either toString() all 7 of them 1 by 1 or make a method to display all of them.

for (int i = 2; i <= rowNumber; i++)
{
    SomeClass objRequest = new SomeClass();

    objRequest.Field1 = valueArray[i, 1].ToString();
    objRequest.Field2 = valueArray[i, 2].ToString();
    objRequest.Field3 = valueArray[i, 3].ToString();
    objRequest.Field4 = valueArray[i, 4].ToString();
    objRequest.Field5 = valueArray[i, 5].ToString();
    objRequest.Field6 = valueArray[i, 6].ToString();
    objRequest.Field7 = valueArray[i, 7].ToString();
    list.Add(objRequest);
}

foreach (var item in list)
{
    MessageBox.Show(item.ToString());
}

Method 1: toString() All of them

foreach (var item in list)
{
    MessageBox.Show("Field 1: " + item.Field1.ToString() + "\n" +
            "Field 2: " + item.Field2.ToString() + "\n" +
            "Field 3: " + item.Field3.ToString() + "\n" +
            "Field 4: " + item.Field4.ToString() + "\n" +
            "Field 5: " + item.Field5.ToString() + "\n" +
            "Field 6: " + item.Field6.ToString() + "\n" +
            "Field 7: " + item.Field7.ToString()
    );
}

Method 2: Make a method to display all of them

public static string toStringAll()
{
    return "Field 1: " + Field1.ToString() + "\n" +
        "Field 2: " + Field2.ToString() + "\n" +
        "Field 3: " + Field3.ToString() + "\n" +
        "Field 4: " + Field4.ToString() + "\n" +
        "Field 5: " + Field5.ToString() + "\n" +
        "Field 6: " + Field6.ToString() + "\n" +
        "Field 7: " + Field7.ToString();
}

Method 3: Override toString() in SomeClass

public override string ToString()
{
    return "Field 1: " + Field1.ToString() + "\n" +
        "Field 2: " + Field2.ToString() + "\n" +
        "Field 3: " + Field3.ToString() + "\n" +
        "Field 4: " + Field4.ToString() + "\n" +
        "Field 5: " + Field5.ToString() + "\n" +
        "Field 6: " + Field6.ToString() + "\n" +
        "Field 7: " + Field7.ToString();
}

1 Comment

Most complete answer, thank you, worked. I'm new to c# and .NET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.