By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,480 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,480 IT Pros & Developers. It's quick & easy.

Nwwbie array question

P: n/a
Got this assignment in my VB .NET class. The program's basically a picture
viewer that lets you add your pictures to an array so you can cycle through
them once the file's been opened. So you open the file, it displays, then
you can select "add to list" from a menu. Anyway, I'm pretty sure the
filenames are being added to the array, and I can go directly to the first
and last images in the array by using the appropriate menu options, but I
can't get my "next" and "previous" options to work. I figured it'd be a
simple matter of incrementing or decrementing the array counter, and opening
the file who's name is stored at that position. Apparently not.

Here's the relevant code:

Private Sub mnuBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuBrowse.Click

' Opens an image file to be displayed. Works fine.

With opdPictureBox
.Title = "Select a Car to View"
.InitialDirectory = "C:\CarDealer\pictures"
.CheckFileExists = True
.Filter = "Picture Files (*.jpg)|*.jpg|All Files (*.*)|*.*)"
.ShowDialog()
picDisplay.Image = Image.FromFile(.FileName)
strCarName = .FileName
stbInfo.Text = strCarName
End With
End Sub

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click

' Adds image path and filename to array. seems to work.

strShortList(index) = strCarName
index = index + 1
End Sub

Private Sub mnuPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPrevious.Click

' Views previous image in array... doesn't work

If i > 0 Then
viewCar = i - 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(0))
End If
End Sub

Private Sub mnuNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuNext.Click

' views next image in array... doesn't work

If i < 9 Then
viewCar = i + 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(i - 1))
End If
End Sub

Nov 25 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Paul in Toronto wrote:
Got this assignment in my VB .NET class. The program's basically a picture
viewer that lets you add your pictures to an array so you can cycle through
them once the file's been opened. So you open the file, it displays, then
you can select "add to list" from a menu. Anyway, I'm pretty sure the
filenames are being added to the array, and I can go directly to the first
and last images in the array by using the appropriate menu options, but I
can't get my "next" and "previous" options to work. I figured it'd be a
simple matter of incrementing or decrementing the array counter, and opening
the file who's name is stored at that position. Apparently not.

Here's the relevant code:

Private Sub mnuBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuBrowse.Click

' Opens an image file to be displayed. Works fine.

With opdPictureBox
.Title = "Select a Car to View"
.InitialDirectory = "C:\CarDealer\pictures"
.CheckFileExists = True
.Filter = "Picture Files (*.jpg)|*.jpg|All Files (*.*)|*.*)"
.ShowDialog()
picDisplay.Image = Image.FromFile(.FileName)
strCarName = .FileName
stbInfo.Text = strCarName
End With
End Sub

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click

' Adds image path and filename to array. seems to work.

strShortList(index) = strCarName
index = index + 1
End Sub

Private Sub mnuPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPrevious.Click

' Views previous image in array... doesn't work

If i > 0 Then
viewCar = i - 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(0))
End If
End Sub

Private Sub mnuNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuNext.Click

' views next image in array... doesn't work

If i < 9 Then
viewCar = i + 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(i - 1))
End If
End Sub


Where do you store the value of i? Where do you define viewCar? Are
you sure that your index variable is being remembered from click to
click? Make sure you do "option strict on" and "option explict on" at
the top of your file.

A better way to write this might be:
You: If i < 9 Then
Better?: If i < strShortList.getupperbound(0) Then

Nov 26 '05 #2

P: n/a

"I Don't Like Spam" <no@spam.com> wrote in message
news:Ov*************@TK2MSFTNGP15.phx.gbl...
Where do you store the value of i? Where do you define viewCar? Are you
sure that your index variable is being remembered from click to click?
Make sure you do "option strict on" and "option explict on" at the top of
your file.
i is a global integer variable, viewCar is the same. I'm using them as
array indexes. The reason I'm using viewCar is so that i doesn't get reset,
and always points to the net free space in the array. Or that's the idea,
anyway. both options you mentioned are on.
A better way to write this might be:
You: If i < 9 Then
Better?: If i < strShortList.getupperbound(0) Then


What does getupperbound do?
Nov 26 '05 #3

P: n/a

"Paul in Toronto" <pf********@look.ca> wrote in message news:eR*******************@news20.bellglobal.com.. .

"I Don't Like Spam" <no@spam.com> wrote in message news:Ov*************@TK2MSFTNGP15.phx.gbl...
Where do you store the value of i? Where do you define viewCar? Are you sure that your index variable is being
remembered from click to click? Make sure you do "option strict on" and "option explict on" at the top of your file.


i is a global integer variable, viewCar is the same. I'm using them as array indexes. The reason I'm using viewCar
is so that i doesn't get reset, and always points to the net free space in the array. Or that's the idea, anyway.
both options you mentioned are on.
A better way to write this might be:
You: If i < 9 Then
Better?: If i < strShortList.getupperbound(0) Then


What does getupperbound do?


