1

I'm trying to add objects of DataPerLabel to my Arraylist allData, following the code of DataPerLabel:

class DataPerLabel
{
    public String labelName;
    public String labelAdress;
    public String dataType;

    public DataPerLabel(String labelName, String labelAdress, String dataType)
    {
        this.labelName = labelName;
        this.labelAdress = labelAdress;
        this.dataType = dataType;
    }

    public String getLabelName()
    {
        return labelName;
    }

    public String getLabelAdress()
    {
        return labelAdress;
    }

    public String getDataType()
    {
        return dataType;
    }
}

In the following code I try to add the objects of DataPerLabel to my arraylist:

submitButton.Click += (sender, args) =>
{
    String label = textboxLabel.Text;
    String adress = textboxAdress.Text;
    String dataType = "hey";

    if (buttonsLabelBool.Checked)
    {
        dataType = "Bool";
    }
    else if (buttonsLabelReal.Checked)
    {
        dataType = "Real";
    }
    else if (buttonsLabelInt.Checked)
    {
        dataType = "Int";
    }
    allData.Add(new DataPerLabel(label, adress, dataType));
};

And finally I try to read out the arrayList by displaying it in a textbox, see the following code:

private void test()
{
    Button btn = new Button();
    btn.Location = new Point(500,500);
    btn.Text = "test";
    btn.Click += (sender, args) =>
    {
        foreach (var item in allData)
        {
            //Display arraylist per object here
            //Something like : item.getLabelName();
        }
    };
}

I'm not sure what I'm doing wrong, hope you can help!

5
  • 3
    Well what is going wrong? Commented Sep 18, 2017 at 9:14
  • 3
    Use List<DataPerLabel> instead of ArrayList. You can solve the problem by casting item, but why cast if you can use List<T>. Commented Sep 18, 2017 at 9:16
  • 1
    Agree with Sinatr. Using List<T> allows you to directly access the members of your current item. With ArrayList you´d have to cast every item to DataPerLabel. Commented Sep 18, 2017 at 9:18
  • Visual studio is not giving me an option to do item.getLabelName and gives the error: Error CS1061 'object' does not contain a definition for 'getLabelName' and no extension method 'getLabelName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Commented Sep 18, 2017 at 9:19
  • use List<T> requires no upcasting/downcasting Commented Sep 18, 2017 at 9:20

2 Answers 2

7

ArrayList stores a list of System.Object. You need to cast the object back to DataPerLabel as follows:

foreach (var item in allData)
{
    ((DataPerLabel)item).getLabelName();
}

Alternatively, you could specify the data type in the foreach instead of var as Jakub Dąbek pointed out in the comment as follows:

foreach (DataPerLabel item in allData)
{
    item.getLabelName();
}

A better approach would be to use generic list/collection List<DataPerLabel> to store the data so that the casting can be avoided.

Sign up to request clarification or add additional context in comments.

1 Comment

You can just do the cast automatically by specifying the type of the variable in foreach like foreach(DataPerLabel item in allData) even while using ArrayList. Nevertheless List<T> is just better
1

Yiu should use a List<T> instead of ArrayList. This way every item in your list has the right type already and you can access the members:

foreach (DataPerLabel item in allData)
{
    item.GetLabelItem();
}

This assumes allData is defined like this:

allData = new List<DataPerLabel>();

instead of allData = new ArrayList()

If you really have to use an ArrayList than you should cast your item to the actual type. The code above actually does this allready. However you could also use this:

foreach (var item in allData)
{
    ((DataPerLabel)item).GetLabelItem();
}

Comments

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.