PDA

View Full Version : VB 2008 help with Arrays


mdnealy
12-12-2009, 12:46 AM
I am trying to put each of these items into three different text boxes on my form..later i will use various buttons to have it go to the first item then the next last and previous. I cannot get them to load when you pull the form up..help please....

Public Class Form1
'
' Dim packages As New List(Of Package)(9)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim packages() As Package = { _
New Package("F101", "Chicago", "IL"), _
New Package("F101", "Chicago", "IL"), _
New Package("F102", "Boston", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL"), _
New Package("F103", "Chicago", "IL")}

DisplayPackages(packages)


'packages.Add(New Package("F110", "Kansas City", "KS"))

End Sub
Sub DisplayPackages(ByVal packages As IEnumerable)
'txtOutPut.Text = ""
For Each pack= 1 to 9
txtPackageID.Text = pack
txtPackageCity.Text = pack
txtPackageState.Text = pack
Next (packages)
End Sub

Private Sub grpBoxPackage_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpBoxPackage.Enter

End Sub
End Class

Public Class Package
Public idValue As String
Public cityValue As String
Public stateValue As String

Public Sub New(ByVal id As String, ByVal city As String, ByVal state As String)
IdPack = id
CityPack = city
StatePack = state
End Sub
Public Property IdPack() As String
Get
Return idValue
End Get
Set(ByVal value As String)
idValue = value
End Set
End Property
Public Property CityPack() As String
Get
Return cityValue
End Get
Set(ByVal value As String)
cityValue = value
End Set
End Property
Public Property StatePack() As String
Get
Return stateValue
End Get
Set(ByVal value As String)
stateValue = value
End Set
End Property

sage45
12-12-2009, 02:57 AM
I think that you would need to do something like this (without even attempting to do this myself, I apologize, I just got through eating dinner and am feeling a little lazy)

Sub DisplayPackages(ByVal packages As IEnumerable)
'txtOutPut.Text = ""
For Each pack= 1 to 9
txtPackageID.Text = pack.IdPack
txtPackageCity.Text = pack.CityPack
txtPackageState.Text = pack.StatePack
Next (packages)
End Sub

HTH,

-saige-

mdnealy
12-12-2009, 02:04 PM
i will try that again but it didn't work..i will post again if it is not compiling

sage45
12-12-2009, 05:17 PM
Make sure you post any errors and associated code... Also, knowing about the buildout of the form will help us to recreate the form for testing purposes...

-saige-

mdnealy
12-13-2009, 12:20 AM
Okay, here is my form and codes for this program. I am only working on the left hand side of the form at this point. on the form1.vb form code, at the sub Next Packages i am getting a syntax error on "=" I am not sure why. I have the first and last buttons down...just working on the loop for the next and previous..any help would be appreciated..

Public Class Form1

Dim packages As New List(Of Package)(9)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load




packages.Add(New Package("F110", "St.Louis", "MO"))
packages.Add(New Package("F111", "Kansas City", "KS"))
packages.Add(New Package("F112", "Detroit", "MI"))
packages.Add(New Package("F113", "Kalamazoo", "MI"))
packages.Add(New Package("F114", "Wichita", "KS"))
packages.Add(New Package("F115", "Springfield", "MO"))
packages.Add(New Package("F116", "Columbia", "KS"))
packages.Add(New Package("F117", "Wichita", "KS"))
packages.Add(New Package("F118", "Kansas City", "KS"))
packages.Add(New Package("F119", "Kansas City", "KS"))
DisplayPackages()



'packages.Add(New Package("F110", "Kansas City", "KS"))

End Sub


Private Sub grpBoxPackage_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpBoxPackage.Enter

End Sub
Sub DisplayPackages()
Dim currPack As String
For Each pack In packages
lblId.Text = packages(currPack).IdPack
lblCity.Text = packages(currPack).CityPack
lblState.Text = packages(currPack).StatePack

Next

End Sub

Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
lblId.Text = ""
lblCity.Text = ""
lblState.Text = ""

lblId.Text = packages(0).IdPack
lblCity.Text = packages(0).CityPack
lblState.Text = packages(0).StatePack
End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'Dim currPack As String
'For Each pack In packages
' lblId.Text = packages(currPack).IdPack
' lblCity.Text = packages(currPack).CityPack + 1
' lblState.Text = packages(currPack).StatePack + 1

'Next



End Sub
Sub NextPackages(ByVal packages As IEnumerable)
'txtOutPut.Text = ""
For Each pack= 1 to 9
txtPackageID.Text = pack.IdPack
txtPackageCity.Text = pack.CityPack
txtPackageState.Text = pack.StatePack
Next (packages)
End Sub
End Class

Public Class Package
Public idValue As String
Public cityValue As String
Public stateValue As String

Public Sub New(ByVal id As String, ByVal city As String, ByVal state As String)
IdPack = id
CityPack = city
StatePack = state
End Sub
Public Property IdPack() As String
Get
Return idValue
End Get
Set(ByVal value As String)
idValue = value
End Set
End Property
Public Property CityPack() As String
Get
Return cityValue
End Get
Set(ByVal value As String)
cityValue = value
End Set
End Property
Public Property StatePack() As String
Get
Return stateValue
End Get
Set(ByVal value As String)
stateValue = value
End Set
End Property


End Class


on the form1 code, at the bottom i made a sub for NextPackage for the Next button in which it will loop and display each package separately...but i get that syntax error

sage45
12-13-2009, 10:12 AM
Currently I am getting the error

[Name 'pack' is not declared.]

Do you have pack declared in your application somewhere else??? Perhaps in Form1.Designer.vb???

-saige-