|
It would help if you posted exact code you are using including input strings and how you are displaying the output.
FYI - text literals for time zones are NOT unique. So if you have a specific problem then using that might work but it will not work in all cases.
|
|
|
|
|
I have a List<> of ShipSprite (a struct). I need to "sort" that list very often. But every once in a while, it Throws this Exception:
ArgumentException.Message: IComparer (or the IComparable methods it relies upon) did not return zero when Array.Sort called x. CompareTo(x). x: '' x's type: 'ShipSprite' The IComparer: 'ChangeDBLOL.ShipSpriteComparer'.
Here is the code for the IComparer.Compare method:
int IComparer<ShipSprite>.Compare(ShipSprite x, ShipSprite y)
{
if (!(x.Confirmed ^ y.Confirmed))
{
if (x.State == y.State)
{
if (x.TA == y.TA)
{
if (x.Equals(y)) {
return 0;
}
else {
int res = Utils.r.Next(2) == 0 ? 1 : -1;
return res;
}
}
else
{
return x.TA.CompareTo(y.TA);
}
}
else
{
return x.State.CompareTo(y.State);
}
}
if (x.Confirmed) return -1;
if (y.Confirmed) return 1;
return 0x1Dead;
}
Just to be clear, I use this code for testing:
List<ShipSprite> sort = new List<ShipSprite>(sprites);
int k = 0;
StringBuilder sb = new StringBuilder();
while (true)
{
sb.AppendLine((k++).ToString());
sort.Sort(new ShipSpriteComparer());
sb.AppendLine("~~~~~Result list~~~~~");
foreach (ShipSprite s in sort)
{
sb.AppendLine(s.ToString());
}
}
What bugs me is that this exception only occurs once every 25~40 sort. Do you guys have any idea of what might be the problem?
Oxfords English < Official CCC Players Dictionary
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
modified 8-Dec-11 22:14pm.
|
|
|
|
|
|
Well,This List is used in Genetic algorithm, so I need to "sort" it very often. Here's the rule for sort:
1. Confirmed ShipSprite goes first
2. Sort according ShipStates
3. If above return 0, sort based on TA
4. If above return 0, check if it's the same ShipSprite and return 0 if it's same (so that we don't get that error)
4b. Random any ShipSprite that doesn't fulfill above conditions.
Oxfords English < Official CCC Players Dictionary
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
Hi,
1.
I would write all that quite differently, see consecutive diff calculations under "composite sort criteria" in my article here[^].
2.
I don't like your random stuff a bit, and I guess neither does the sorting algorithm. It really wants IComparer(x,y) to be trustworthy, i.e. to ALWAYS return the same result. If you need some randomness, I think you should somehow store the result of the RNG and make sure you return consistent values (otherwise X<Y and Y<Z might not imply X<Z , which would upset most everyone).
[EDIT] The right thing to do for randomness would be like this: give your "objects" one more field, which you fill with random integer numbers beforehand; then use that field as the last sort criterion. [/EDIT]
[ADDED] FWIW: If I were to need data sorted, the first thing I'd do is make them real objects (i.e. reference type), not structs. It would avoid all boxing/unboxing pitfalls and costs, and it most likely would sort faster as now only the pointers need being swapped, not the entire object values. [/ADDED]
|
|
|
|
|
What confuses me is that the previous version works fine even with this "random sort"
Yeah, I'll try to add a field and fill it with random value before sort. Also, that struct is a gift from previous developer.
Oxfords English < Official CCC Players Dictionary
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
A sort algorithm should always return consistent results, i.e. don't do the random thing. If two items are sort-order-equivalent, the comparison should return 0.
Why are you doing that weird XOR check when it seems to be equivalent to [ if(x.Confirmed == y.Confirmed) ]? Not that I think that's responsible for the problem but it is needlessly confusing.
What types are TA and State, and do they have comparators (or equality operator overrides) which could mess things up?
Did you override Equals on ShipSprite, and if so did you do it correctly?
As an aside, why is this a struct? It looks complex enough that it should be a class. Sorting a list of structs requires moving around and copying a lot more bits of memory.
|
|
|
|
|
Well, TA is a DateTime and State is an int, so they shouldn't mess things up. I have also checked Equals on ShipSprite and it seems fine. I will do as Luc's suggestion, create a new field and fill it with random value before sorting.
That XOR and struct is results of fluctuate requirement and management's motto "Previous developer did it fine this way, don't change it!"
Oxfords English < Official CCC Players Dictionary
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
Hi all,
I'm porting a C# 2 application from Visual Studio 2005 to 2010 .Net 4, and getting a baffling error:
"Error 1 'Could not load file or assembly 'Aurigma.GraphicsMill' or one of its dependencies. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)' C:\Alan\Label Designer\64-bit\Source\LabelDesigner\LC LabelDesigner"
It's complaining about not being able to load Aurigma.GraphicsMill but the damn DLL is there! It's maddening. The same files run perfectly under VS 2005. It also mentions an "LC" file, but no file has anything like that name anywhere under this project folder, or the original which compiles.
I tried deleting the GraphicsMill reference and re-adding it, but got the same results.
Wasted hours on this; I'm calling it a day.
Can anyone make any suggestions of what to try next?
modified 12-Dec-11 11:24am.
|
|
|
|
|
I noticed the 64-bit in your path above. Could it be that you are creating a 64-bit app and this dll is a) stored in the 32-bit directory or b) not available in 64-bit? Just a guess.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thanks for the suggestion, but at this point it's 32-bit. (64-bit is a later goal, but the first step is just to get it working under VS 2010.)
|
|
|
|
|
I put that error code into Google and came up with this[^]; the first link looks like it may be of use - if you understand what they are saying.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thanks. I did the same thing! Great minds think alike. Microsoft has no workaround for this artificial "bug" they've caused. I've emailed Aurigma to see they have an updated DLL for .NET 4.
|
|
|
|
|
|
Hi,
I have a "asp:listview" bind to web service without database.
I don't use "ObjectDataSource" and in my contol "ListView" : I haven't atribut "DataKeyNames" and "DataSourceID"
I bind to it in code-behind
protected void Page_Load(object sender, System.EventArgs e)
{
Act ActWEBService = WebServiceFactory.CreateInstance().CreateActWebService();
ActStruct[] myActs = ActWEBService.GetActs(nameIdItemDataBound);
mSeenFromPageState.Acts = new ActStruct[myActs.Length];
foreach(ActStruct act in myActs)
{
ListViewActs.DataSource = ActWEBService.GetActs(nameIdItemDataBound);
ListViewActs.DataBind();
}
protected void OurListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
Act ActWEBService = WebServiceFactory.CreateInstance().CreateActWebService();
ActStruct[] myActs = ActWEBService.GetActs(nameIdItemDataBound);
So here is my list on screen :
nameIdItemDataBound....thatOneId............name
11BB...................................1..............................john
13AA...................................3..............................alice
15AAA..................................2..............................roxy
16TXQ..................................1..............................jean
But I want items that have [3] in their [thatOneId], does not appear in my list!
"if statement" does not use with Listview so I must put a filter...
like this : Where(a => a.name != value);
oreach(ActStruct act in myActs)
{
ListViewActs.DataSource = ActWEBService.GetActs(nameIdItemDataBound).Where(d => d.thatOneId != 3);
ListViewActs.DataBind();
}
I have a message error :
'ListView with id 'ListViewActs' must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true.' (line Ligne : 82 )
Could you help me for this ?
|
|
|
|
|
Hi all,
I seem to be having a problem with value validation of a list in a foreach loop and was wondering if you could please point out where the logic/syntactic error is?
Basically I am enumerating the Windows Application log for an eventid value then for a timewritten value. If the value is not found the code should run. If the value is found but is older than 1 hour then the code should run. If the value is found and is not 1 hour old then the code should not run. This is what I have but the code runs on my 2-minute timer anyway indicating that the value is not being found (I think)
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
int run;
run = 0;
EventLog eLog = new EventLog("Application");
foreach (EventLogEntry entry in eLog.Entries)
{
if (entry.EventID == 13162)
{
List<int> evid = new List<int>();
evid.Add(entry.EventID);
foreach (int IDNo in evid)
{
if (entry.TimeWritten > DateTime.Now.AddHours(-1))
{
run = 1;
}
else run = 2;
}
}
}
if(run == 0 ||run == 1)
{
}
if (!EventLog.SourceExists("mylog")) EventLog.CreateEventSource("mylog", "Application");
EventLog.WriteEntry("mylog", "code has run", EventLogEntryType.Information, 13162);
}
This has been driving me nuts for nearly 3 days now and I just can't figure it out. I have tried evid.contains and evid.find methods but either the //do stuff won't run at all or it keeps running on the timedevent, i.e. every 2 minutes. Could you help please?
|
|
|
|
|
I would change this piece of code
foreach (EventLogEntry entry in eLog.Entries)
{
if (entry.EventID == 13162)
{
List<int> evid = new List<int>();
evid.Add(entry.EventID);
foreach (int IDNo in evid)
{
if (entry.TimeWritten > DateTime.Now.AddHours(-1))
{
run = 1;
}
else run = 2;
}
}
}
for
foreach (EventLogEntry entry in eLog.Entries)
{
if (entry.EventID == 13162)
{
if (entry.TimeWritten > DateTime.Now.AddHours(-1))
{
run = 1;
}
else run = 2;
}
}
}
Please let me know if it works.
|
|
|
|
|
Hi emardini,
Nope, still runs every 2 minutes. I am currently thinking that "13162" is not a valid eventid number. By calling the other numbers it seems that are not ints, they are bytes[] converted to ints for legibility.
Still investigating but will advise
|
|
|
|
|
I have a question about a C#. net 2008 desktop where I am working with .XSD files (datasets and dataadapters). I need to add a capability for a user to enter a 'alt-id' key as a search key to look up lots of information about a customer. To accomplish this goal a stored procedure with lots of 'left outer join statments' is called to obtain the information.
When the stored procedure is executed on its own, it runs in all cases. When the C#.net app executes in runs in all cases, except when there there is only data in the 'alt-key' column. Basically all the other tables that are associated in the join do not data the correspsonding data.
I am getting the error 'Failed to enable constraints. One or more rows contain values violating non-null,unique, or foreign-key constraints.'
The error occurs on the statment where I try to fill a dataset.
Thus to solve this problem, I am trying to decide what to do.
What you do think my options are:
1. Is there some setting I should change in the dataset so at least the 'alt-key' value is returning and all the other joined columns contain null as their value?
2. Should I do some kind of a preselect to see if any real data exists? if so how would you code that?
3. Do you have other suggestions? If so, can you tell me and/or point me to a reference where I can solve this problem?
|
|
|
|
|
I have a list of these models:
public class NodeModel : ViewModelBase
{
public Guid Id { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public int Sequence { get; set; }
public ImageSource Image { get; set; }
public bool IsExpanded { get; set; }
public string FileName { get; set; }
public NodeType Type { get; set; }
public List<NodeModel> Children { get; set; }
public NodeModel()
{
Children = new List<NodeModel>();
FileName = string.Empty;
}
}
The list is contained in a collection called Folders. I want to remove an object from the collection, so I'm using:
Folders.Remove(f => f.Id == model.Id);
But it's not removing it, What's wrong here?
Everything makes sense in someone's mind
|
|
|
|
|
I know I'm stating the bloody obvious, but have you placed a breakpoint on the Folders.Remove statement to make sure you have a proper model.Id? You can also convert the statement into a foreach temporarily so that you have more debugging visiblity. Once the problem is solved, I would certainly return the statement into the linq syntax, though.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Remove doesn't take a predicate as far as I know.
Either use .RemoveAll(f => f.Id == model.Id) or use Remove(specificInstanceOfNode)
All the best,
Dan
|
|
|
|
|
Ok, so now I have this:
NodeModel folder = (from f in Folders
where f.Id == model.Id
select f).FirstOrDefault();
Folders.Remove(folder);
The query is returning null. It's not finding the folder.
It appears that the query is only looking at the first level and not drilling down into the collection.
Everything makes sense in someone's mind
|
|
|
|
|
Yes. It sure doesn't. I completely missed the inner list.
So you want to remove from the ChildNode inner list or from the main/upper list or from both?
All the best,
Dan
|
|
|
|
|
I want to find i anywhere in the list and remove it.
Everything makes sense in someone's mind
|
|
|
|