Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27223

VS 2010 DirectCast used to work and now it doesn't

$
0
0
I have 10 textboxes called TextBox1 to 10 and I need to refer to them by their number. I used the code below and it worked fine. When I closed VS and reopened it later, I changed the filename of the list of words and nothing more. Now it throws an exception on all the DirectCast lines and I can't work out why. The error is a NullReferenceException - Reference to object not established as object. It suggests using New but I don't know how to do that to relate to what I want it to do.
The code is below
vb.net Code:
  1. Private Sub btnMatchLetters_Click(sender As System.Object, e As System.EventArgs) Handles btnMatchLetters.Click
  2.         Dim Letters(9) As String
  3.         Dim WordLength As Integer = 10
  4.         'clear the listbox ready for a new list
  5.         ListBox1.Items.Clear()
  6.         'work backwards until find first highlighted textbox which indicates length of word
  7.         'DirectCast here allows us to refer to each textbox by its number
  8.         For CtrlPtr As Integer = 10 To 1 Step -1
  9.             If DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).BackColor = Color.DarkCyan Then
  10.                 'found length so get out
  11.                 WordLength = CtrlPtr
  12.                 Exit For
  13.             End If
  14.         Next
  15.         'copy the letters to an array so we can check them
  16.         For CtrlPtr = 10 To 1 Step -1
  17.             Letters(CtrlPtr - 1) = DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).Text().ToLower
  18.         Next
  19.  
  20.         For Each word In Dict
  21.             'ProgressBar1.PerformStep()
  22.             Dim MatchLetters As Boolean = True      'set flag to true before entering the routine. It will  be set to false on no match
  23.             If word.Length = WordLength Then        'for speed only check words of the correct length
  24.                 For Letter As Integer = 0 To word.Length - 1
  25.                     'check each letter in turn
  26.                     'ignore blank spaces
  27.                     'if letter does not match, set flag to false
  28.                     If Letters(Letter) <> "" AndAlso Letters(Letter) <> word.Substring(Letter, 1) Then MatchLetters = False
  29.                 Next
  30.                 'when word is checked, if the flag is still true, add it to the listbox
  31.                 If MatchLetters = True Then
  32.                     ListBox1.Items.Add(word)
  33.                 End If
  34.             End If
  35.         Next
  36.         lblMatches.Text = ListBox1.Items.Count.ToString & " Matches"
  37.     End Sub
And the other place I used DirectCast
vb.net Code:
  1. Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
  2.         For CtrlPtr = 10 To 1 Step -1
  3.             'directcast allows us to reference the textboxes by their number
  4.             DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).Clear()
  5.             DirectCast(Me.Controls("TextBox" & CtrlPtr.ToString), TextBox).BackColor = Control.DefaultBackColor
  6.         Next
  7.     End Sub
The only piece of code I changed when I came back to it was this. The old line is commented out (setting the filename)
vb.net Code:
  1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  2.         'the files are called "A Words" "B Words" etc. CHR65 to 90 represent capital A to Z
  3.         '        For LetterRef As Integer = 65 To 90
  4.         'create the filename
  5.         ' MyFile = Chr(LetterRef) & " Words.csv"
  6.         MyFile = "dictionary.txt"
  7.         'read it
  8.         Dim Words() As String = File.ReadAllLines(MyFile)
  9.         Dim f As Integer = 0
  10.         For f = 1 To Words.Length - 1
  11.             'add to the final list
  12.             Dict.Add(Words(f))
  13.         Next
  14.         NoOfWords = f           'save the number of words to be used later by the progressbar
  15.         ProgressBar1.Maximum = CInt(NoOfWords)
  16.         ProgressBar1.Minimum = 0
  17.  
  18.         '        Next

Viewing all articles
Browse latest Browse all 27223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>