none
Using an array inside of a structure in Visual Basic RRS feed

  • Question

  • I'm trying to create a structure and inside it put an array of "Genres". 

    This is my structure declaration:

    Structure BookData        Dim strGenres() As String     

    Dim strTitle, strAuthor1, strAuthor2, strAuthor3, strPublisher As String       

    Dim intBookID, intYearPublished, intNumberOfCopies As Integer        Dim sngBuyingPrice, sngSellingPrice As Single    End Structure


    And this is where I am trying to call the structure

    Public Sub btnSaveDetails_Click()       

    Dim LocalBookData As BookData        Dim LocalBookData.strGenres(5)        LocalBookData.strGenres(0) = "Hello" End Sub


    But I keep getting an error with the .strGenres(5). VB is telling me "End of statement expected"

    I don't know where I'm going wrong! I'm basing this off code given to us in the examples and I'm following it perfectly so I don't know what the issue is.

    Thanks



    • Edited by Mike2.0 Thursday, May 17, 2012 7:19 AM
    Thursday, May 17, 2012 7:18 AM

Answers

  • Try using ReDim instead of Dim.

    Also, are you sure using a structure is the best way to go? Structures are value types, and you have declared an array within that type. If you copy the structure they will both share the same array with the same values (if you change the value in one structure, it will be reflected in the other structure).

    I know you stated this is an example, but this is one of the fundamental points that should be discussed very early regarding structures, classes, value types, reference types.


    Stephen J Whiteley

    Thursday, May 17, 2012 12:28 PM
    Moderator
  • Use this instead:

      ReDim LocalBookData.strGenres(5)

    Thursday, May 17, 2012 9:37 AM

All replies

  • Mike

    This forum is not for vintage VB try this link to see what to do

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b


    Success
    Cor

    Thursday, May 17, 2012 7:22 AM
  • What exactly is vintage VB? I'm using visual studio 2010?
    Thursday, May 17, 2012 7:25 AM
  • I'm trying to create a structure and inside it put an array of "Genres". 

    This is my structure declaration:

    Structure BookData        Dim strGenres() As String     

    Dim strTitle, strAuthor1, strAuthor2, strAuthor3, strPublisher As String       

    Dim intBookID, intYearPublished, intNumberOfCopies As Integer        Dim sngBuyingPrice, sngSellingPrice As Single    End Structure

    And this is where I am trying to call the structure

    Public Sub btnSaveDetails_Click()       

    Dim LocalBookData As BookData        Dim LocalBookData.strGenres(5)        LocalBookData.strGenres(0) = "Hello" End Sub

    But I keep getting an error with the .strGenres(5). VB is telling me "End of statement expected"

    I don't know where I'm going wrong! I'm basing this off code given to us in the examples and I'm following it perfectly so I don't know what the issue is.

    Thanks


    Thursday, May 17, 2012 7:26 AM
  • What exactly is vintage VB? I'm using visual studio 2010?

    In modern VB  your way for a button click

    Public Sub btnSaveDetails_Click()        
    Dim LocalBookData As BookData        
    Dim LocalBookData.strGenres(5)       
     
    LocalBookData.strGenres(0) = "Hello"
    
    End Sub

    does not exist, it should be at least

        Public Sub btnSaveDetails_Click(sender As Object, e As EventArgs)
            Dim LocalBookData As BookData
            Dim LocalBookData.strGenres(5)       
            LocalBookData.strGenres(0) = "Hello"
        End Sub
    But normally is also the handler added to the method


    Success
    Cor



    Thursday, May 17, 2012 7:45 AM
  • It is the second, I just cut out the excess stuff when I pasted (thought it was irrelevant sorry).

    Direct c/p

    Public Sub btnSaveDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveDetails.Click

    Thursday, May 17, 2012 7:57 AM
  • If you are converting you can keep things the same.

    However, if you make things new then don't use old vintage VB stuff. The Struct is an piece of memory on the Top Stack. Very cheap to use if it is less then 16bytes but very expensive as soon as it becomes more of that. 

    Use then Classes.

    However, in your case there is not even need for that. 

    So you can create a class like this

        Public Class BookData
            Public Property Genres As List(Of String)
            Public Property Title As String
        End Class
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim Book As New BookData
            Book.Genres = New List(Of String) From {"Art", "History"}
            Book.Title = "Whatever"
        End Sub


    Success
    Cor

    Thursday, May 17, 2012 8:51 AM
  • Use this instead:

      ReDim LocalBookData.strGenres(5)

    Thursday, May 17, 2012 9:37 AM
  • Try using ReDim instead of Dim.

    Also, are you sure using a structure is the best way to go? Structures are value types, and you have declared an array within that type. If you copy the structure they will both share the same array with the same values (if you change the value in one structure, it will be reflected in the other structure).

    I know you stated this is an example, but this is one of the fundamental points that should be discussed very early regarding structures, classes, value types, reference types.


    Stephen J Whiteley

    Thursday, May 17, 2012 12:28 PM
    Moderator
  • Please do not double post... merging with original thread.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, May 17, 2012 4:34 PM
    Moderator
  • ...

    Also, are you sure using a structure is the best way to go? Structures are value types, and you have declared an array within that type. If you copy the structure they will both share the same array with the same values (if you change the value in one structure, it will be reflected in the other structure).

    I know you stated this is an example, but this is one of the fundamental points that should be discussed very early regarding structures, classes, value types, reference types.


    Stephen J Whiteley

    Mike,

    Please pay close attention to this...  you probably want this object defined as a class.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, May 17, 2012 4:37 PM
    Moderator