Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,561 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,639 people online right now. Registration is fast and FREE... Join Now!




Array help

 
Reply to this topicStart new topic

Array help

woodduck
29 Nov, 2007 - 02:40 PM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 6


My Contributions
I am trying to allow users to input array subscript size through a input box. I may need to use a dynamic array though I am nt sure how to code this. It keeps making me state the array size within the parethesis. Here is my code. It build but will not calculate. I entered 0 in subscript size to remove errors but this did not work.

CODE

Public Class mainForm
    'module-level array
    Private gradeValues(0) As String
    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
        'allows users to enter grades
        ' stores the grades in the module-level
        ' gradeValues array
        
        Dim gradeValues(0) As String
        gradeValues(0) = InputBox("Enter number of grades to be entered. Click Cancel to end.", "Number of Grades", "0")



        For subscript As Integer = 0 To gradeValues.Length - 1
            gradeValues(subscript) = InputBox("Grade Value:", "Grade Values")

        Next subscript
    End Sub

    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        'displays grades and calculated average grade
        Dim gradeValues() As Integer = {}
        Dim gradeAccumulator As Integer
        Dim averageGrade As Double
        ' accumulates total grades
        For Each grade As Integer In gradeValues
            gradeAccumulator = gradeAccumulator + grade
        Next grade
        '  displays grades and calculates  average grade
        For Each grade As String In gradeValues
            gradeLabel.Text = gradeLabel.Text & grade & ControlChars.NewLine
        Next grade
        averageGrade = gradeAccumulator / gradeValues.Length
        avggradeLabel.Text = Convert.ToString(averageGrade)



    End Sub
End Class

code.gif

This post has been edited by William_Wilson: 29 Nov, 2007 - 02:45 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Array Help
29 Nov, 2007 - 03:12 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Declare the array at the class level, as you originally did. And then ReDim the array once the user has specified the size.
Now I only did it in Load event, just to give you an example. Just use the example to modify your code the way it currently stands, you do not need to do it in the Load event like I did.

Here is an example with comments explaining what is going on:
CODE

Public Class Form1

    ' class level array declaration
    Private gradeValues() As String

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'declare an Integer variable to hold user response to the InputBox
        Dim numGrades As Integer

        'store the user response
        numGrades = InputBox("Enter number of grades to be entered. Click Cancel to end.", "Number of Grades", "0")

        'ReDim the array using the total grades - 1
        'Need to subtract one, because you are declaring the highest index number
        'of the array, not the total elements as in most other languages
        ReDim gradeValues(numGrades - 1)

    End Sub
End Class

User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Array Help
29 Nov, 2007 - 09:52 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Don't forget when you use the ReDim statement to also use the Preserve Modifier or everything currently in the Array is wiped out, like so:

CODE

        'ReDim the array using the total grades - 1
        'Need to subtract one, because you are declaring the highest index number
        'of the array, not the total elements as in most other languages
        ReDim Preserve gradeValues(numGrades - 1)

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Array Help
29 Nov, 2007 - 11:36 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Good point, PsychoCoder, I should have mentioned that. I made the assumption that he would want the values wiped out each time the Enter button was clicked.
User is online!Profile CardPM
+Quote Post

f_soto
RE: Array Help
30 Nov, 2007 - 11:55 PM
Post #5

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 10


My Contributions
Since we are talking about arrays, i have a question about them too if you guys can help me out... I have tried and tried and failed, cannot find examples of how to work with 2 dimensional arrays and text files.. My problem is that my array can finally write out to a text file(dont know if its right but it writes it); i cannot figure out how to make it read the text file back into my array.. Any help is appreciated.. Here is the code...

CODE

Option Strict On
Imports System.IO


Public Class Form1

    Dim Horas(31, 7) As Integer
    Dim Totals(7) As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Loads info from txt file to array
        Dim diasinteger As Integer
        Dim MinisterioStreamReader As New StreamReader("Ministerio.txt")

        Do While MinisterioStreamReader.Peek <> -1
            Dim row As String = MinisterioStreamReader.ReadLine

            Horas(diasinteger, 7) = CInt(Horas(diasinteger, 1) & _
            Horas(diasinteger, 2) & _
            Horas(diasinteger, 3) & _
            Horas(diasinteger, 4) & _
            Horas(diasinteger, 5) & _
            Horas(diasinteger, 6))
        Loop

        MinisterioStreamReader.Close()


    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Saves input into array
        Dim diasinteger As Integer

        If diasinteger <> -1 Then
            diasinteger = Integer.Parse(cboDias.Text)
            Horas(diasinteger, 1) = Integer.Parse(txtLibros.Text)
            Horas(diasinteger, 2) = Integer.Parse(txtFolletos.Text)
            Horas(diasinteger, 3) = Integer.Parse(txtH.Text)
            Horas(diasinteger, 4) = Integer.Parse(txtM.Text)
            Horas(diasinteger, 5) = Integer.Parse(txtRev.Text)
            Horas(diasinteger, 6) = Integer.Parse(txtRvsitas.Text)

            'Clears text boxes for new entry
            txtLibros.Text = ""
            txtFolletos.Text = ""
            txtH.Text = ""
            txtM.Text = ""
            txtRev.Text = ""
            txtRvsitas.Text = ""

            'Saves Array info to text file

            Dim MinisterioStreamWriter As New StreamWriter("Ministerio.txt")
          
            For diasinteger = 1 To 31
                MinisterioStreamWriter.WriteLine(Horas(diasinteger, 1) & _
                Horas(diasinteger, 2) & _
                Horas(diasinteger, 3) & _
                Horas(diasinteger, 4) & _
                Horas(diasinteger, 5) & _
                Horas(diasinteger, 6))
            Next

            MinisterioStreamWriter.Close()
        Else
            MessageBox.Show("Favor de seleccionar Dia", "Dia no esta seleccionado", MessageBoxButtons.OKCancel, _
            MessageBoxIcon.Exclamation)
        End If
    End Sub

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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