473,405 Members | 2,421 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,405 software developers and data experts.

Nwwbie array question

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
8 1216
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

"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

"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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: eft0 | last post by:
Hi there, the question: I have this array: var Thumbnails = new Array(); Thumbnails = new Array('1', 'listing_34.jpg', 'Home View'); Thumbnails = new Array('2', 'sexy2.jpg', 'Sexy');...
5
by: Abraham Lopez | last post by:
Hi.. Is there a way to convert a System.Array to XML... If you know thanks very much... if you don't... Please do not respond stupid things like " Yes -- many ways."
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.