Results 1 to 10 of 10

Thread: [RESOLVED] How to handle a VERY simple data problem

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Resolved [RESOLVED] How to handle a VERY simple data problem

    I need to make a program that can open and read some simple data and make an association.

    So for example the data would be something like this:

    Name Phone number
    Bob 408-911-9111
    ...
    ...

    I want to be able only show the name in a listbox and once the user clicks on the name the phone number shows in a message box.

    I handled it using a case statement, but unfortunately the case statement is getting WAY too big since there is about 30 items in the list (and there will be more). How can I do this using either an XML file or reading off a text file or something?

    I don't have any DB programs btw.

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to handle a VERY simple data problem

    use an array (declare public at form level).
    as you add the names to the list, use redim preserve to resize the array, then store the phone number in the new array index.
    when you select a name in the list, the selectedindex will be the array index that holds the phone number

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: How to handle a VERY simple data problem

    right on, thanks, I will try that.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to handle a VERY simple data problem

    Why not just store them in a database that can grow as you need it to grow.

    The code you use would never have to be touched when you wanted to add new records or remove existing ones.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to handle a VERY simple data problem

    Or at least if you're to use a text based file, choose a well formatted format such as XML or CSV... That way, you can work with the file a lot easier using the tools readily available in the framework.

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: How to handle a VERY simple data problem

    Well I figured out how to do it with an array, but now I want to make a txt or XML file and use that to store the data and load it into the array. How would I go about setting the data from an opened file to an array?

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: How to handle a VERY simple data problem

    Here is what I have so far:
    Code:
        Structure People
            Dim Name As String
            Dim Number As String
    
        End Structure
    
     Dim People(5) As People
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim filename As String = "C:\Documents and Settings\Blah\Desktop\Persons.txt"
            Dim fields As String()
            Dim delimiter As String = ">"
            Using parser As New TextFieldParser(filename)
                parser.SetDelimiters(delimiter)
                While Not parser.EndOfData
                    fields = parser.ReadFields()
    'not sure what to do here
                   
                End While
            End Using
    
            Dim i As Integer
            For i = 0 To 2
                ListBox1.Items.Add(People(i).Name)
            Next
    
        End Sub
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, ListBox1.DoubleClick
            Dim SelectedItem = ListBox1.SelectedIndex
    
            MsgBox(People(SelectedItem).Number)
    
    
        End Sub

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: How to handle a VERY simple data problem

    If you want to go with XML (an excellent choice over the plain text file), easiest way is to use a data table... Think of a data table as a 2-d array or an excel sheet... If has rows and columns. So supposed you want to store a person's contact info on a datatable, you'd create a datatable consist of these columns: FirstName, LastName, PhoneNumber.... (whatever info you want to store), and each row in this table will be a record. This is the code to create such a datatable
    Code:
    Dim contactTable As New Data.DataTable()
    With contactTable.Columns
           .Add("FirstName", GetType(String))
           .Add("LastName", GetType(String))
           .Add("PhoneNumber", GetType(String))
    End With
    Now to add a row to this datatable, you do this
    Code:
    Dim row As Data.DataRow = contactTable.NewRow()
    row("FirstName") = "Bob"
    row("LastName") = "Blair"
    row("PhoneNumber") = "123-456-7890"
    contactTable.Rows.Add(row)
    To write this table to an XML file, you simply do
    Code:
    contactTable.WriteXml("c:\my folder\myFile.xml")
    To read this xml file back into a datatable, you simply do
    Code:
    Dim table As New DataTabble
    table.ReadXml("c:\my folder\myFile.xml")
    That's will cover the most parts that you'll need.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] How to handle a VERY simple data problem

    thats 1 way.
    to keep it simple i'd use a streamreader/streamwriter with a text file and an array

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2007
    Location
    Silly Clone Valley CA
    Posts
    47

    Re: [RESOLVED] How to handle a VERY simple data problem

    Thanks guys!

    I went the XML route and created a datagrid view item and use it to load and save an xml file with the items in it. It works great! Now to figure out how to know what was selected in the datagridview control.

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