C
Carlo
Hi
I am going through question in my textbook(An Introduction to
programming using Visual Basic .NET by David I Schneider) and I am
going around in circles on one of the simple questions in the chapter
dealing with Object Oriented Programming and more specifically with
the ‘array of objects'.
I have a text file containing the name of the
state,abbreviation,date,area,population size.
The program below does go through all the lines correctly and displays
them in the first lstOut.Items.add statement.
What I then wanted to do is to display the 4 state name in the txtIn
box using the statement txtState.Text = dstate(4).name but it only
displays the very last state in the text file. What am I doing wrong?
Also, I want to have a textbox where a person inputs the name of the
state and it only displays the population of that state in the list
box. I thought the statement if state.name = txtIn.text then
lstOut.items.add(state.pop) would work.
But it doesn't find it.
Also, if there anywhere where I can get all the solutions to the
problems in the textbook (the textbook only has the odd number
solutions and for object oriented it only has 2 solutions)
Thanks
Carlob1
Dim dstates(50) As states
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim state As New states()
Dim name_in As String
Dim i As Integer
Dim sr As IO.StreamReader = IO.File.OpenText("C:\STATES.TXT")
name_in = txtIn.Text
name_in = name_in.ToUpper
For i = 1 To 50
state.name = sr.ReadLine
state.name = state.name.ToUpper
state.abbr = sr.ReadLine
state.I_date = sr.ReadLine
state.area = sr.ReadLine
state.pop = sr.ReadLine
dstates(i) = state
lstOut.Items.Add(dstates(i).name & " " & i)
Next
txtState.Text = dstates(4).name
End Sub
End Class
Class states
Private m_name As String
Private m_abbrev As String
Private m_date As String
Private m_area As Double
Private m_pop As Double
Public Property name() As String
Get
Return m_name
End Get
Set(ByVal Value As String)
m_name = Value
End Set
End Property
Public Property abbr() As String
Get
Return m_abbrev
End Get
Set(ByVal Value As String)
m_abbrev = Value
End Set
End Property
Public Property I_date() As String
Get
Return m_date
End Get
Set(ByVal Value As String)
m_date = Value
End Set
End Property
Public Property area() As Double
Get
Return m_area
End Get
Set(ByVal Value As Double)
m_area = Value
End Set
End Property
Public Property pop() As Double
Get
Return m_pop
End Get
Set(ByVal Value As Double)
m_pop = Value
End Set
End Property
End Class
I am going through question in my textbook(An Introduction to
programming using Visual Basic .NET by David I Schneider) and I am
going around in circles on one of the simple questions in the chapter
dealing with Object Oriented Programming and more specifically with
the ‘array of objects'.
I have a text file containing the name of the
state,abbreviation,date,area,population size.
The program below does go through all the lines correctly and displays
them in the first lstOut.Items.add statement.
What I then wanted to do is to display the 4 state name in the txtIn
box using the statement txtState.Text = dstate(4).name but it only
displays the very last state in the text file. What am I doing wrong?
Also, I want to have a textbox where a person inputs the name of the
state and it only displays the population of that state in the list
box. I thought the statement if state.name = txtIn.text then
lstOut.items.add(state.pop) would work.
But it doesn't find it.
Also, if there anywhere where I can get all the solutions to the
problems in the textbook (the textbook only has the odd number
solutions and for object oriented it only has 2 solutions)
Thanks
Carlob1
Dim dstates(50) As states
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim state As New states()
Dim name_in As String
Dim i As Integer
Dim sr As IO.StreamReader = IO.File.OpenText("C:\STATES.TXT")
name_in = txtIn.Text
name_in = name_in.ToUpper
For i = 1 To 50
state.name = sr.ReadLine
state.name = state.name.ToUpper
state.abbr = sr.ReadLine
state.I_date = sr.ReadLine
state.area = sr.ReadLine
state.pop = sr.ReadLine
dstates(i) = state
lstOut.Items.Add(dstates(i).name & " " & i)
Next
txtState.Text = dstates(4).name
End Sub
End Class
Class states
Private m_name As String
Private m_abbrev As String
Private m_date As String
Private m_area As Double
Private m_pop As Double
Public Property name() As String
Get
Return m_name
End Get
Set(ByVal Value As String)
m_name = Value
End Set
End Property
Public Property abbr() As String
Get
Return m_abbrev
End Get
Set(ByVal Value As String)
m_abbrev = Value
End Set
End Property
Public Property I_date() As String
Get
Return m_date
End Get
Set(ByVal Value As String)
m_date = Value
End Set
End Property
Public Property area() As Double
Get
Return m_area
End Get
Set(ByVal Value As Double)
m_area = Value
End Set
End Property
Public Property pop() As Double
Get
Return m_pop
End Get
Set(ByVal Value As Double)
m_pop = Value
End Set
End Property
End Class