Answered by:
Using an array inside of a structure in Visual Basic

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
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
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 2:40 PM
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 4:36 PM
- Marked as answer by Youen ZenModerator Monday, May 28, 2012 9:23 AM
-
Use this instead:
ReDim LocalBookData.strGenres(5)
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 4:36 PM
- Marked as answer by Youen ZenModerator Monday, May 28, 2012 9:23 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 -
-
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
- Merged by Reed KimbleMVP, Moderator Thursday, May 17, 2012 4:35 PM duplicate post across forums
-
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
- Edited by Cor Ligthert Thursday, May 17, 2012 7:47 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 -
Use this instead:
ReDim LocalBookData.strGenres(5)
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 4:36 PM
- Marked as answer by Youen ZenModerator Monday, May 28, 2012 9:23 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
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 2:40 PM
- Proposed as answer by Reed KimbleMVP, Moderator Thursday, May 17, 2012 4:36 PM
- Marked as answer by Youen ZenModerator Monday, May 28, 2012 9:23 AM
-
-
...
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"