Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,330 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,813 people online right now. Registration is fast and FREE... Join Now!




VB.NET2003 arrays

 
Reply to this topicStart new topic

VB.NET2003 arrays

circuspeanuts
11 Apr, 2008 - 09:28 AM
Post #1

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 68

hello,


I'm working on a project for my visual basic II class and I'm having some troubles...I have a break problem with my display average button (bolded). I'm trying to copy my class level array (numarray) to numarray1 (my other array) so that I can find my total and my average and display it as such (lstNumbers.items.add("Your total is: " & total) and lstNumbers.items.add("The average is: " & average).. Any advice? I don't have my book with me and am unable to find it online... this is my code:

CODE

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim frmSplash As New frmSplash

        'shows the splash screen
        frmSplash.Show()
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Dim iexit As Integer

        iexit = MessageBox.Show("Are you sure you wish to exit?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

        If iexit = DialogResult.Yes Then
            End
        End If
    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        Dim iclear As Integer

        iclear = MessageBox.Show("Are you sure you want to clear?", "Confirm Clear", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If iclear = DialogResult.Yes Then
            lstNumbers.Items.Clear()
        End If
    End Sub

    Private Sub cmdNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNum.Click
        Dim inum As Integer
        Dim count As Integer

        inum = InputBox("Please enter the number of numbers you will be using: ")
        lstNumbers.Items.Add("You've chosen to enter " & inum & " numbers")

        ReDim numarray(inum)

        For count = 1 To inum
            numarray(inum) = InputBox("Please Enter Your Numbers:")
            lstNumbers.Items.Add("You've Chosen The Number " & numarray(inum))
        Next

    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        Dim iexit As Integer

        iexit = MessageBox.Show("Are you sure you wish to exit?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

        If iexit = DialogResult.Yes Then
            End
        End If
    End Sub

    Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
        Dim iclear As Integer

        iclear = MessageBox.Show("Are you sure you want to clear?", "Confirm Clear", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If iclear = DialogResult.Yes Then
            lstNumbers.Items.Clear()
        End If
    End Sub

      Private Sub cmdDisplayAv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplayAv.Click
        Dim total As Integer
        Dim average As Integer
        Dim numarray1 As Integer
        Dim count As Integer

        For count = 0 To (numarray.Length - 1)
            numarray(inum) = numarray1
            total = inum + numarray(inum)
        Next



        average = total / inum

        lstNumbers.Items.Add("the average of the numbers is: " & average)
        lstNumbers.Items.Add("The sum of the numbers is: " & total)

        average = total / numarray(inum)
    End Sub
End Class


thanks for the help smile.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: VB.NET2003 Arrays
11 Apr, 2008 - 10:41 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
First of all, if you have an array at the class level are you looking to copy it to do an average? You can read the array itself and just figure out the average.

vb


Private Sub cmdDisplayAv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplayAv.Click
Dim total As Integer
Dim average As Integer
Dim numarray1 As Integer
Dim count As Integer

' Here total is going to be the sum of all the values in numarray
For count = 0 To (numarray.Length - 1)
total += numarray(count)
Next

' We then find the average by displaying the sum divided by the length of your array.
average = total / numarray.Length

lstNumbers.Items.Add("the average of the numbers is: " & average)
lstNumbers.Items.Add("The sum of the numbers is: " & total)
End Sub


No need to copy over an array nor use inum. You can find out everything without even leaving the function except for accessing numarray. Btw, you might want to consider making average a double value because averages tend to be decimal points. Otherwise 1 / 3 will be 0.

Is there another reason you need numarray1 or was it just for summing up the values? smile.gif

This post has been edited by Martyr2: 11 Apr, 2008 - 10:42 AM
User is offlineProfile CardPM
+Quote Post

circuspeanuts
RE: VB.NET2003 Arrays
14 Apr, 2008 - 05:33 AM
Post #3

D.I.C Head
**

Joined: 11 Apr, 2008
Posts: 68

thanks, it was summing up the values and getting the average smile.gif

EDIT: I tried a few things and then I inserted the code you gave me and I'm closer, however I think I may have misplaced one of my variables because when I run my program and I choose to enter 2 numbers, ( 3 and 3) I get my average as 1 and my total as 3 :S

This post has been edited by circuspeanuts: 14 Apr, 2008 - 06:00 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:48AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month