Why is the following VB.NET code setting str to Nothing in my VS2005 IDE:
If Trim(str = New StreamReader(OpenFile).ReadToEnd) <> "" Then
Button2.Enabled = True
TextBox1.Text = fname
End If
OpenFile is a Function that returns a FileStream
EDIT: If the above line containing Trim is not correct, is there a way to achieve the intended result in only one line?
The code is never setting
strat all:This line doesn’t set
str, it compares it to the result of reading the file.In VB, the
=operator has two meanings, depending on context. If used in a statement, it assigns the right-hand expression to the left-hand expression. If used in any other context (i.e. in an expression), it performs an equality comparison, not an assignment.Thus, in VB you must write the following:
Notice that I’ve replaced the free function
Trimby a method call to make the code more consistent with common .NET coding practices.