It gets the upper bound of the array. If the array is defined as
having 16 elements, then the upper bound would be 15,
because arrays are zero-based by default. If you set your
own base then the upper and lower bounds will be offset
by that base. E.g., base 1 would make the lower bound 1
and the upper bound 16, in a 16 element array.

Personally, I think you'd be better off using a Collection class.
Better still, use a proper List control and let the user navigate
the list itself rather than use menu options to navigate an otherwise
invisible list of items. There's no point maintaining a separate
array when a list can easily update itself.

Nov 26 '05 #4

P: n/a
dont you want to use 'index' instead of i?

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click

' Adds image path and filename to array. seems to work.

strShortList(index) = strCarName
index = index + 1
End Sub

Private Sub mnuPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrevious.Click

' Views previous image in array... doesn't work

If i > 0 Then
viewCar = i - 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(0))
End If
End Sub

Private Sub mnuNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuNext.Click

' views next image in array... doesn't work

If i < 9 Then
viewCar = i + 1
picDisplay.Image = Image.FromFile(strShortList(viewCar))
Else
picDisplay.Image = Image.FromFile(strShortList(i - 1))
End If
End Sub

Nov 26 '05 #5

P: n/a
What is the error youre getting?

The string in vb.net is different than in C, if Im not mistaken the string
holds an array of pointers to type safe references to type String
"Paul in Toronto" <pf********@look.ca> wrote in message
news:eR*******************@news20.bellglobal.com.. .

"I Don't Like Spam" <no@spam.com> wrote in message
news:Ov*************@TK2MSFTNGP15.phx.gbl...
Where do you store the value of i? Where do you define viewCar? Are you sure that your index variable is being remembered from click to click?
Make sure you do "option strict on" and "option explict on" at the top of your file.
i is a global integer variable, viewCar is the same. I'm using them as
array indexes. The reason I'm using viewCar is so that i doesn't get

reset, and always points to the net free space in the array. Or that's the idea,
anyway. both options you mentioned are on.
A better way to write this might be:
You: If i < 9 Then
Better?: If i < strShortList.getupperbound(0) Then


What does getupperbound do?

Nov 26 '05 #6

P: n/a
"Micky" <mi***@n05pam.com> wrote in message
news:dm**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...

Personally, I think you'd be better off using a Collection class.
Better still, use a proper List control and let the user navigate
the list itself rather than use menu options to navigate an otherwise
invisible list of items. There's no point maintaining a separate
array when a list can easily update itself.


Unfortunately, the assignment calls for an array, which you navigate using a
bunch of menu options (next image, previous image, first image, last image).
A collection or a List control would make sense, but that's not what the
assignment's about.
Nov 26 '05 #7

P: n/a
Hi Paul,

I am not sure of this, however I thought that you had to set your image to
nothing before you could add a new image.

Cor
Nov 26 '05 #8

P: n/a
Hi,

"Paul in Toronto" <pf********@look.ca> wrote in message
news:11********************@news20.bellglobal.com. ..
Got this assignment in my VB .NET class. The program's basically a
picture
viewer that lets you add your pictures to an array so you can cycle
through
them once the file's been opened. So you open the file, it displays, then
you can select "add to list" from a menu. Anyway, I'm pretty sure the
filenames are being added to the array, and I can go directly to the first
and last images in the array by using the appropriate menu options, but I
can't get my "next" and "previous" options to work. I figured it'd be a
simple matter of incrementing or decrementing the array counter, and
opening
the file who's name is stored at that position.


You have both "i" & "viewCar". "i" isn't increasing nor decreasing and
"viewcar" is only one less or one more then i. I doubt you need two
indexes, just use one. If the user is at the last picture (or the first)
then you should do nothing.

And Cor is right, you need to Dispose the old picture before loading a new
one ( see LoadImage ).

See corrections in code:

Private count As Integer = 0
Private idx As Integer = 0

Private Sub mnuBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuBrowse.Click

' Opens an image file to be displayed. Works fine.

With opdPictureBox
.Title = "Select a Car to View"
.InitialDirectory = "C:\CarDealer\pictures"
.CheckFileExists = True
.Filter = "Picture Files (*.jpg)|*.jpg|All Files (*.*)|*.*)"
.ShowDialog()
LoadImage( .FileName )
strCarName = .FileName
stbInfo.Text = strCarName
End With
End Sub

Private Sub mnuAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuAdd.Click
' Adds image path and filename to array. seems to work.
strShortList(count) = strCarName
count = count + 1
End Sub

Private Sub mnuPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPrevious.Click
If idx > 0 Then
idx = idx - 1
LoadImage( strShortList(idx) )
End If
End Sub

Private Sub mnuNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuNext.Click
If idx < count-2 Then
idx = idx + 1
LoadImage( strShortList(idx) )
End If
End Sub

Private Sub LoadImage( string FileName )
Dim oldImage As Image = picDisplay.Image
If ( Not oldImage Is Nothing ) Then
picDisplay.Image = Nothing
oldImage.Dispose
End If
picDisplay.Image = Image.FromFile( FileName )
End Sub

HTH,
Greetings
Nov 26 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.