473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Object Oriented Question

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
Nov 20 '05 #1
2 1187
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/vbr...q/#installvdir

I hope this helps?

Cor

Nov 20 '05 #2
On 4 Jun 2004 23:57:12 -0700, Carlo wrote:
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

--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sandman | last post by:
Could anyone give me a tip about a good primer on object oriented php programming - why I should use it, the benefits, the drawbacks, the bugs, the glory? And, should I upgrade to php5 before...
2
by: Ricardo Batista | last post by:
I need that someone help me. I need to program the dijkstra algorithm by object oriented. I'll send my code. #!/usr/bin/env python # -*- encoding: latin -*- NIL =
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
11
by: Vishal Naidu | last post by:
i m a college student in my second year..... my queston is.. is it really possible to write object oriented code in C ? and if yes how do we achieve abstration, polymorhism , hierarchy etc. in C
2
by: ulrich schumacher | last post by:
Hi ASP.NET 2.0 Professionals, I've got a question concerning the new ASP.NET 2.0 DataBinding with object oriented business classes: Example: There are two business classes in my domain...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.