Trouble With Arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Trouble With Arrays

  1. #1
    Join Date
    Jun 2007
    Posts
    1

    Thanks

    Thank u
    Last edited by Drummin_Guy; 06-11-2007 at 08:25 AM. Reason: Because

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    I'am a little board today, so you get luckey, and I write your code for you! Have Fun!
    Code:
    Type walltype
       Length As Double
       Width As Double
    End Type
    
    Sub GetWallInfo()
      On Error GoTo Er
      Const SubTitle = "Get Wall Info"
      Dim I As Long
      Dim cntWalls As Long
      Dim Walls() As walltype
      Dim Size() As String
      Dim str As String
      
      'Get number of walls
      cntWalls = InputBox("How Many Walls?", SubTitle, 0)
      If cntWalls = 0 Then Exit Sub
      
      'Resize Dynamic Array to wall count
      ReDim Walls(1 To cntWalls)
      
      For I = 1 To cntWalls
        'Simpler code but alot more input for the user
        'Walls(I).Length = InputBox("Length in Inches Wall " & I & " :", "Wall " & I & " Get Length", 0.5)
        'Walls(I).Width = InputBox("Width in Inches Wall  " & I & " :", "Wall " & I & " Get Width", 0.5)
        
        'More complex code but stops more errors and uses less inputs for the user
        str = Trim(InputBox("Enter Length;Width for Wall " & I & ":", "Wall " & I, "0.5;0.5"))
        'Give them a way to cancel
        If str = "" Or InStr(str, ";") = 0 Then Goto Ex
        'Seperate length from width
        Size = Split(str, ";")
        'Store in array
        Walls(I).Length = Val(Size(0))
        Walls(I).Width = Val(Size(1))
        'Reset size for next wall
        Erase Size
      Next I
      
      '...do other stuff with wall info
      str = ""
      For I = 1 To cntWalls
        str = str & "Wall " & I & " Length:" & Walls(I).Length & " & Width:" & Walls(I).Width & vbCrLf
      Next I
      MsgBox str, vbInformation, "Results"
      '...
      
    Ex:
      'All done So Release Memory used to store wall info
      Erase Walls
      Exit Sub
      
    Er:
       MsgBox "Error# " & Err.Number & " - " & Err.Description, vbCritical, SubTitle
       Resume Ex
       Resume
    End Sub

  3. #3
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Quote Originally Posted by Drummin_Guy
    Thanks for the code that you sent me. I need more help though lol. Sorry

    Code:
    Type walltype
       Length As Double
       Width As Double
    End Type
    
    Sub GetWallInfo()
      On Error GoTo Er
      Const SubTitle = "Get Wall Info"
      Dim I As Long
      Dim cntWalls As Long
      Dim Walls() As walltype
      Dim Size() As String
      Dim str As String
    Does all that code up there go in the declarations? Because i've done that and it doesnt like the "type walltype" it says its a "compile error" I duno if its because im using Visual Basic 6 or what.

    And for the rest of the code would that just work in a click event or what. I'll probably use the more simple one as with the other one its more complicated code i dont fully understand what its all doing. Im sorry for this like i said im a noob and need more info on were to place the code. If you could help me i would really appriciate it. thanks alot.
    Type definitions need to be at the Module Level, or the class Module Level. If you place the type def within a class module you will need to make it private. Public type defs are only allowed in standard modules.
    Code:
    'Private type defs are allowed in class modules
    Private Type walltype
       Length As Double
       Width As Double
    End Type
    One last note: A Forms Module is a Class Module so you would have to make it private there also.
    Have Fun!
    Last edited by Ron Weller; 06-13-2007 at 08:39 PM.

Similar Threads

  1. Passing Arrays into Functions
    By Catalyst8487 in forum C++
    Replies: 7
    Last Post: 05-08-2007, 07:48 AM
  2. Replies: 3
    Last Post: 11-10-2006, 12:54 PM
  3. Replies: 15
    Last Post: 05-09-2001, 04:40 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 03:06 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 06-20-2000, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center