|
Okay, I pondered and pondered and realised that you could refactor to this:
void RemoveDuplicatesButton_Click(object sender, RoutedEventArgs e) => this.InputTextBox.Text = string.Join(Environment.NewLine, new SortedSet<string>(longString.Split('\r', '\n'))); It's as easy as that.
This space for rent
|
|
|
|
|
Does the fact that you pondered so long not suggest you might be over-engineering it? There will probably be an equal amount of pondering to working out how it works.
Each to their own, but I much prefer a few simple lines to one clever one.
I suspect you've lost the preservation of order of the original implementation, and why are people using a SortedSet rather than a HashSet?
Regards,
Rob Philpott.
|
|
|
|
|
You do realise that this was in response to the original poster don't you? I'm not a big fan of "clever code" so I wouldn't tend to write my code like this, this was just a way to show how this could have been done with the basics that were already present without relying on the extra scaffolding the OP put in. As for why a SortedSet, that's what the OP used so I have followed that; presumably he needs the output to be sorted, hence the SortedSet.
This space for rent
|
|
|
|
|
I must be stupid, since I wind up having to "decompose" this stuff when the "intermediate" results and "lazy execution" starts yielding other than the results I expect (even if it was due to my own "mind fog").
Then what? Put it "back together" again for the next oaf?
Perhaps "any idiot" can figured this one out, but at what point is it "too much"? And who says so?
This is the opposite extreme of posters who have been chasticed for failing to cater to the lowest common denominator (when "they" used LINQ instead of something less "obtuse").
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I prefer to write code that is maintainable rather than elegant.
|
|
|
|
|
That's multiple statements in a single line.
Are you paying for each line-feed character you use?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
imho, you are doing a lot of extra work:
textBox1.Lines = textBox1.Lines.Distinct().OrderBy(str => str).ToArray(); However, if you anticipate that in the future your code will be read by people who are Linq-illiterate, then: whatever.
«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
|
|
|
|
|
Hi Bill, but ah.. You're ordering the strings alphabetically. Original implementation preserved the order just removing duplicates.
Nice implementation though (TextBox.Lines not requiring line delimiting). If you drop the OrderBy(), I think there are no guarantees about preserving order.
I have an inkling there's a select method which has a second 'index' parameter which you could order by.
Regards,
Rob Philpott.
|
|
|
|
|
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) { }
}
|
|
|
|