Array Structure Help

Fairly new to VB, but i have been advancing pretty decently in the past couple months. I'm currently writing a program that takes data entered from the user and stores it, and then i have various buttons within 2 forms for calculating the average, showing certain filtered items, ect...

I have used array structures before, but not globally (among a multi-form project). In my module that i have declared all of my global variables, i have the following for my structure ...

Public Structure Entry
Dim gScore() As Decimal
Dim gGender(), gDean(), gName() As String
End Structure
Public Data() As Entry
Public gCtr As Integer = -1
Public Heading, Divider, NameTemp, GenderTemp, DeanTemp As String
Public ScoreTemp As Decimal

In my main form i have ...

ReDim Preserve Data(gCtr)

Data(gCtr).gName = txtName.Text
Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
Data(gCtr).gGender = GenderTemp
Data(gCtr).gDean = DeanTemp

Now I'm getting this error ..
Error 1 Value of type 'String' cannot be converted to '1-dimensional array of String'.
The error is reoccurring for all of my Data.variables.

Like i said above I've coded with an array structure before, but not globally and the way i set my variables worked in that program. However they are not working properly here. Am i setting them wrong ... or what should i do/be doing



Answer this question

Array Structure Help

  • LouisPeter

    In reading through this thread, it appears the root of the problem is that your arrays are not declared with a dimension. Since you created empty arrays, they need to be redimensioned before you can start assigning values. If you don't know what the size of the array needs to be when it is declared, you might consider using an ArrayList, List(Of T), or strongly typed collection object instead of just an array. Then you don't have to worry about the size of the collection. You should at least use the collection privately within the structure - if you want to expose an array of values from the structure, use the ToArray() method of your collection in a readonly property on the structure.

    Also, since these are globally shared variables, it may be that some other code routine is changing your arrays. Global variables should be avoided whenever possible for just this reason. It is much better to pass an instance of the object between forms rather than declare the object globally.



  • Sumesh Nair

    ok thanks dman that did help, however now it has created another error for me.

    When i enter all my values and hit show all to show all the entered information in my list box on the main form it presents me with a "NullReferenceException was unhandled" error.

    It highlights this code ...

    For X = 0 To gCtr
    Line = Data(X).gName.PadLeft(0) & Data(X).gScore.ToString.PadLeft(7) _
    & Data(X).gGender.ToString.PadLeft(15) & Data(X).gDean.ToString.PadLeft(15)
    lstData.Items.Add(Line)
    Next X


  • furjaw

    Then your first element of the array or one of its items has not been declared...repost the corrrected code and we will go from there....

    Dim Data(NumRecords) as Entry

    Dim gCtr as Integer = 0

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp

    This code only sets element zero of the Data Array



  • dvidal

    It's showing X as 0


  • Stefan Wrobel

    frmMain code section
    'Counting number of entries
    gCtr = gCtr + 1

    'Preserve original array(s) and set variables
    ReDim Preserve Data(gCtr)

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp

    My Globals
    Public Structure Entry
    Dim gScore As Decimal
    Dim gGender, gDean, gName As String
    End Structure
    Public Data() As Entry
    Public gCtr As Integer = -1
    Public Z, Max, K, X As Integer
    Public Heading, Divider, NameTemp, GenderTemp, DeanTemp As String
    Public ScoreTemp As Decimal


  • hommer

    In the code above that you posted only the last element of the Data array gets filled(unless it is in a loop)...and that element number is gCtr

    Post the complete method that this code is in and I will better be able to help you

    'Counting number of entries
    gCtr = gCtr + 1

    'Preserve original array(s) and set variables
    ReDim Preserve Data(gCtr)

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp



  • Coritani

    If Data(0) to data(gCtr) has not been filled with values then you will get the null ref exception on your structure elements because they have not been explicitly set.....

    When it stops and highlights that line during debug...what is the value of X...



  • rahulmanasa

    Is Entry.gName suppose to be a string value or an array of strings...it looks to me like it should be a single string value...that being the case then your structure should declare the elment without the parentheses....

    Public Structure Entry

    Dim gScore() As Decimal<-This is an array of decimals
    Dim gGender(), gDean(), gName() As String <-These are arrays of Strings

    End Structure

    Public Structure Entry

    Dim gScore As Decimal <----single decimal entry

    Dim gGender, gDean, gNameAs String <----single string entries

    End Structure

    You also can not declare the size of an array as -1


    Public NumRecords = 99

    Dim gCtr as Integer = 0

    Dim Data(NumRecords) as Entry <-------an array of entry containing 100 records

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp



  • AlexBB

     DMan1 wrote:

    In the code above that you posted only the last element of the Data array gets filled(unless it is in a loop)...and that element number is gCtr

    Post the complete method that this code is in and I will better be able to help you

    'Counting number of entries
    gCtr = gCtr + 1

    'Preserve original array(s) and set variables
    ReDim Preserve Data(gCtr)

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp


    frmMain Code
    Click event for btnEnter

    'Validation
    If Validation() = False Then
    Exit Sub
    End If

    'Counting number of entries
    gCtr = gCtr + 1

    'Preserve original array(s) and set variables
    ReDim Preserve Data(gCtr)

    Data(gCtr).gName = txtName.Text
    Data(gCtr).gScore = Decimal.Parse(txtScore.Text)
    Data(gCtr).gGender = GenderTemp
    Data(gCtr).gDean = DeanTemp

    'Changes radbutton to display a gender and not a true/false statement
    If radMale.Checked = True Then
    GenderTemp = "M"
    Else
    GenderTemp = "F"
    End If

    Making checkbox state yes/no if on dean's list
    If ckbDean.Checked = True Then
    DeanTemp = "Yes"
    Else
    DeanTemp = "No"
    End If

    'Clear values for next input
    txtName.Clear()
    txtScore.Clear()
    radMale.Checked = False
    radFemale.Checked = False
    cboDep.Text = ""
    ckbDean.Checked = False
    lstData.Text = ""
    txtName.Focus()

  • Array Structure Help