Arraylist casting

Results 1 to 2 of 2

Thread: Arraylist casting

  1. #1
    Join Date
    Dec 1969

    Default Arraylist casting

    Is there a better way to do this:<BR><BR>ArrayList btnBasic = new ArrayList(); <BR>btnBasic.Add(new Button());<BR>((Button) btnBasic[0]).Text = "B";<BR><BR>basically, without the cast (Button) VB.Net treats btnBasic[0] as an Object. If there isn&#039;t a better way, does it mean I need to cast each time I want to set/get a specific property for the button at a given index?<BR><BR>Karl

  2. #2
    Join Date
    Dec 1969

    Default RE: Arraylist casting

    If you don&#039;t cast it to the Button type, there&#039;s no way for the compiler to know that the stuff inside the array list that you&#039;re dealing with are of button type. An alternative is to create a class that contains the array list and have it return a button object casted from the object inside the array list.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts