|
Hi, Rob, I assumed, based on the OP's use of a SortedSet that sorting was required. If a more fancy sort is required, then, of course, you could write a custom sort function.
My observation of the behavior of 'Distinct is that eliminates duplicates whose ordinal position is greater in the structure, but, there could well be dimensions of its behavior I am unaware of for other Types/Collections.
cheers, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
Rob Philpott wrote: If you drop the OrderBy(), I think there are no guarantees about preserving order.
The documentation[^] doesn't seem to explicitly mention it, other than saying it "returns an unordered sequence".
But looking at the source code[^], the sequence returned from Distinct will be in the same order as the input sequence:
static IEnumerable<TSource> DistinctIterator<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) {
Set<TSource> set = new Set<TSource>(comparer);
foreach (TSource element in source)
if (set.Add(element)) yield return element;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am relieved to know (for once) the source matches my observation of a very limited sample-set
cheers, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
Yeah, it's tricky isn't it. Documentation doesn't seem to state that order is guaranteed not to change, but implementation indicates that's the case. I can't really imagine how you can improve much on that implementation either (which is strikingly similar to the initial posted implementation), so it's probably fair to assume no reordering will occur.
But without that cast-iron guarantee, a future version of .NET could scupper things. Well hey, that's what consultancy rates are for.
The bit which has me intrigued now is the Set<t> class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: The bit which has me intrigued now is the Set<t> class. Didn't know there was such a thing. HashSet would be my go to choice, so I presume its an internal-to-framework class.
Yes, it's an internal class within the Enumerable class: Enumerable.Set<TElement>[^]
I'd have used a HashSet<T> as well, but I guess MS were probably writing the Distinct method before the HashSet class was finished.
Either that, or there were some very specific performance issues they were trying to work around. But if that was the case, I'd have expected to see a comment explaining the problem they were trying to solve.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Interesting!!
I have never seen a method, like your RemoveDuplicatesButton_Click() use the => syntax at this point?! That actually works?
Ben Scharbach
Temporalwars.Com
YouTube:Ben Scharbach
|
|
|
|
|
i hope be ok
i want to learn how a month calender is writen
i search whole the web and couldent find source code for month calender in csharp
i will be appreciated to help me
|
|
|
|
|
You know that there are seven days in a week. So, that gives you some basic information that you need to get started. You need to decide what you want the first day of the week to be; is it Sunday or is it Monday? You can use the DateTime class to get what day of the week a given date falls on and use this to calculate how many days (if any) from the previous month would need displaying. You can use a similar technique for working out how many days of the next month need to be displayed. From there on, it's just a matter of drawing your cells and putting a date in (and as we don't know what platform your program is going to run on, we can't be any more help than that).
This space for rent
|
|
|
|
|
I would use the DateTime structure and TimeSpan structures to gain this information for the OS. Then I would put this into some type of Silverlight or WPF XAML format. Finally, I would use threading to free-up the UI from stalls, so the user can move around your wonderful calendar with ease!
Ben Scharbach
Temporalwars.Com
YouTube:Ben Scharbach
|
|
|
|
|
byte[] bytesToSend={0xA0, 0x04, 0x01, 0x89, 0x01,0xD1}; // command to send to device
mySerialPort.Write( bytesToSend,0,bytesToSend.Length); //writing to serial port
System.Threading.Thread.Sleep(200);
int count=mySerialPort.BytesToRead; //counting the bytes of response command
System.Console.WriteLine(count);
mySerialPort.Read(name,0,count); //read data from serial port
System.Console.WriteLine(Convert.ToBase64String(name)); //write data to console
Now i am having issue that my response command have 12 bytes and bytesToRead is giving 12 ..but i am not getting the exact response command/data .I am getting garbage value .what should i have to do ??
|
|
|
|
|
Start by using Hyperterminal or similar to check that you are communicating with the right device, on the right port, at the right communications settings. When you have that working fine, then transfer those details to you app and start establishing communications - but until you know that everything is working fine first, you have far too many variables to even start debugging comms in your own software. And using Thread.Sleep is not a good idea: the other device may or may not respond completely in that time - it's a better idea to use the SerialPort.DataReceived event to "build up" the response data instead of expecting comms to happen in a fixed timeframe.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes i already used Termite .The issue has been resolved .Thank you
|
|
|
|
|
mySerialPort.Read(name,0,count);
The "Read" returns the number of bytes actually read (which you don't check).
You need to compare the "BytesToRead" with the number actually retrieved, and do another "Read" if all the bytes have not yet been returned (it happens).
Yes; you can use the "received" event to "receive" data asynchronously; in all my "cases" however, it never worked properly and I did my own "reading" and "timeouts". And trying to access a "single" resource like a com port asynchronously for multiple "clients" is a difficult proposition.
The "sleep" is not a problem; particularly if the device being queried requires "settling time" (like a scale).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
without sleep ,it is not writing proper command to serial port. the issue has been resolved .thank you
|
|
|
|
|
Why do you use Convert.ToBase64String(name) ?
I am pretty sure that that's the line producing the garbage you see. I'd try System.Text.Encoding.ASCII.GetString() instead. But of course, a different encoding could be required depending on the specification of the sender.
|
|
|
|
|
yes you are right .when i used
BitConverter.ToString(data); .i got my required response. Thank you
|
|
|
|
|
my video/audio/text application give this exception specially when i allow video call.
project is tcp/ip base , allow video in picturebox by directx capture and for audio is NAudio.
butwhen i allow user to communicate with video also give above exception . i dont know why? plz help.
private void button_PuchVideo_Click(object sender, EventArgs e)
{
lblVCleint1.Visible = true;
lblVCleint2.Visible = true;
pictureBox.Visible = true;
pictureBox_Remote.Visible = true;
try
{
if (capture != null)
{
if (capture.PreviewWindow != panelVideo)
{
capture.PreviewWindow = panelVideo;
}
capture.FrameEvent2 += new Capture.HeFrame(CaptureDone);
capture.GrapImg();
PeerIP_TXT.Enabled = false;
button_PuchVideo.Enabled = false;
isSending = true;
}
}
catch (Exception) { }
}
void server()
{
try
{
server_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server_sock.Bind(new IPEndPoint(IPAddress.Any, 6000));
server_sock.Listen(-1);
while (true)
{
try
{
Socket new_socket = server_sock.Accept();
NetworkStream ns = new NetworkStream(new_socket);
pictureBox_Remote.Image = Image.FromStream(ns);
ns.Close();
new_socket.Close();
}
catch (Exception)
{
}
}
}
catch (Exception) { }
}
void SendVideoBuffer(object bufferIn)
{
try
{
TcpClient tcp = new TcpClient(PeerIP_TXT.Text, 6000);
NetworkStream ns = tcp.GetStream();
Image buffer = (Image)bufferIn;
lock(buffer)
buffer.Save(ns, System.Drawing.Imaging.ImageFormat.Jpeg);
ns.Close();
tcp.Close();
}
catch (Exception) { }
}
bool isSending = false;
private void CaptureDone(System.Drawing.Bitmap e)
{
try
{
this.pictureBox.Image = e;
if (isSending)
ThreadPool.QueueUserWorkItem(new WaitCallback(SendVideoBuffer), pictureBox.Image);
}
catch (Exception) { }
}
|
|
|
|
|
The exception will have a stack trace. This will tell you the exact line in your code that has threw the exception. It's a simple case of putting a breakpoint in your code and inspecting the value when you run through. The one that is null is the one that's causing you a problem.
This space for rent
|
|
|
|
|
I've just had a quick scan of your code. I would recommend that you move away from the idea of repeatedly opening and closing connections. These can end up being expensive operations and it is possible to starve your application of resources if you aren't careful.
This space for rent
|
|
|
|
|
Exception Details: System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Assembly Load Trace: The following information can be helpful to determine why the assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6' could not be loaded.
=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
(Fully-specified)
LOG: Appbase = file:///D:/MOJT/DAE_PIMS_New/Dae_PIMS/
LOG: Initial PrivatePath = D:\MOJT\DAE_PIMS_New\Dae_PIMS\bin
Calling assembly : Ext.Net, Version=1.2.0.24218, Culture=neutral, PublicKeyToken=2e12ce3d0176cd87.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\MOJT\DAE_PIMS_New\Dae_PIMS\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/mojt/7748f75a/267903e8/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/mojt/7748f75a/267903e8/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///D:/MOJT/DAE_PIMS_New/Dae_PIMS/bin/Newtonsoft.Json.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Stack Trace:
[FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +191
System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +162
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +148
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +604
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimePropertyInfo property, RuntimeType caType) +114
Ext.Net.ViewStateProcessor.GetProperties(Object obj) +580
Ext.Net.ViewStateProcessor.SaveViewState(Object obj) +79
Ext.Net.XControl.SaveViewState() +214
System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +103
System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +398
System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +398
System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +398
System.Web.UI.Control.SaveViewStateRecursive(ViewStateMode inheritedMode) +398
System.Web.UI.Page.SaveAllState() +685
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8302
|
|
|
|
|
|
fyi: System.Drawing.Color.Red has:
'IsKnownColor and 'IsNamedColor properties : 'true
Can you predict the results of the comparisons in this code ?
public void ColorChallenge()
{
int rval = Color.Red.ToArgb();
Color clr = Color.FromArgb(rval);
bool ismatchEq1 = Color.Red.Equals(clr);
bool ismatchEq2 = Color.Red == clr;
bool ismatchEq3 = rval == clr.ToArgb();
bool isknown = clr.IsKnownColor;
bool isnamed = clr.IsNamedColor;
} And for extra dollops of delicious pro-social goodness, predict the Type of the variable 'hx stored in 'hxtype:
public void ColorChallenge2()
{
int rval = Color.Red.ToArgb();
string hex = $"0x{rval:X8}";
var hx = 0xFFFF0000;
string hxtype = hx.GetType().Name;
} Notice anything interesting ?
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
modified 30-Jun-17 22:10pm.
|
|
|
|
|
BillWoodruff wrote: Can you predict the results of the comparisons in this code ? No, but I can guess. A Color would be a struct (even the "Red" property is a struct), and you are comparing that to an integer. If I "ToString" a color, I'd get a description of that color in a non-ARGB format. Makes me wonder how the comparer is implemented
BillWoodruff wrote: And for extra dollops of delicious pro-social goodness, predict the Type of the variable 'hx stored in 'hxtype: Hx would be a value-type, and I'd guess it would have to be something like a dword. ..which .NET does not have
I can predict, without trying, that the example cannot be compiled on my machine, due to the format-string syntax in the second example
--edit
Just gave it a try; var has its uses, among obfuscation. That last part works.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
modified 1-Jul-17 9:58am.
|
|
|
|
|
Eddy Vluggen wrote: you are comparing that to an integer. Nope ... only same types are compared. While both Integer and Color are structs, trying to compare them will make a compile-time error.Eddy Vluggen wrote: Hx would be a value-type, and I'd guess it would have to be something like a dword. Well, yes it's a value-type, but, it's 'uint. For details on the hack you need to use to work around that, please see my response to Harold Aptroot below.Eddy Vluggen wrote: format-string syntax in the second example [^] C# 6 string-interpolation has some very powerful facilities of particular interest if you need to deal with multiple languages/CultureInfo.
cheers, Bill
«Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy
|
|
|
|
|
Cool, been using that "simple" thing for a long time now; learned something new about Color
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|