|
Thank u everybody
If you can think then I Can.
|
|
|
|
|
Hi I am trying to figure out why my method keeps only showing me the first character from a sent string. I would like it to show all the characters that were sent.
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0;
iRx = socketData.currentSoc.EndReceive(asyn);
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
MessageBox.Show(szData);<<<<<<<<<<<<What ever message is sent is supposed to show up in this message box...
WaitForData(socketData.currentSoc);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
|
|
|
|
|
Instead of
System.String szData = new System.String(chars)
you could try
StringBuilder str = new StringBuilder();
for (int x = 0; x < charLen; x++)
{
str.Append(chars[x]);
}
I haven't had a problem with that.
Brent
|
|
|
|
|
Hate to break it you, but this had no effect.
|
|
|
|
|
What happens when you put a break point at socketData.currentSoc.EndReceive and then step over the code? What do you see when you watch the variables?
[Edit]I see you don't like debugging - too much like hard work is it?[/Edit]
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Saturday, October 23, 2010 6:47 AM
|
|
|
|
|
Several things could be wrong here, however I can't tell which one(s) it will be as you have shown only a fraction of the relevant code. Here are the main possibilities:
1.
you may be invoking a receive with a small buffer; if you were to ask for one byte, then you would only get one byte.
2.
you might have an encoding mismatch between sender and receiver. e.g. if the sender uses Unicode, regular characters would look like real (ANSI) bytes and NULLs intertwined; an 8-bit encoder would react badly on the NULLs.
3.
what EndReceive returns is the number of bytes, not characters, hence char[] chars = new char[iRx]; is wrong. IMO you should use [EDIT] Encoding.GetString() right away to convert bytes into a string.[/EDIT]
|
|
|
|
|
So Im reading a csv file and splitting the string with "," as the deliminator
but some of them have quotes as to not split the specific field because it has a comma in it.
1530,Pasadena CA,"2008, 05/01","2005, 12/14"
with just comma it would be:
1530
Pasadena CA
"2008
05/01"
"2005
12/14"
I need it to take commas into consideration when splitting so its like this
1530
Pasadena CA
"2008 05/01"
"2005 12/14"
|
|
|
|
|
You may wish to do a search and replace of all commas within the valid bounds of the quotation marks first(with a ~ perhaps ), and then use the resulting string to do the split.
After you have done that you could just do a search and replace on all the split elements to replace the tildes with a comma again.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Please do not use this solution - the FileHelper one is much better and easier to use / understand.
You can't do that easily with string.Split - it only works on a single character. You will either have to do it manually or use a regex:
string inputText = @"1530,Pasadena CA,""2008, 05/01"",""2005, 12/14""";
Regex regex = new Regex("(?=,)|[^\",]+|\"(?:[^\"]|\"\")*\"",
RegexOptions.Compiled);
MatchCollection ms = regex.Matches(inputText);
foreach (Match m in ms)
{
if (m.Length > 0)
{
Console.WriteLine(m.Value);
}
}
The regex looks ugly, but it was thrown together a bit quickly - that's why it generates blank matches.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
you may want and read the Rive[^] article PIEBALD created.
|
|
|
|
|
10!
|
|
|
|
|
PIEBALDconsult wrote: 10!
That is 5 each.
|
|
|
|
|
Observe the coding principle of DRY (don't repeat yourself), in fact don't repeat what others have already done either. If at all possible, try using the FileHelpers library. It does nearly everything you could possibly need on CSV processing. I recently used this and found it great.
|
|
|
|
|
|
Personally, I prefer a separation of responsibilities -- one method to read the data, another to split it, another to parse it, etc.
Some times I allow CSV or other text file lines to be commented-out (e.g. first character a semi-colon) -- I don't want to split or parse them.
I haven't really looked at FileHelpers, but does it support newlines within values?
|
|
|
|
|
I haven't worked with FileHelpers for a few months, and have lost touch a bit, but AFAIR it caters for both your requirements. As for SoR, I also think it allows you to handle your own reading and pass it data only for splitting.
|
|
|
|
|
Please ignore my earlier solution - I have been playing with the one Brady supplied, and I am impressed how easily and how well it works:
Add a reference to FileHelper.
Add a class:
using System;
using FileHelpers;
namespace GUITester
{
[DelimitedRecord(",")]
public class Customer
{
#region Constants
#endregion
#region Fields
#region Internal
#endregion
#region Property bases
#endregion
#region FileHelper interaction
public int Id;
public string Location;
[FieldQuoted(), FieldConverter(ConverterKind.Date, "yyyy, MM/dd")]
public DateTime AccessDate;
[FieldQuoted(), FieldConverter(ConverterKind.Date, "yyyy, MM/dd")]
public DateTime CreateDate;
#endregion
#endregion
#region Properties
#endregion
#region Regular Expressions
#endregion
#region Enums
#endregion
#region Constructors
#endregion
#region Events
#region Event Constructors
#endregion
#region Event Handlers
#endregion
#endregion
#region Public Methods
#endregion
#region Overrides
#endregion
#region Private Methods
#endregion
}
}
Add code to your form:
using FileHelpers;
...
FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
Customer[] customers = engine.ReadFile(@"F:\Temp\Records.txt") as Customer[];
foreach (Customer c in customers)
{
Console.WriteLine(c.Id + ": ");
Console.WriteLine(" >" + c.Location);
Console.WriteLine(" >" + c.AccessDate);
Console.WriteLine(" >" + c.CreateDate);
} And by George it works!
I am very impressed indeed. If you use it, vote the article a five, it deserves it...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I take it you're a regions fan.
|
|
|
|
|
Yes, to an extent. It's my standard boilerplate class file template - it just means I can collapse everything I'm not interested in at the moment without having too many tabs open at the top of the IDE.
Can't stick it when you get regions inside a method though...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I have a problem with pop windows. When I Open Pop up page, do actions on that and go back to parent page. In Parent page if user click on logout or if session expired,how to close pop up windows.
|
|
|
|
|
The answer depends on the whether it is Windows or Web? Since you have posted to the general C# forum and have not specified which you are using it makes it difficult to give an accurate answer.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I have a ListBox with SelectionMode.MultiExtended .
I add an item programatically and set it as selected. I want it to be the sole selection.
I have tried Clear() on all the Selectedxxx collections, set all the individual Selectedxxx properties to values which should deselect any current selections. I have tried iterating over the SelectedIndices and using SetSelected(x, false) all to no avail, whatever was selected before the addition remains selected highlighted afterwards.
I have resolved the problem by setting the SelectionMode to SelectionMode.One , SelectedIndex to -1, making the addition reverting the SelectionMode and selecting the added item.
Code:
this.lboxEntries.SelectionMode = SelectionMode.One;
this.lboxEntries.SelectedIndex = -1;
this.data.Add(this.txtEditEntry.Text);
RefreshDataBoundControls();
this.lboxEntries.SelectedIndex = data.Count - 1;
this.lboxEntries.SelectionMode = SelectionMode.MultiExtended;
This seems unnecessarily convoluted.
Any one have a more direct method?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
modified on Friday, October 22, 2010 1:08 PM
|
|
|
|
|
Nah. There is that little thingy called documentation[^]; one should regularly scan the list of class members, there's always something useful to be learned...
Henry Minute wrote: a more direct method?
You did ask for a method!
|
|
|
|
|
There's method in your madness, Luc!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Yeah, my methodology exceeds my madness by far. I'll try and keep it that way.
|
|
|
|