I'm experiencing a strange problem with a VB.Net - If statement:
is executed when riImageFile.Name contains a valid path string (as it should) and when
riImageFile.Name = "" (which is what I'm trying to prevent).
This seems to defy the basic principles of the VB "If" statement.
The same thing happens with
with images that are listed in a random access file. Each record in the file contains a marker
("*" asterisk or " " space) indicating whether or not the image is in the active wallpaper rotation,
a fixed length string (255) containing the path and filename and an integer denoting the
display style (tiled, centered, stretched). As each record is read the values are assigned to
properties of an instance of a class I wrote that represents a wallpaper image object (class
code is shown below.)
The program worked fine until I wrote code to remove records from the random file. The code rewrote
the record with all spaces in the filename and marker fields and integer 0 (zero) in the
display style field. I used this approach to avoid rewriting the whole file each time the user
wants to remove an image from the list, and will rewrite the file omitting these blanked records
when program closes only if the user has removed items from the list. Now when the program opens
and reads thru the list file I need to skip over records that contain blank values and not include
them in the listview. I added the IF statement to the LoadImageList() procedure but my If statement
doesn't work. The If statement in question in the code below is proceeded and followed by comment lines of asterisks.
( '*********************)
I believe my logic is sound but then again maybe I have been staring at code too long and have
gone insane.
If you can see anything that might be causing the problem your help would be greatly appreciated.
If Not riImageFile.Name = "" Then
The code in the 'IF block' still executes when riImageFile.Name = "" In fact the If statement is executed when riImageFile.Name contains a valid path string (as it should) and when
riImageFile.Name = "" (which is what I'm trying to prevent).
This seems to defy the basic principles of the VB "If" statement.
The same thing happens with
If riImageFile.Name.Length > 0 Then
The code in 'IF block' still executes when riImageFile.Name.Length = 0The program is a Windows wallpaper management program.
The LoadImageList() procedure is called from the Form_Load event to load a ListView control with images that are listed in a random access file. Each record in the file contains a marker
("*" asterisk or " " space) indicating whether or not the image is in the active wallpaper rotation,
a fixed length string (255) containing the path and filename and an integer denoting the
display style (tiled, centered, stretched). As each record is read the values are assigned to
properties of an instance of a class I wrote that represents a wallpaper image object (class
code is shown below.)
The program worked fine until I wrote code to remove records from the random file. The code rewrote
the record with all spaces in the filename and marker fields and integer 0 (zero) in the
display style field. I used this approach to avoid rewriting the whole file each time the user
wants to remove an image from the list, and will rewrite the file omitting these blanked records
when program closes only if the user has removed items from the list. Now when the program opens
and reads thru the list file I need to skip over records that contain blank values and not include
them in the listview. I added the IF statement to the LoadImageList() procedure but my If statement
doesn't work. The If statement in question in the code below is proceeded and followed by comment lines of asterisks.
( '*********************)
I believe my logic is sound but then again maybe I have been staring at code too long and have
gone insane.
If you can see anything that might be causing the problem your help would be greatly appreciated.
VB.NET:
Private Sub LoadImageList()
Dim ImageListFilePath As String = CurDir() & "\"
Dim FileName As String
Dim ImageRec As ImageRecord ' Declare a record variable for the new file.
Dim RecLength As Long ' Will hold the length of a record.
Dim Position As Long ' Use to track the current record.
Dim LastRecord As Long ' Will hold the position of the last record.
Dim FileNum As Integer ' Numeric identifier to reference opened file.
Dim riImageFile As New cRotatorImage
Dim ImgIdx As Integer
Dim NewListViewItem As New ListViewItem
Dim NewSubItem As ListViewItem.ListViewSubItem
FileNum = FreeFile()
Try
FileName = ImageListFilePath & "WALLPAPER" & ".Dat"
RecLength = Len(ImageRec) ' Calculate the record length.
FileOpen(FileNum, FileName, OpenMode.Random, OpenAccess.Read, , RecLength) 'Open file.
LastRecord = LOF(FileNum) / RecLength ' Determine position of last record in file.
For Position = 1 To LastRecord ' Loop through file.
FileGet(FileNum, ImageRec, Position) ' Read record at specified position
' into ImageRec variable.
riImageFile.Name = ImageRec.FileName.Trim ' Assign record values to corresponding
' properties of image file object which
If ImageRec.RotateMarker = ACTIVE Then ' is instance of the cRotatorImage class.
riImageFile.IsActiveInRotation = True
Else
riImageFile.IsActiveInRotation = False
End If
If ImageRec.StyleNum = 1 Then
riImageFile.Style = WallpaperStyle.Tiled
ElseIf ImageRec.StyleNum = 2 Then
riImageFile.Style = WallpaperStyle.Centered
ElseIf ImageRec.StyleNum = 3 Then
riImageFile.Style = WallpaperStyle.Stretched
End If
' If record is not blank (deleted) add to ListView. If record is blank
' riImageFile.Name will be equal to = "" (vbNullString)
' and riImageFile.Name.Length will be equal to = 0
' ***************************************************************************
' **** Following 'If' statement is not functioning properly (or I've gone insane)
If Not riImageFile.Name = "" Then
' ********** These If statements are not working either. ********************
'If riImageFile.Name <> "" Then
'If riImageFile.Name.Length > 0 Then
'If ImageRec.FileName.Trim.Length > 0 Then
' The code below is still executed even when riImageFile.Name = ""
' ***************************************************************************
NewListViewItem = New ListViewItem
NewSubItem = New ListViewItem.ListViewSubItem
' Set NewListViewItem properties.
NewListViewItem.Checked = riImageFile.IsActiveInRotation
NewListViewItem.Text = Path.GetFileName(riImageFile.Name)
NewListViewItem.Tag = riImageFile.Name
'NewSubItem.Text = riImageFile.Name.Substring(riImageFile.Name.LastIndexOf(",") + 1, 1)
NewSubItem.Text = riImageFile.Style
NewListViewItem.SubItems.Add(NewSubItem)
NewListViewItem.ImageIndex = ImgIdx
' Add NewListViewItem to listview.
lstvwImages.Items.Add(NewListViewItem)
If NewListViewItem.Tag = riCurrentImage.Name Then
NewListViewItem.Selected = True
End If
' Create image and add it to imagelist.
LrgImageList.Images.Add(Image.FromFile(riImageFile.Name))
ImgIdx += 1
End If
Next
Catch ex As Exception
FileName = ex.Message
Finally
FileClose(FileNum) ' Close file.
End Try
End Sub
'_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
Class definition:
Public Class cRotatorImage
Dim strName As String
Dim blnIsActiveInRotation As Boolean
Dim iStyle As WallpaperStyle
Public Property Name() As String
Get
Name = strName
End Get
Set(ByVal Value As String)
strName = Value
End Set
End Property
Public Property Style() As WallpaperStyle
Get
Style = iStyle
End Get
Set(ByVal Value As WallpaperStyle)
iStyle = Value
End Set
End Property
Public Property IsActiveInRotation() As Boolean
Get
IsActiveInRotation = blnIsActiveInRotation
End Get
Set(ByVal Value As Boolean)
blnIsActiveInRotation = Value
End Set
End Property
End Class
Public Structure ImageRecord
<VBFixedString(1)> Public RotateMarker As String
<VBFixedString(255)> Public FileName As String
Public StyleNum As Integer
End Structure
Public Const ACTIVE As String = "*"
Last edited: