Page 3 of 3 FirstFirst 123
Results 81 to 98 of 98

Thread: good old arrays

  1. #81

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    I havent forgetten you, im stranded in Europe due to the volcano...

    I have been reading about records and arrays during my vacation - yes I know lucky me (NOT) and believe for my final task I need to:

    - define a type that consists of mixed data types so Name would be string and Mark would be Integer (infact it could be byte)

    - after creating the type, call it in the program so that when the ADD button is clicked the name is stored in the name list box and the mark in the mark list box...

    So far I have this but it doesnt work and Im not sure why, perhaps you can guys can help?

    Code:
    Option Explicit
    
    '~~~ Creating the type
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Private Sub Form_Load()
    
        '~~~ Checking. This should be done first !
        If Val(txtStudentMark.Text) <= 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf Val(txtStudentMark.Text) > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        Element = Element + 1
        ReDim Preserve strName(1 To Element)
        ReDim Preserve intMark(1 To Element)
     
        strname(student) = txtStudentName.Text
        intMark(student) = txtStudentMark.Text
    
           
        lstNames.AddItem txtStudentName.Text
        lstMarks.AddItem txtStudentMark.Text
    
    End Sub
    thanks in advance - will check back shortly, depending on internet connection availabilty...

    thank you so much

  2. #82
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by xirokx View Post
    - define a type that consists of mixed data types so Name would be string and Mark would be Integer (infact it could be byte)

    - after creating the type, call it in the program so that when the ADD button is clicked the name is stored in the name list box and the mark in the mark list box...
    You have already done those things.
    Check all the previous posts. Start reading from the beginning.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #83

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    yes i have but i did not assign the student type to the array so I could add names and marks to my list boxes..??

    thats the part Im confused on as you can see from my above attempt..

    remember it needs to be a dynamic array

  4. #84
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by xirokx View Post
    yes i have but i did not assign the student type to the array so I could add names and marks to my list boxes..??

    thats the part Im confused on as you can see from my above attempt..

    remember it needs to be a dynamic array


    Could you explain it ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #85

  6. #86
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by MartinLiss View Post
    lstNames.AddItem Students(Element).StudentName
    You did it again...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #87

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    shouldnt that be:

    lstNames.AddItem Students(Element).strname

    ??

  8. #88

  9. #89

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    here is my code, I receive an error message "variable not defined"

    Option Explicit
    Dim Element As Integer

    '~~~ Creating the type
    Private Type Student
    strName As String
    intMark As Integer
    End Type
    Private Sub cmdAddRecord_Click()

    '~~~ Checking. This should be done first !
    If Val(txtStudentMark.Text) <= 0 Then
    MsgBox "student mark cannot be less than zero. Please re-enter it"
    Exit Sub
    ElseIf Val(txtStudentMark.Text) > 100 Then
    MsgBox "student mark cannot be greater than 100. Please re-enter it"
    Exit Sub
    End If

    Element = Element + 1
    ReDim Preserve strName(1 To Element) <---------------error msg "variable not defined"
    ReDim Preserve intMark(1 To Element)

    strName(Student) = txtStudentName.Text
    intMark(Student) = txtStudentMark.Text

    lstNames.AddItem Student(Element).strName
    lstMarks.AddItem Student(Element).intMark

    End Sub

    I thought it was though above? Have I declared the datatypes correctly?

  10. #90

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    i had a look at post #2 now I have an internet connection again but I dont see how that can help me because I am not using a loop...also post #2 does not say anything about private types which I have used in my code..

    therefore I remain unsure of how to resolve my code so that it works i.e. resolving the error msg 'variable not defined' especially when I have used a private type and am using the 'student' type..

    please put me on the right track or point me in the right direction

    thanks all

  11. #91
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: good old arrays

    It looks like you have declared the type Student but have not declared an instance of the type ie

    Dim XXX as Student

    Also when accessing you need to do

    XXX.strName(Student) = txtStudentName.Text
    XXX.intMark(Student) = txtStudentMark.Text

    I may have also missed the point completely but you really should use [ code ][ /Code] tags and indent your code.
    Slower than a crippled Vista
    More buggy than a fresh XP install
    Look! Down the road, some 50 miles behind the drunken snail.
    It's Ubuntu!

  12. #92

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    Code:
    Option Explicit
    Dim Element As Integer
    Dim Students As Student
    
    '~~~ Creating the type
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Private Sub cmdAddRecord_Click()
    
        '~~~ Checking. This should be done first !
        If Val(txtStudentMark.Text) <= 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf Val(txtStudentMark.Text) > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        Element = Element + 1
        ReDim Preserve Students.strName(1 To Element)
        ReDim Preserve Students.intMark(1 To Element)
        
        Students.strName(Student) = txtStudentName.Text
        Students.intMark(Student) = txtStudentMark.Text
           
        lstNames.AddItem Student(Element).strName
        lstMarks.AddItem Student(Element).intMark
    
    End Sub
    
    Private Sub cmdClearRecord_Click()
        txtStudentName.Text = ""
        txtStudentMark.Text = ""
        txtStudentName.SetFocus
    End Sub
    this gives me the following error mesage:

    "expected array"

    it points me towards the "redim" line as if to say there is an error on that line...

    what am i doing wrong..

    please help and show me the way

    thanks

  13. #93

    Thread Starter
    Lively Member
    Join Date
    Nov 2009
    Location
    North London
    Posts
    121

    Re: good old arrays

    somethings up with the array and the calling off I believe - but what?

    I'm one baffled IROK...

  14. #94
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: good old arrays

    Your TYPE doesn't include arrays, but you are trying to use them as arrays

    See changes in blue, the parentheses
    Code:
    Private Type Student
      strName() As String
      intMark() As Integer
    End Type
    After above change, you will probably get an error on these lines
    Code:
        Students.strName(Student) = txtStudentName.Text
        Students.intMark(Student) = txtStudentMark.Text
           
        lstNames.AddItem Student(Element).strName
        lstMarks.AddItem Student(Element).intMark
    That is because I believe Student should be Students, you need to include the array index, i.e.
    Code:
        Students.strName(Element) = txtStudentName.Text
        Students.intMark(Element) = txtStudentMark.Text
           
        lstNames.AddItem Students.strName(Element)
        lstMarks.AddItem Students.intMark(Element)
    Last edited by LaVolpe; Apr 21st, 2010 at 03:56 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #95
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: good old arrays

    Try this:
    Code:
    Option Explicit
    
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Dim Students() As Student
    
    Private Sub cmdAddRecord_Click()
        Dim iMark As Long
        Dim sName As String
        Dim i As Long
        
        sName = Trim(txtStudentName.Text)
        If sName = "" Then
            MsgBox "student name cannot be blank. Please re-enter it"
            Exit Sub
        End If
    
        iMark = Val(txtStudentMark.Text)
        If iMark < 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf iMark > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        If (Not Students) = -1 Then '-- Students() array was not initialized
            i = 1
        Else
            i = UBound(Students) + 1
        End If
        ReDim Preserve Students(1 To i)
        
        Students(i).strName = sName
        Students(i).intMark = iMark
           
        lstNames.AddItem sName
        lstMarks.AddItem iMark
    
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  16. #96
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by anhn View Post
    Try this:
    Code:
    Option Explicit
    
    Private Type Student
      strName As String
      intMark As Integer
    End Type
    
    Dim Students() As Student
    
    Private Sub cmdAddRecord_Click()
        Dim iMark As Long
        Dim sName As String
        Dim i As Long
        
        sName = Trim(txtStudentName.Text)
        If sName = "" Then
            MsgBox "student name cannot be blank. Please re-enter it"
            Exit Sub
        End If
    
        iMark = Val(txtStudentMark.Text)
        If iMark < 0 Then
            MsgBox "student mark cannot be less than zero. Please re-enter it"
            Exit Sub
        ElseIf iMark > 100 Then
            MsgBox "student mark cannot be greater than 100. Please re-enter it"
            Exit Sub
        End If
        
        If (Not Students) = -1 Then '-- Students() array was not initialized
            i = 1
        Else
            i = UBound(Students) + 1
        End If
        ReDim Preserve Students(1 To i)
        
        Students(i).strName = sName
        Students(i).intMark = iMark
           
        lstNames.AddItem sName
        lstMarks.AddItem iMark
    
    End Sub
    I had already suggested and posted the code for using dynamic array in post #21

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  17. #97
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: good old arrays

    Quote Originally Posted by akhileshbc View Post
    I had already suggested and posted the code for using dynamic array in post #21
    I have no time to read 94 posts.
    The main thing is the declare of Students. That must be an array of type Student.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  18. #98
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: good old arrays

    Quote Originally Posted by anhn View Post
    I have no time to read 94 posts.
    The main thing is the declare of Students. That must be an array of type Student.
    I wasn't complaining you You have done a great job...
    I was trying to provide a statement for this:
    Quote Originally Posted by akhileshbc View Post
    You have already done those things.
    Check all the previous posts. Start reading from the beginning.
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width