How to Assign Columns into a Dynamic ArrayList

Last post 07-06-2008 1:07 AM by Coool. 4 replies.

Sort Posts:

  • How to Assign Columns into a Dynamic ArrayList

    07-05-2008, 12:23 AM
    • Member
      9 point Member
    • TimBue
    • Member since 06-07-2008, 8:27 PM
    • Posts 77

    Hello all,

     

    I am trying to figure out how to setup a dynamic arraylist, in which the arraylist will be variable in size depending upon the row it retrieves.  In other word the question it retreives will have variable number of answers.  One question might have 3 answers another might have 4 answers.  The columns for unused answer choices will be assigned a null value. 

     

    I am using VB.NET 2005 through my ASP.NET page.  My main goal is basically this.

    Step 1.  Select Answer Columns from table where Answer value is not Null

    Step 2. Generate radiobutton listitems based on number of answer columns in the AnswersTable for the retrieved question.

            Step 2A: assign generated list items the values from the answerchoice columns.

    I mainly need help with steps 2 and 2A.  I was looking at possibly interrating with a for  i to x  Next loop, where x is the last answer column available in the array to be assigned to the listitem   If you need anymore clarification, please let me know.

  • Re: How to Assign Columns into a Dynamic ArrayList

    07-05-2008, 12:39 AM
    Answer
    • Member
      192 point Member
    • cvrajeesh
    • Member since 05-25-2006, 7:23 AM
    • Kerala
    • Posts 31

     ArrayList is dynamic by default. You can add any number of items, no need to specify the size when initializing.

    Since you are using .Net 2.0, you could go one step further i.e. use Generics there you have a List<T> with which you can ensure the type safety and improved performance when compared to ArrayList.

    Please let me know, did this helped 

    -----------------
    Rajeesh
    My Blog
  • Re: How to Assign Columns into a Dynamic ArrayList

    07-05-2008, 12:54 AM
    Answer
    • Contributor
      4,183 point Contributor
    • Coool
    • Member since 04-11-2007, 1:24 AM
    • Ahmedabad, India
    • Posts 648

    Hi TimBue,

    ArrayList is not sufficient and somehow eligent way to acomplish the task u want to do. U can use StringCollection for string Answers and Dictionary to store Question and Answers in StringCollection assuming that ur question strings are unique.

    Just look at the following demo.

     

      Dim strQuestionList As New Dictionary(Of String, System.Collections.Specialized.StringCollection)
            Dim strAnswerList As New System.Collections.Specialized.StringCollection()
            strAnswerList.Add("Answer 1")
            strAnswerList.Add("Answer 2")
            strAnswerList.Add("Answer 3")
            strAnswerList.Add("Answer 4")
            strQuestionList.Add("Question 1", strAnswerList)
    
            strAnswerList = New System.Collections.Specialized.StringCollection()
            strAnswerList.Add("Answer 1")
            strAnswerList.Add("Answer 2")
            strQuestionList.Add("Question 2", strAnswerList)
    
            Dim enumQuestionList As Dictionary(Of String, System.Collections.Specialized.StringCollection).Enumerator = strQuestionList.GetEnumerator()
            While enumQuestionList.MoveNext()
                Dim strQuestion As String = enumQuestionList.Current.Key 'This Question u can add to Form Dynamically or whatever way u want
                'U can also add this RadioButtonList in Form too!!!
                Dim rdo1 As New RadioButtonList
                For Each ans As String In enumQuestionList.Current.Value
                    rdo1.Items.Add(ans)
                Next
            End While
     
    Please mark as Answer if it helps u. Thanks!

    Parth Patel
    Techsture Technologies
    Software Developer
    Ahmedabad
  • Re: How to Assign Columns into a Dynamic ArrayList

    07-06-2008, 12:27 AM
    • Member
      9 point Member
    • TimBue
    • Member since 06-07-2008, 8:27 PM
    • Posts 77
    Thanks for the example, However in trying to define an instance of a dictionary in VB.NET 2005 through and ASP.NET 2.0 application, the system cannot seem to find the dictionary type.  Only the DictionaryEntry, and DictionarySectionHandler types are available and does not allow for parameters.  I was wondering you could recommend a remedy to this situation.
  • Re: How to Assign Columns into a Dynamic ArrayList

    07-06-2008, 1:07 AM
    Answer
    • Contributor
      4,183 point Contributor
    • Coool
    • Member since 04-11-2007, 1:24 AM
    • Ahmedabad, India
    • Posts 648

    Sorry for late reply TimBue,

    Well Dictionary Class is defined under System.Collections.Generic Namespace.

    So u can use either Imports at the top of the Page or Directly use

    Dim strQuestionList As New System.Collections.Generic.Dictionary(Of String, System.Collections.Specialized.StringCollection)

    Please mark as Answer if it helps u. Thanks!

    Parth Patel
    Techsture Technologies
    Software Developer
    Ahmedabad
Page 1 of 1 (5 items)