locked
Very simple question, about lists RRS feed

  • Question

  • Hello Everybody, 

    It is simple but I do not know why I get an error.

    1. I defined a structure with 2 fields.

    2. I defined a list (my_list) of that structure.

    3. I Created 2 cell of the list.

    When I want to change a field of second Cell of(my_list),

    I get an error.

    -----------------------------

    Class Form1

        Structure Str_Rec       ' String record

            Dim Str As String

            Dim Loc As String

        End Structure

       Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

            Dim my_list As New List(Of Str_Rec)

              Dim Tmp As Str_Rec

            Tmp.Str = "ABC"

            Tmp.Loc = "1"

            my_list.Add(Tmp)

            Tmp.Str = "DEF"

            Tmp.Loc = "2"

            my_list.Add(Tmp)

            my_list(1).Loc = "HHHH"   ‘ DOES NOT WORK. I GET AN ERROR.

        End Sub

    End Class

     my_list(1).Loc = "HHHH"  Creates an error. Why is it, and how to overcome 

                                                The problem?

    Thanks to all helpers.

       Moshe.


    All things are difficult before they are easy. (unknown)

    Saturday, June 20, 2020 5:13 PM

Answers

  • Hi

    An alternative which works

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
      Class Str_Rec
        Property Str As String
        Property Loc As String
      End Class
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim my_list As New List(Of Str_Rec)
        Dim Tmp As New Str_Rec
        Tmp.Str = "ABC"
        Tmp.Loc = "1"
        my_list.Add(Tmp)
        Tmp.Str = "DEF"
        Tmp.Loc = "2"
        my_list.Add(Tmp)
        my_list(1).Loc = "HHHH"
      End Sub
    End Class
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Head_one Saturday, June 20, 2020 6:47 PM
    Saturday, June 20, 2020 5:23 PM

All replies

  • Hi

    An alternative which works

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
      Class Str_Rec
        Property Str As String
        Property Loc As String
      End Class
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim my_list As New List(Of Str_Rec)
        Dim Tmp As New Str_Rec
        Tmp.Str = "ABC"
        Tmp.Loc = "1"
        my_list.Add(Tmp)
        Tmp.Str = "DEF"
        Tmp.Loc = "2"
        my_list.Add(Tmp)
        my_list(1).Loc = "HHHH"
      End Sub
    End Class
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Head_one Saturday, June 20, 2020 6:47 PM
    Saturday, June 20, 2020 5:23 PM
  • hi

    Public Class Form1
        Structure Str_Rec
            Dim str As String
            Dim loc As String
        End Structure
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim list As New List(Of Str_Rec)
            Dim l2 As Str_Rec
            Dim l3 As Str_Rec
            l2.str = "ABC"
            l2.loc = "1"
            list.Add(l2)
            l3.str = "DEF"
            l3.loc = "2"
            list.Add(l3)
    
     Dim alist = list.ToArray()
            alist(1).loc = "HHHH"
            MessageBox.Show(alist(1).loc)
    
            ' Part 2: loop through List elements.
            For Each number As Str_Rec In list
                MessageBox.Show(number.str)
            Next
        End Sub
    End Class

    Thanks and Regrds
    Laxmidhar sahoo


    Saturday, June 20, 2020 5:34 PM
  • Hello Les,

      Yes. It works.

     The only difference between mine and yours is that

    I defined a Structure and you have defines a class.

       Anyway, thank you very much for your help.

                               Moshe.


    All things are difficult before they are easy. (unknown)

    Saturday, June 20, 2020 6:50 PM
  • .

     The only difference between mine and yours is that

    I defined a Structure and you have defines a class.


    Because a Structure is a value type, the expression my_list(1).Loc returns the
    value in a temporary variable. Hence changing it will not have any effect on
    the element in the list. To overcome this, retrieve the element to be changed 
    from the list into a variable, change the value of Loc in that variable, then 
    replace the element in the list with that variable:

    Structure Str_Rec       ' String record
        Dim Str As String
        Dim Loc As String
    End Structure
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim my_list As New List(Of Str_Rec)
        Dim Tmp As Str_Rec
        Tmp.Str = "ABC"
        Tmp.Loc = "1"
        my_list.Add(Tmp)
        Tmp.Str = "DEF"
        Tmp.Loc = "2"
        my_list.Add(Tmp)
        'my_list(1).Loc = "HHHH"   ' DOES NOT WORK. I GET AN ERROR.
    
        Tmp = my_list(1)
        Tmp.Loc = "HHHH"
        my_list(1) = Tmp
    
    End Sub
    

    Unlike a Structure, a Class is a reference type.

    - Wayne

    Saturday, June 20, 2020 7:18 PM
  • If the structure cannot be changed to class, then try replacing

       my_list(1).Loc = "HHHH"   ' DOES NOT WORK.

    with

       ChangeLoc(my_list(1), "HHHH")

    where ChangeLoc is:

    sub ChangeLoc(ByRef s As Str_Rec, newLoc As String)

        s.Loc = newLoc

    End Sub

    You can also write a Sub that sets more fields.



    • Edited by Viorel_MVP Saturday, June 20, 2020 8:16 PM
    Saturday, June 20, 2020 8:14 PM
  • Hello Wayne

      Thanks for your explanation.

       Yesterday, Les from Livingston Scotland, solved me the problem.

        Defining a class instead of a structure. 

      You gave me the explanation, why a class works and a structure does not.

                   Thank you for this and all the best.

                               Moshe.


    All things are difficult before they are easy. (unknown)

    Sunday, June 21, 2020 5:50 AM
  • Hello Mr. sahoo

       Thank you very much for your answer.

      Another solution is to define a class instead  of a structure,

      and everything works.

        

    Class Str_Rec Property Str As String Property Loc As String End Class

    As Les from Scotland adviced me.

    Thanks for your help.

    Moshe.


    All things are difficult before they are easy. (unknown)

    Sunday, June 21, 2020 5:59 AM
  •  Hello Viorel,

       Thanks for you help.

          Moshe.


    All things are difficult before they are easy. (unknown)

    Sunday, June 21, 2020 6:01 AM