G
Guest
I need to sort an Arraylist. The Arraylist is contained in classArt. The
Arraylist contains objects that have been defined in a sperate class, objArt.
I have attempted to implement the sort using IComparable, but I'm hopelessly
lost. With the addition of "Implements ICompariable" and the "CompareTo"
function the program will ont even compile.
Any suggestion that you have would be gratefully welcomed. I am
including the relevant code:
Public Class classArt
Implements IComparable
Private Shared ArtList As New ArrayList
Private ArtIndex As Integer
Private ArtFileName As String = "art.xml"
Public Sub SaveFile() 'save ArtList to ArtFile
Dim Msg1 As String = "ArtFile was not saved! Reason: "
Dim Msg2 As String = "Art"
Dim sf As New SoapFormatter
Dim fs As New FileStream(ArtFileName, FileMode.Create)
Try
sf.Serialize(fs, ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
End Try
End Sub
Public Sub GetFile() 'get ArtList from ArtFile
Dim Msg1 As String = "Failed to open ArtList! Reason: "
Dim Msg2 As String = "Art"
If System.IO.File.Exists(ArtFileName) Then
Dim fs As New FileStream(ArtFileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deserialize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
sf = Nothing
fs = Nothing
End Try
Else
AddArt()
End If
End Sub
Public Function CompareTo(ByVal xArt As objArt) As Integer Implements
System.IComparable.CompareTo
Dim xDes As String
xDes = xArt.Des
Return "hi".CompareTo(xDes)
End Function
Public Sub AddArt()
'add an empty objArt to ArtList
Dim emptyArt As New objArt
With emptyArt
.Des = ""
.Title = ""
.Pic = ""
.Type = ""
.Room = ""
.Location = ""
.Height = 0
.Width = 0
.Depth = 0
.Bequeth = ""
.Appraised = ""
.Value = 0
End With
ArtList.Add(emptyArt)
emptyArt = Nothing
End Sub
End Class
<[Serializable]()> Public Class objArt
'Implements IComparable
Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer
End Class
Arraylist contains objects that have been defined in a sperate class, objArt.
I have attempted to implement the sort using IComparable, but I'm hopelessly
lost. With the addition of "Implements ICompariable" and the "CompareTo"
function the program will ont even compile.
Any suggestion that you have would be gratefully welcomed. I am
including the relevant code:
Public Class classArt
Implements IComparable
Private Shared ArtList As New ArrayList
Private ArtIndex As Integer
Private ArtFileName As String = "art.xml"
Public Sub SaveFile() 'save ArtList to ArtFile
Dim Msg1 As String = "ArtFile was not saved! Reason: "
Dim Msg2 As String = "Art"
Dim sf As New SoapFormatter
Dim fs As New FileStream(ArtFileName, FileMode.Create)
Try
sf.Serialize(fs, ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
End Try
End Sub
Public Sub GetFile() 'get ArtList from ArtFile
Dim Msg1 As String = "Failed to open ArtList! Reason: "
Dim Msg2 As String = "Art"
If System.IO.File.Exists(ArtFileName) Then
Dim fs As New FileStream(ArtFileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deserialize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
sf = Nothing
fs = Nothing
End Try
Else
AddArt()
End If
End Sub
Public Function CompareTo(ByVal xArt As objArt) As Integer Implements
System.IComparable.CompareTo
Dim xDes As String
xDes = xArt.Des
Return "hi".CompareTo(xDes)
End Function
Public Sub AddArt()
'add an empty objArt to ArtList
Dim emptyArt As New objArt
With emptyArt
.Des = ""
.Title = ""
.Pic = ""
.Type = ""
.Room = ""
.Location = ""
.Height = 0
.Width = 0
.Depth = 0
.Bequeth = ""
.Appraised = ""
.Value = 0
End With
ArtList.Add(emptyArt)
emptyArt = Nothing
End Sub
End Class
<[Serializable]()> Public Class objArt
'Implements IComparable
Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer
End Class