Results 1 to 9 of 9

Thread: [RESOLVED] need help with array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2012
    Location
    St. Louis, MO
    Posts
    166

    Resolved [RESOLVED] need help with array

    I want to have an array with 2 columns - a list of applications in the first and the command to install them in the 2nd column. I want to display the array in a checked list box and based on selections run the command in the 2nd column
    I have done single column arrays but never tied them to commands

    any ideas where to begin?

    Code:
        
    Private Sub FindApp()
            Dim AppName(10) As String
            Dim i As Integer
            AppName(0) = "Anira"
            AppName(1) = "AutoCad Mechanical X64"
            AppNAme(2) = "AutoCad Electirical X64"
            AppNAme(3) = "AutoCad Civil X64"
            AppName(4) = "SAP 7.20 X64"
            AppName(5) = "Office 2010 Professional"
            AppName(6) = "Visio 2010"
            AppName(7) = "Project 2010"
            AppName(8) = "ProphetX"
            AppName(9) = "Rhumba"
    
            For i = 0 To 9
                AppListBox.Items.Add(AppName(i))
            Next i
    
        End Sub
    Last edited by sheetzdw; Feb 24th, 2012 at 05:57 PM. Reason: not done editing

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: need help with array

    I wouldn't use an array with two columns. Heck, I'm not even sure what that is. I would guess that you mean a two dimensional array. The better solution would be to create a class with two members, one the application, and the second for the command. Those sound like a pair of strings, but maybe not. Once you have such a class, you would be working with a List (of that class).

    If the class exposes those two items as properties, I believe you could bind the Listbox to the List with the DisplayMember being the application name and the ValueMember being the command. I haven't tried that, but it should work.

    An alternative that I have tried would be to have those things in a datatable, which might be easier, anyways. A datatable with two columns would be fairly easy to set up, and would more closely resemble your array, anyways, and it would certainly be bindable to the listbox.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2012
    Location
    St. Louis, MO
    Posts
    166

    Re: need help with array

    Quote Originally Posted by Shaggy Hiker View Post
    I wouldn't use an array with two columns. Heck, I'm not even sure what that is. I would guess that you mean a two dimensional array. The better solution would be to create a class with two members, one the application, and the second for the command. Those sound like a pair of strings, but maybe not. Once you have such a class, you would be working with a List (of that class).

    If the class exposes those two items as properties, I believe you could bind the Listbox to the List with the DisplayMember being the application name and the ValueMember being the command. I haven't tried that, but it should work.

    An alternative that I have tried would be to have those things in a datatable, which might be easier, anyways. A datatable with two columns would be fairly easy to set up, and would more closely resemble your array, anyways, and it would certainly be bindable to the listbox.

    thanks for you response,
    OK so what is the best way to add a datatable with maybe 20 - 30 rows and 2 -3 columns ? use a SQL or Acess db, csv file or what. I am a noob and a little rusty since my vb classes in college 2 years ago so think simple please.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: need help with array

    Using a database gives you a lot more options but it really depends on what you want to do with the data. If you just want to read it into your program and display it then a csv or other delimited file would be fine, especially if there are only a few lines as indicated.

    Use a class and a list (of that class) as suggested by Shaggy Hiker to store your data in memory.

    If you need to insert, search, sort and such with your data and your data may grow to a large size then a database would be a much better choice than a flat file.

  5. #5
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: need help with array

    Oh and sheetzdw, just a little info about arrays in VB: specifying a number to initialize it is to specify the upper index, not count. Meaning you're using 10/11 of the defined array. VB is weird like that.

    If the data isn't likely to grow in size at all, you could just have it hard coded. Otherwise it just depends on the volume and how often it needs to be accessed. I would say 30 records would not be enough to get any benefit from an SQL database. Maybe go csv/xml. But like mentioned above, using a custom class or structure with properties is a good option to store data as you can customize how it's held and displayed to a higher degree than the default object types.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2012
    Location
    St. Louis, MO
    Posts
    166

    Re: need help with array

    Quote Originally Posted by skor13 View Post
    Oh and sheetzdw, just a little info about arrays in VB: specifying a number to initialize it is to specify the upper index, not count. Meaning you're using 10/11 of the defined array. VB is weird like that.

    If the data isn't likely to grow in size at all, you could just have it hard coded. Otherwise it just depends on the volume and how often it needs to be accessed. I would say 30 records would not be enough to get any benefit from an SQL database. Maybe go csv/xml. But like mentioned above, using a custom class or structure with properties is a good option to store data as you can customize how it's held and displayed to a higher degree than the default object types.
    the data wont be much but will cahnge occasionally. Can you give an example of how to do a custom class using data.xml as the data file?

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: need help with array

    Do you care whether a person can edit the data in the file in something like Notepad?

    Actually, it seems like this thread has moved in a different direction from how it started out. There seems to be some assumption that you want to save this information, but you don't actually say that you want that. If you do, then a datatable would be a good way, because you can save/restore to and from XML with a single statement. That's where it would matter whether you want to allow a person to easily edit the data, because XML is a good option in that case. If you don't want people to easily edit that file, then binary serialization to a binary file would be better, which would suggest that a List of a custom class would be a good option.

    So, is this something that you want to read/write to a file, and do you want it easily readable or not?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2012
    Location
    St. Louis, MO
    Posts
    166

    Re: need help with array

    Quote Originally Posted by Shaggy Hiker View Post
    Do you care whether a person can edit the data in the file in something like Notepad?

    Actually, it seems like this thread has moved in a different direction from how it started out. There seems to be some assumption that you want to save this information, but you don't actually say that you want that. If you do, then a datatable would be a good way, because you can save/restore to and from XML with a single statement. That's where it would matter whether you want to allow a person to easily edit the data, because XML is a good option in that case. If you don't want people to easily edit that file, then binary serialization to a binary file would be better, which would suggest that a List of a custom class would be a good option.

    So, is this something that you want to read/write to a file, and do you want it easily readable or not?
    Actually I would prefer to be able to edit the file easily in notepad so I dont have to re-write code to make changes to the data. Any changes made must be saved for future use.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: need help with array

    Ok, then the easiest thing to do would probably be to use a datatable. You would create a new datatable, then add two columns to it, one for each of your fields. Once you have the datatable created (I should have examples around somewhere, but I can't think where they are at the moment. Still, creating two columns isn't very difficult, and there are examples in MSDN), you would populate it like this:

    Dim dr as yourDatatable.NewRow
    dr("first column name") = whatever value.
    dr("second column name") = whatever value
    yourDatatable.Rows.Add(dr)

    That's what would be done in your loop to fill it the first time. Once you have filled it, you would use the WriteXML(filename here) method to write it out, and next time you ran the program you would use the ReadXML(filename here) method to restore it. You can look up those two methods for examples. There are other alternate overloads of both Write and Read, but the ones that take a file path and name are the easiest, I would say.
    My usual boring signature: Nothing

Tags for this Thread

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