locked
How can I avoid code repetition?

    Question

  • Hi

    I need some help on my project. I have 76 textboxes and 76 textfiles. I want each one to display the text from it's corresponding textfile. In other words

    Text in textfile1 in Label 1

    Text in textfile2 in Label 2

    Text in textfile3 in Label 3

    and so on until 76

    Is there any way easier than repeating the code 76 times?

    Monday, October 23, 2006 1:37 PM

Answers

  • Hi,

    you could make a function like in the following example:

    Public Sub FillTextBoxWithFileContents(ByVal control As TextBox, ByVal filename As String)
        ' Return if file doesn't exist
        If Not My.Computer.FileSystem.FileExists(filename) Then
           
    Return
       
    End If
       
    control.Text = My.Computer.FileSystem.ReadAllText(filename)
    End Sub

    ... and call it with required parameters - the following example uses a loop to make the code even shorter:

    For i As Integer = 1 To 76
       
    Dim control As TextBox = Controls("TextBox" & i)
       
    Dim filename = String.Format("c:\textfile{0}.txt", i)
       
    FillTextBoxWithFileContents(control, filename)
    Next

    If you're filling labels instead of textboxes, replace TextBox references with Label in above code.

    Andrej

    Monday, October 23, 2006 2:01 PM

All replies

  • well if the labels had an incremental number at the end of the control name and so did the textfile, then its simple. Example:

    label1 -> textfile1.txt

    label2 -> textfile2.txt

     

    as you see, each "number" for the control maps to the "number" of the textfile. If this were the case, this could work:

     

    for each currentControl as Control in Me.Controls

       if currentControl.GetType() Is GetType(Label) then

          if currentControl.Name.StartsWith("label") then

             currentControl.Text = Me.GetText("fileName" & currentControl.SubString(currentControl.LastIndexOf("label" + 1) & ".txt")

          end if

       end if

    next

     

    private function GetText(byval fileName as string) as string

       if System.IO.File.Exists(fileName) = false then

          return "file not found"

       else

          Dim theFileContents as String = My.Computer.FileSystem.ReadAllText(fileName)

          return theFileContents

       end if

    end function

     

    this would be bad when getting the number of the label as we are doing some string parsing, the label may not have a number in it but lets just say it does, then this would hopefully work.

    The other way would be to check to see if the label has a number at the end of the word "label", by going through after each character of "label" and see if there is a number using Char.IsNumber()

    what would make it much easier is if the textfiles had the same name as the label control...we wouldnt have to do all the checking to see if there is a number that exists at the end of the word label and do some string parsing

    you could also maybe keep track of the current label index you are on so you can then load the textfile if the textfile has an incremental number so you can get the textfilename + the current index number

    does this help?

    Monday, October 23, 2006 1:57 PM
  • Hi,

    you could make a function like in the following example:

    Public Sub FillTextBoxWithFileContents(ByVal control As TextBox, ByVal filename As String)
        ' Return if file doesn't exist
        If Not My.Computer.FileSystem.FileExists(filename) Then
           
    Return
       
    End If
       
    control.Text = My.Computer.FileSystem.ReadAllText(filename)
    End Sub

    ... and call it with required parameters - the following example uses a loop to make the code even shorter:

    For i As Integer = 1 To 76
       
    Dim control As TextBox = Controls("TextBox" & i)
       
    Dim filename = String.Format("c:\textfile{0}.txt", i)
       
    FillTextBoxWithFileContents(control, filename)
    Next

    If you're filling labels instead of textboxes, replace TextBox references with Label in above code.

    Andrej

    Monday, October 23, 2006 2:01 PM
  • Thanks both of you for your time, but it did not work.

    I already had a code similar to yours Andrej and it did not work. Unfortunately, this one didn't work either.

    I got this error message:

    "Object reference not set to an instance of an object."

    with

    control.Text = My.Computer.FileSystem.ReadAllText(filename)

    highlighted in yellow. I don't know if this means I havent goit the object, but i checked and everything is fine. Even the names range from "Label1" until "Label76".

    Hope you can help me

    Wednesday, October 25, 2006 11:30 AM
  • I modified the code a bit (added bold italic lines):

    For i As Integer = 1 To 76
       
    Dim control As TextBox = Me.Controls("TextBox" & i)
       
    If control Is Nothing Then
           
    MessageBox.Show("Control not found, check the name again")
       
    Else
          
    Dim filename = String.Format("c:\textfile{0}.txt", i)
           
    FillTextBoxWithFileContents(control, filename)
       
    End If
    Next

    What was added, was a check if control was actually found - your exception's text indicates that it wasn't. Also, the above code assumes that all controls are put directly on the form, hence the Me.Controls(...) statement. If controls are put in some other container (a panel or similar), replace the Me part with the name of this container (e.g. Me.Panel1.Controls(...)).

    Hope this helps,

    Andrej

     

    Wednesday, October 25, 2006 11:48 AM
  • It worked. Thanks for your time. The thing  I didn't know was the container (Me.GroupBox1)
    Wednesday, October 25, 2006 4:05 PM