|
You may want to use a generic list instead. This article[^] shows you how to add a generic sort routine.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
I use List<...> for a lot of code. I find it convenient to be able to have a wrapped list. Here's an example with a simple method to demonstrate how easy it is:
using System.Collections.Generic;
using System;
namespace Generics.Test
{
public class TestGeneric
{
public TestGeneric()
{
MyList<MyTest> test = new MyList<MyTest>();
test.Add(new MyTest("Test 1"));
test.Add(new MyTest("Test 2"));
if (test.HasValue("Test 2"))
Console.WriteLine("Test 2 is present");
}
}
public interface ISomeValue
{
string SomeValue { get; set; }
}
public class MyList<T> : List<T>, IEnumerable<T> where T : ISomeValue
{
public bool HasValue(string key)
{
foreach (T item in this)
{
if (item != null && item.SomeValue == key)
return true;
}
return false;
}
#region IEnumerable<T> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
for (int i = 0; i < this.Count; i++)
{
yield return this[i];
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi there,
Thanks for the sample code. I've gone to use it this morning and hit another problem.
I've created this:
<br />
public interface RequestCollectionInterface<br />
{<br />
ImageList Images { get; set; }<br />
int OpenCount { get; }<br />
int PendingCount { get; }<br />
int ImmediateCount { get; }<br />
int HighCount { get; }<br />
}<br />
public class RequestCollection<T> : List<T>, IEnumerable<T> where T : RequestCollectionInterface<br />
{<br />
IEnumerator<T> IEnumerable<T>.GetEnumerator()<br />
{<br />
for (int i = 0; i < this.Count; i++)<br />
{<br />
yield return this[i];<br />
}<br />
}<br />
IEnumerator IEnumerable.GetEnumerator()<br />
{<br />
throw new Exception("The method or operation is not implemented.");<br />
}<br />
public int OpenCount<br />
{<br />
get<br />
{<br />
int ReturnValue = 0;<br />
foreach (Request item in this)<br />
{<br />
if (item.Status == RequestStatus.Open)<br />
ReturnValue++;<br />
}<br />
return ReturnValue;<br />
}<br />
}<br />
public int PendingCount<br />
{<br />
get<br />
{<br />
int ReturnValue = 0;<br />
foreach (Request item in this)<br />
{<br />
if (item.Status == RequestStatus.Pending)<br />
ReturnValue++;<br />
}<br />
return ReturnValue;<br />
}<br />
}<br />
public int ImmediateCount<br />
{<br />
get<br />
{<br />
int ReturnValue = 0;<br />
foreach (Request item in this)<br />
{<br />
if (item.Priority = RequestPriority.Immediate)<br />
ReturnValue++;<br />
}<br />
return ReturnValue;<br />
}<br />
}<br />
public int HighCount<br />
{<br />
get<br />
{<br />
int ReturnValue = 0;<br />
foreach (Request item in this)<br />
{<br />
if (item.Priority = RequestPriority.High)<br />
ReturnValue++;<br />
}<br />
return ReturnValue;<br />
}<br />
}<br />
}<br />
And tried using it like this:
<br />
<br />
private RequestCollection<Request> __GetRequests()<br />
{<br />
RequestCollection<Request> rc = new RequestCollection<Request>();<br />
Request r = new Request();<br />
r.Id = 1;<br />
r.Summary = "This is a test";<br />
r.To = "Test";<br />
r.From = new User();<br />
r.Priority = RequestPriority.Normal;<br />
r.Status = RequestStatus.Open;<br />
r.Created = DateTime.Now;<br />
r.Assigned = r.Created;<br />
r.Parent = rc;<br />
r.Hours = r.Created.Subtract(r.Assigned).Hours;<br />
<br />
rc.Add(r);<br />
return rc;<br />
}<br />
but I get the following error:
<br />
Error 3 The type 'HelpDesk.Request' must be convertible to 'HelpDesk.RequestCollectionInterface' in order to use it as parameter 'T' in the generic type or method 'HelpDesk.RequestCollection<T>' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\HelpDesk\HelpDesk\DataAccess.cs 66 44 HelpDesk<br />
this is my Request code:
<br />
class Request<br />
{<br />
private int _Id;<br />
private string _Summary;<br />
private User _From;<br />
private string _To;<br />
private RequestPriority _Priority;<br />
private RequestStatus _Status;<br />
private DateTime _Created;<br />
private DateTime _Assigned;<br />
private DateTime _LastChanged;<br />
private double _Hours;<br />
private RequestCollection<Request> _Parent;<br />
<br />
public int Id<br />
{<br />
get { return _Id; }<br />
set { _Id = value; }<br />
}<br />
public string Summary<br />
{<br />
get { return _Summary; }<br />
set { _Summary = value; }<br />
}<br />
public User From<br />
{<br />
get { return _From; }<br />
set { _From = value; }<br />
}<br />
public string To<br />
{<br />
get { return _To; }<br />
set { _To = value; }<br />
}<br />
public RequestPriority Priority<br />
{<br />
get { return _Priority; }<br />
set { _Priority = value; }<br />
}<br />
public RequestStatus Status<br />
{<br />
get { return _Status; }<br />
set { _Status = value; }<br />
}<br />
public DateTime Created<br />
{<br />
get { return _Created; }<br />
set { _Created = value; }<br />
}<br />
public DateTime Assigned<br />
{<br />
get { return _Assigned; }<br />
set { _Assigned = value; }<br />
}<br />
public DateTime LastChanged<br />
{<br />
get { return _LastChanged; }<br />
set { _LastChanged = value; }<br />
}<br />
public double Hours<br />
{<br />
get { return _Hours; }<br />
set { _Hours = value; }<br />
}<br />
public RequestCollection<Request> Parent<br />
{<br />
get { return _Parent; }<br />
set { _Parent = value; }<br />
}<br />
public Image PriorityImage<br />
{<br />
get<br />
{<br />
Image ReturnValue = null;<br />
try<br />
{<br />
if (this.Parent != null && this.Parent.Images != null)<br />
ReturnValue = this.Parent.Images.Images[(int)this.Priority];<br />
}<br />
catch (Exception ex)<br />
{<br />
int i = 99;<br />
}<br />
return ReturnValue;<br />
}<br />
}<br />
}<br />
|
|
|
|
|
You've become slightly mixed up in what you need the interface to be. When a generic states where T : ... you are indicating that the type T will implement the interface. Also, you have used a generic list and then hardcoded the type in the iterator. To be honest, you possibly don't need to expose your class as generic because you have a specific non-generic need here. What you could do is declare a List<...> member as a private member and then use your own methods to add to it. If you definitely want to go the generic route here, this is a sample that I have just knocked up.
using System;
using System.Collections.Generic;
using System.Text;
namespace TestIterator
{
class Program
{
static void Main(string[] args)
{
RequestItemCollection<Request> coll = new RequestItemCollection<Request>();
Request req = new Request();
req.Status = RequestStatus.Open;
coll.Add(req);
req = new Request();
req.Status = RequestStatus.Open;
coll.Add(req);
System.Console.WriteLine("Open count = {0}", coll.OpenCount);
Console.ReadLine();
}
}
public class RequestItemCollection<T> : List<T>, IEnumerable<T> where T: IStatus
{
#region IEnumerable<T> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
for (int i = 0; i < this.Count; i++)
yield return this[i];
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
public int OpenCount
{
get
{
return StatusCount(RequestStatus.Open);
}
}
public int Closed
{
get
{
return StatusCount(RequestStatus.Closed);
}
}
private int StatusCount(RequestStatus status)
{
int count = 0;
foreach (T item in this)
{
if (item.Status == status)
count++;
}
return count;
}
}
public enum RequestStatus
{
Open,
Closed,
Pending
}
public interface IStatus
{
RequestStatus Status { get; set; }
}
public class Request : IStatus
{
private RequestStatus _status;
#region IStatus Members
public RequestStatus Status
{
get
{
return _status;
}
set
{
_status = value;
}
}
#endregion
}
}
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello,
I think you have to call the Sort method of the InnerList property.
There you have to pass an instance of a UserComparer class inheritet from IComparer.
For excample, if your Request class has a Name property which you whant to use for sorting:
public class YourComparer : IComparer
{
int IComparer.Compare( object x, object y )
{
Request request_x= x as Request ;
Request request_y= y as Request ;
if((request_x!=null)&&(request_y!=null))
{
return( (new CaseInsensitiveComparer()).Compare( request_x.Name, request_y.Name ) );
}
else
{
return 0;
}
}
}
Hope it helps!
All the best,
Martin
|
|
|
|
|
I am working with a mobile device. I have a 3GPP video file with the extension .3gp. I want to read the first frame of the video file which contains the format, header realted infos. I want to dispaly the same with as jpeg or gif extension. Can anyone help me regarding this. If anyone is having any source code for that or if anyone can suggest any open sources for this then it will be great.
Thanks & Regards
Ramanath Panda
Ramanath Panda
Bangalore
|
|
|
|
|
Hi,
I have a print preview control that works fine. I've added a tool strip button to allow the user to change the printer settings, this button loads up the PrintDialog.
What i want to do is if the user changes the page layout to be landscape, once they click apply it should redraw the preview. The problem is, how do i fire the PrintPage event to redraw the preview?
Regards,
Gareth.
|
|
|
|
|
I found out on the MS forums that i needed to use: PrintPreviewControl.InvalidatePreview();
Regards,
Gareth.
|
|
|
|
|
Hi
I am doing an application in which I am using a XML file to populate a datagrid.
In that XML file I have one like.....
1.To generate leads efficiently and profitably.
2.To ensure that all details are collected in one call.
3.To provide email and chat support.
4.To manage Work Basket reports as required.
5.To meet targets for both quantity and quality.
When I am trying to populate this column onto datagrid, all these lines or sentences are getting combine and displaying like a paragraph.
I want to know how to format this column to get them in the a new line
Thank u
Chandu
|
|
|
|
|
You could try to add the newline escape (\r\n) chars where you need a new line. I do not know if this will work though.
|
|
|
|
|
Hello,
I have big problem.Please help me.
I playing video with QuartzTypeLib.Video frame are drawing in panel component
and I need drawing my picture(rectangle) on an video.But I dont know which event is calling redrawing frame.
I use protected override onPaint but this metod is call only first time and my picture is redrawing with video frame
How can I do?
I'M beginner in C#
|
|
|
|
|
It's a *lot* more complicated than that. Video is played for you by DirectX, or perhaps another library, and you need to hook into that, the paint event has nothing to do with it.
There's info on the web, it can be done. I am not familiar with QuartzTypeLib, why are you not using the Windows Media player ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Mh...
I use WMP is same how Quartztypelib or in DirectX is simples?
Can I do with Dx SDK?
I'M beginner in C#
|
|
|
|
|
Hello together,
has anybody an idea how I can check with C# if the Oracle client is installed at the computer?
Greets
Patrick
|
|
|
|
|
Check for registry entrys.
Regards,
Gareth.
|
|
|
|
|
Which ones?
Greets
Patrick
|
|
|
|
|
Has nobody an idea? How do I get the version of the installed ORACLE client from C#??? Please help, it's urgent. Thanks for your help.
Greets
Patrick
|
|
|
|
|
How to write code to install database into SQL Server ?
-- modified at 4:26 Tuesday 22nd May, 2007
|
|
|
|
|
What do you mean by install?
Do you want to create a database?
|
|
|
|
|
I think he means attaching a database
|
|
|
|
|
was it write a cookies?
if it was write a cookies,
How do write and read?
please give me directions, thanks.
|
|
|
|
|
Windows application or ASP.NET?
Windows application could be done several ways, you can write an encrypted file with all the login information, system registry(better not to), or store it in the application settings. I would suggest the first solution.
ASP.NET/ASP, then you would use cookies.
To do the reading/writing of a file, use FileStream , and StreamReader/StreamWriter .
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
|
|
|
|
|
I am using an xml and in that childnodes have one attribute which is optional, so that attribute may be present or not.
in front-end from c#.net 2005 i am reading that attribute, when it is not present it giving exception.
so how to find that there is no attribute for that childnode in runtime.
Thanks in advance
PraveenKumar Palla
|
|
|
|
|
Hello,
I think Reflection is what you are looking for.
The way from the object to the Attributes would be like this.
using System.Reflection;
Type t = yourObject.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if(pi.PropertyType.Attributes & TypeAttributes.Serializable)
{
}
}
using System.Reflection;
Type t = yourObject.GetType();
object[] AllAttributes = t.GetCustomAttributes(true);
Hope it helps!
All the best,
Martin
|
|
|
|