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(ArtF ileName, FileMode.Create )
Try
sf.Serialize(fs , ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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(ArtFileN ame) Then
Dim fs As New FileStream(ArtF ileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deseri alize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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.ICompara ble.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(emp tyArt)
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
--
GrandpaB 8 2641
GrandpaB,
Just use a datatable, that has all that you ask standard in system.data
I hope this helps,
Cor
GrandpaB,
Or if you for what ever reason don't want a datatable (I never saw here
anything else than silly reason for that, with the except from somebody who
told he did it to study), than make yourself a new arrayclass using
collectionbase http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,
Cor
"GrandpaB" <Gr******@discu ssons.microsoft .com> schreef in bericht
news:AD******** *************** ***********@mic rosoft.com... 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(ArtF ileName, FileMode.Create ) Try sf.Serialize(fs , ArtList) Catch ex As Exception MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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(ArtFileN ame) Then Dim fs As New FileStream(ArtF ileName, FileMode.Open) Dim sf As New SoapFormatter Try ArtList = CType(sf.Deseri alize(fs), ArrayList) Catch ex As Exception MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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.ICompara ble.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(emp tyArt) 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 -- GrandpaB
Hi,
There is a sortedlist class http://msdn.microsoft.com/library/de...classtopic.asp
Ken
-----------------------
"GrandpaB" <Gr******@discu ssons.microsoft .com> wrote in message
news:AD******** *************** ***********@mic rosoft.com... 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(ArtF ileName, FileMode.Create ) Try sf.Serialize(fs , ArtList) Catch ex As Exception MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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(ArtFileN ame) Then Dim fs As New FileStream(ArtF ileName, FileMode.Open) Dim sf As New SoapFormatter Try ArtList = CType(sf.Deseri alize(fs), ArrayList) Catch ex As Exception MsgBox(Msg1 & ex.Message, MsgBoxStyle.Cri tical, 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.ICompara ble.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(emp tyArt) 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 -- GrandpaB
Thank you for your suggestions. As you can tell, I'm new to VB.Net & am not
familiar with all the resourceses of the language. I chose to create the art
data in an XML file because it would contain only 250 items at most and so
that the data file would be viewable in a browser. The Arraylist looked
inviting with all its methods including the sort method. Are you, as MVP's,
suggesting that my uninformed choice was not workable? Is there no way to
make the ArrayList work with IComparable or would it be so difficult that I
should just start over?
In reviewing your suggestions, I think that I'll attempt to add a SortedList
subclass to the class, classArt. The key in the Sortedlist key will contain
the values from the Arraylist that I want to sort on and Sortedlist value
will contain the index of the Arraylist. Do you have any other words of
wisdom before I embark on this approach?
Thanks again.
--
GrandpaB
Grandpa,
That XML does let me choose now even more for a dataset/datatable
\\\
dim ds as new dataset
dim dt as new datatable
ds.tables.add(d t)
for each i as integer = 1 to 250
dt.columns.add( "i.toString ")
next 'this makes 250 columns with the name 1 to 250
for i = 0 to 1
dt.addrow(dt.ne wrow)
for y = 0 to 249
dt(i)(y) = i.tostring
next
next ' this makes 2 rows filled all with 0 and 2
ds.writexml("c: \grandpa.xml")
ds.tables(0).de faultview.sort = "1" 'sort on the column with the name 1
datagrid1.datas ource = ds.tables(0).de faultview
ds.readxml("c:\ grandpa.xml")
///
In my opinion do you have here roughly typed all that you need.
I hope this helps,
Cor
"GrandpaB" <Gr******@discu ssons.microsoft .com> schreef in bericht
news:49******** *************** ***********@mic rosoft.com... Thank you for your suggestions. As you can tell, I'm new to VB.Net & am not familiar with all the resourceses of the language. I chose to create the art data in an XML file because it would contain only 250 items at most and so that the data file would be viewable in a browser. The Arraylist looked inviting with all its methods including the sort method. Are you, as MVP's, suggesting that my uninformed choice was not workable? Is there no way to make the ArrayList work with IComparable or would it be so difficult that I should just start over?
In reviewing your suggestions, I think that I'll attempt to add a SortedList subclass to the class, classArt. The key in the Sortedlist key will contain the values from the Arraylist that I want to sort on and Sortedlist value will contain the index of the Arraylist. Do you have any other words of wisdom before I embark on this approach?
Thanks again. -- GrandpaB
Cor,
Thanks; I had not realized that it was so easy to create a datatable from
and XML file. You have outlined an intreguing approach and if I can't get
the IComparable to work I'll give your suggestion a try.
--
GrandpaB
After finding an example about Collections, I believe that the IComparable
interface and the CompareTo function should be in the class, objArt. Then
the Arraylist, which is list of objArt objects, can be sorted by calling the
Arraylist sort method, Arraylist.Sort( ).
This is the current state of the objArt class; it contains two errors! Any
help eliminating these errors would be appreciated?
<[Serializable]()> Public Class objArt
Implements IComparable 'Error #1
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
Public Function CompareTo(ByVal xArt As objArt) As Integer _
Implements System.ICompara ble.CompareTo 'Error #2
Return Me.Title.Compar eTo(xArt.Title)
End Function
End Class
Error #1 - 'Art2.objArt' must implement 'Overridable Function CompareTo(obj
As Object) As Integer' for interface 'System.ICompar able'.
Error#2 - 'CompareTo' cannot implement 'CompareTo' because there is no
matching function on interface 'IComparable'.
--
GrandpaB
Problem Solved!!!
The function CompareTo is fussy; it wants the parameter xArt to be passed as
an object, not objArt.
Public Function CompareTo(ByVal xArt As Object) As Integer _
Implements System.ICompara ble.CompareTo 'Error #2
Return Me.Title.Compar eTo(xArt.Title)
End Function
--
GrandpaB
"GrandpaB" wrote: After finding an example about Collections, I believe that the IComparable interface and the CompareTo function should be in the class, objArt. Then the Arraylist, which is list of objArt objects, can be sorted by calling the Arraylist sort method, Arraylist.Sort( ).
This is the current state of the objArt class; it contains two errors! Any help eliminating these errors would be appreciated?
<[Serializable]()> Public Class objArt Implements IComparable 'Error #1
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
Public Function CompareTo(ByVal xArt As objArt) As Integer _ Implements System.ICompara ble.CompareTo 'Error #2 Return Me.Title.Compar eTo(xArt.Title) End Function
End Class
Error #1 - 'Art2.objArt' must implement 'Overridable Function CompareTo(obj As Object) As Integer' for interface 'System.ICompar able'.
Error#2 - 'CompareTo' cannot implement 'CompareTo' because there is no matching function on interface 'IComparable'.
-- GrandpaB This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Leon |
last post by:
I have six textbox controls on my webform that allows the user to enter any
numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. How can
accomplish this task? Thanks!
|
by: Gunjan Garg |
last post by:
Hello All,
I am working to create a generic datagrid which accepts a
datasource(ListData - This is our own datatype) and depending on the calling
program customizes itself for sorting, paginantion or accepting the add and
remove item events.
What i am observing is that none of the vents are happening... (Sort, page,
or item). I am sure I am missing something basic here... Need help... Thanks
much
|
by: Adam J. Schaff |
last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class
does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii
values) but what I got was the opposite. Simple code:
Dim y As New ArrayList
y.Add("Abc")
y.Add("abc")
y.Add("_bc")
|
by: Derek Martin |
last post by:
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains objects
and one of the members of the object is 'name.' I would like to sort the
arraylist based on object.name - is that possible?
Thanks!
Derek
|
by: Grant Wickman |
last post by:
Our team has just fixed a really nasty problem that appears to be
caused by an obscure bug in the sort method of Arraylist and
daisy-chained webservice stubs.
We've fixed the bug so I'll not investigate any further but there was
nothing about it when googling the newsgroups so I am posting a note in
case it helps somebody else out.
The problem was in a system with a web front end calling through to a
chain of 3 webservices i.e.
| |
by: Alex Chudnovsky |
last post by:
I have come across with what appears to be a significant performance bug in
..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same
data. Same data on the same CPU gets sorted a lot faster with both methods
using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below
you can find C# test case that should allow you to reproduce this error, to
run it you will need to put 2 data files into current directory...
|
by: M Harvey |
last post by:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?
Thanks,
Matt
|
by: jtfaulk |
last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional
I have a multi-dimensional arraylist, and I would like to sort one
level of it but not all. The multi-dimensional arraylist represents my
menu system, and I would like to sort the third level menu items only
and not the first two.
So, I've been trying to use some of the suggestions I've found here
about sorting. Mainly, I've created a comparison interface.
|
by: rn5a |
last post by:
Can the items in a ListBox be sorted by the name of the items?
The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.
|
by: djp |
last post by:
Hi
I have to sort arraylist. I tried to do this using this page as a reference:
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm
I did it exactly the same way but unfortunately in opposite to the example
from this link here it does not sort.
I Create Nodes ( color, name ) and add them to nodeList in Graph class. Then
i try to sort..
The code is relatively simple:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |