Object Oriented Question

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
 
C

Cor Ligthert

Hi Carlo,

Mostly a writer of books has the problem that he has to tell everything step
by step, which can mean that not everything is done as it could have been
done.

It is to make you understand what is been going. However, samples in books
are often not so well usable in practical situations, because the writer
could not add things he did not yet teach at that time.

A good resource for that is the recourse kit; however try first reading your
book to understand what is written, than using that recourse kit will lead
you to the next step in my opinion.

VB.net Resource kit
http://msdn.microsoft.com/vbasic/vbrkit/default.aspx

And if you have problems installing the resource kit
http://msdn.microsoft.com/vbasic/vbrkit/faq/#installvdir

I hope this helps?

Cor
 
C

Chris Dunaway

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?

The problem is that you only created ONE instance of the states class when
you needed to create 50!
Dim dstates(50) As states

BTW: The line above creates a 51 element array (0-50).
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim state As New states()

The line above should be:

Dim state As states

Notice that you don't need New yet!
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

Now, inside the For loop, you create each new state:

state = New states()
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

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top