none
Unable to set Structure element via CallByName() RRS feed

  • Question

  • I'm so confused.  I am trying to set the elements of an instance of a structure by using a variable name that is gotten from a list of strings.  If I use the vbGet option I am able to get the contents just fine, but when I use vbLet, or vbSet to try to set the value it doesn't seem to do anything.

    Example code:

    Public Structure TestStructure
        Public Column1 As String
        Public Column2 As String
    End Structure
    Public Class Form1
        Public ListofStructure As New List(Of TestStructure)
        Public StructureRow As New TestStructure
        Public ListofColumns As New List(Of String)

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

            ListofColumns.Add("Column1")
            ListofColumns.Add("Column2")

            StructureRow.Column1 = "default"
            StructureRow.Column2 = "default"

            For Each ColumnName In ListofColumns
                CallByName(StructureRow, ColumnName, vbLet, "test")
            Next

            MessageBox.Show(StructureRow.Column1 & ", " & StructureRow.Column2)

        End Sub
    End Class

    When I run this code I get a mesagebox with the text "default, default" instead of "test, test" however, if I change it to vbGet, I can read the values of "default" using the CallByName() function with no problem.

    I must be missing something, but I have no idea what it is.  Help?

    Wednesday, December 12, 2018 10:47 PM

Answers

  • Sorry to inform you that setting a item in a structure does not work (as you have noticed) with structures. Now if we were talking a class instance then set works as expected, same for controls on a form. I looked at the source code but it's a seal class, not access as with much of the .NET source code.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BrandenBass Thursday, December 13, 2018 2:16 AM
    Thursday, December 13, 2018 12:50 AM
    Moderator

All replies

  • Sorry to inform you that setting a item in a structure does not work (as you have noticed) with structures. Now if we were talking a class instance then set works as expected, same for controls on a form. I looked at the source code but it's a seal class, not access as with much of the .NET source code.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by BrandenBass Thursday, December 13, 2018 2:16 AM
    Thursday, December 13, 2018 12:50 AM
    Moderator
  • Thank you so much!
    Thursday, December 13, 2018 2:16 AM
  • Thank you so much!
    Your welcome :-)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 13, 2018 2:27 AM
    Moderator