|
What's missing in this scenario is information about where the strings you add newlines to are coming from.
If you are reading them in from a file, and they are delimited by newlines, that's one case: in that case you can add them to your StringBuilder as you read the file, a line at a time, taking care of delimiting with newlines right there. If it's a file, but not delimited with newlines, and you've got to parse the whole file to create the whatever semantic units that are to become "lines," that's another story. And, finally, if these strings are being created on-the-fly by you ... being calculated/composed in some way: another scenario.
If you can focus on getting the strings into a List<string>, you can just call 'Sort on that List, and then you can directly fill a TextBox by setting the 'Lines property of that TextBox (an array of string) to, courtesy of Linq, the List<string> converted to an array: as in:
List<string> sl = new List<string> { "wo", "ab", "c"};
sl.Sort();
textBox1.Lines = sl.ToArray(); And all of the above, as is said in almost every answer above, should not obscure that the problem here is probably not in the speed of StringBuilder itself.
"Anyone who shows me my 'blind spots' gives me the gift of sight." ... a thought from the shallows of the deeply shallow mind of ... Bill
|
|
|
|
|
Hi All !
There are some Items in my List Box(ListBox1) include "Positive" or "Negative" items ,so when I want to add Positive , "Fore color" for this Item in list Box should be Green , and if I want to add Negative item to List Box , "Fore color" for this Item in list Box should be Red :
if (ListBox1.Items.ToString() == "Positive")
ListBox1.ForeColor = Color.Green;
else
ListBox1.ForeColor = Color.Red;
thanks for any Help !
|
|
|
|
|
That's wrong cause you set the color for the entire listbox.
The best/easiest way is to set the DrawMode to OwnerDarwVariable and handle the ListBox.DrawItem event.
There you could use any brush you like to draw the text.
Just look for listboxDrawItem on bingle and you'll find lots of examples.
All the best,
Dan
|
|
|
|
|
Have you considered the ListView as an alternative to the ListBox. When set to the 'Details' view it supports per item colouring via the ListViewItem.ForeColor and .BackColor properties.
Alan.
|
|
|
|
|
All the best,
Dan
|
|
|
|
|
Wonde Tadesse
MCTS
|
|
|
|
|
Applying different ForeColors to the items of a ListBox is pretty easy; here[^] is an article that discusses an entirely different topic, however the download will show you how to get what you want.
|
|
|
|
|
You need to either custom draw the items or have a custom control (e.g. here is one by me[^]).
|
|
|
|
|
hello guys.. I have a function in some class (actually its DAL). Now return type of this function can be either DataTable, DataSet or DataRow like this
public DataTable function(int myTableKey)
{
DataTable dt;
return dt;
}
Now if everything goes right, this function returns me the data table.
But what if some goes wrong, like if I get some exception or I wanna return some string message in case of failure. What should I do? thnx
|
|
|
|
|
Exceptions are thrown, not returned.
In the case of a user-defined failure you can choose to either throw an exception with a meaningful error message, if it's an exceptional condition that should be handled as an error, or return null from the function (obviously this should be defined in the contract for the function and calling methods should handle it appropriately).
|
|
|
|
|
But how? Here is the code to make it more clear
public DataTable function(int myTableKey)
{
string msg;
try
{
DataTable dt;
return dt;
}
catch(Exception ex)
{
msg = ex.Message;
return msg;
}
}
Now focus on the return statement in catch block. This is what I was talking about.
|
|
|
|
|
Just like said before/above either throw the exception and handle it on the calling client,
or log it, return null and check for null on client.
catch(Exception ex)
{
throw;
dt = null;
}
return dt;
I recommend throwing it and handle the exception on the client calling method.
All the best,
Dan
modified 2-Dec-11 17:16pm.
|
|
|
|
|
Unless this is a top tier interface function where exceptions are bad (and since it's doing database stuff, it shouldn't be), you should just let the exception escape, to be handled by the calling function.
|
|
|
|
|
I'd say do nothing, unless you want to do some custom exception handling in your function. Exceptions are for exceptional circumstances and in my mind there's always been little point in catching an exception just to rethrow it. The error message in an exception is often useless without the stack trace as well.
It's up to the *calling function* to decide what to do in the event of an error, so it may handle the exception there or let it pass further up the call stack.
In particular stuff like this
try
{
}
catch (Exception ex)
{
throw ex;
}
is pointless and bad as you'll lose the stack trace.
Regards,
Rob Philpott.
|
|
|
|
|
If you just use
try { ... }
catch(Exception) { throw; }
(note, 'throw' not 'throw ex'), you don't lose the stack trace. It's still pointless but at least it's not harmful.
|
|
|
|
|
Indeededoody!
Regards,
Rob Philpott.
|
|
|
|
|
I agree with allowing the Exception to be thrown. You may also add some context to the Exception if you choose:
catch (Exception ex)
{
ex.Data.Add("Method", "function");
ex.Data.Add("myTableKey", myTableKey);
throw;
}
Personally, I like to determine what type of Exception it is (timeout, deadlock, duplicate key, etc.) and wrap it in a custom Exception.
|
|
|
|
|
Thanks for a good tip, +5
Bastard Programmer from Hell
|
|
|
|
|
I hate to admit that I still don't fully understand what happens when it comes to .NET versioning.
If I want to use an Infragistics control that was compiled against the 2.0 framework, in a 4.0 project, does this cause a mix of 2.0 and 4.0 objects in the same runtime environment?
For instance, if the 2.0 control creates a new Rectangle object, is that a version 2.0 Rectangle, while my 4.0 code would be creating version 4.0 Rectangles?
Or does all the code in the process run against version 4.0 objects?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
2.0 code would know nothing about the 4.0 code so it could only create its own version. Only one version of an assembly can be loaded into the appdomian at a time so objects created will be from that version. However, it does get a bit tricky if you use codeBase[^] or assembly rediection[^]
No comment
|
|
|
|
|
Thanks, that helps. I take note that only one version of an assembly can be loaded at a time, so that means that the 2.0 assembly will be using objects from the 4.0 version of the .NET framework.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Have a look at the modules window in the debugger. That'll tell you all the assembles that are loaded and their version.
Regards,
Rob Philpott.
|
|
|
|
|
K, I am relatively new to sockets so bear with me.
I have an experimental Client/Server socket application I am developing to replace an existing
legacy DCOM Client/Server architecture.
What i am trying to do is replicate the class interface of the legacy DCOM client classes in the TCP Socket client classes.
For instance
DCOM Client Call Signature:
Call on the client class to retrieve an array of information from the DCOM server.
int returnValue = client.GetArray(int arrayID, ref object values)
This client also receives updates from the server at regular intervals.
So my problem is with the new TCP Socket based class.
I have a thread within the client class that is listening for updates from the server.
Now I send a byte[] based call to the TCP server which returns a byte[] containing 2 integers and an array in this order: ResultOfCall(4 bytes) ArrayBytesLength(4 bytes) ArrayBytes(? bytes)
byte[] callSignature = GetSomeCallSignature(parameters);<br />
mySocket.Send(callSignature);<br />
<br />
byte[] returnVal = new byte[4]<br />
mySocket.Receive(returnVal,4,0);<br />
int retVal = BitConverter.ToInt32(returnVal);<br />
<br />
returnVal = new byte[4]<br />
mySocket.Receive(returnVal,4,0);<br />
int arrayLength = BitConverter.ToInt32(returnVal);<br />
<br />
byte[] arrayBytes = new byte[arrayLength];<br />
mySocket.Receive(arrayBytes,arrayLength,0);<br />
<br />
object arrayValue = this.ConvertToArray(arrayBytes);
My problem is that the thread that is constantly checking for updates will read everything past the first receive(receive the function return value) and the program will lock up on the second receive
(i.e. receive the array bytes length). My question, is it only feasible to have two socket connections to the server class for each client? One for executing commands against the server and one for listening for events from the server. There really is no way to tell how many clients will be connected to the server, doesn't this open up a can of worms?
"All of us who served in one war or another know very well that all wars are the glory and the agony of the young."
Gerald Ford
|
|
|
|
|
Hi, as i always say; it's software, anything is possible. First of all, depending on the Listen of the server you can connect as much clients as you need.
Second; would you really want to have it that way? If the other channel is only for a event so once and a while you could also use UDP for that, not that it really makes a difference but if you could broadcast the events the client would not need to bother about connecting to the server.
Next issue is naturally what you're sending /receiving. Make your life easier and build yourself a proper protocol where each packet at least contains the protocol version (certainly for UDP) or when connecting first exchange protocol versions.
Next about receiving data, there are several ways to do this and so far it seems that you have used the blocking way; you can also handle the receive in a async way, that frees the system to do other interesting stuff.
I think i gave you a lot to chew on but these are things you will need to consider sooner or later.
Let me know if you like more detailed info and i'll see what i can cook up for you.
Regards, AT
Cogito ergo sum
|
|
|
|
|
Sautin.net wrote: There really is no way to tell how many clients will be connected to the server, doesn't this open up a can of worms?
The Server has a thread call it A. Thread A accepts connections (creates a connection) specifically for that client.
The server then spins up a thread to deal with the connection, only that connection. So a new thread exists for each connection.
Thread A on the server then goes back to waiting for a connection.
So when client X does a connection the following happens.
1. Server opens accepting port.
2. Thread A gets a connection call on port and that creates connection X.
3. Server creates thread 1 and passes X to it.
4. Thread A goes back to listening for connections.
5. Thread 1 listens for input (your callSignature) using X.
6. Thread 1 gets msg and then sends response (bytes) on X.
7. Thread 1 can stop or go back to step 5.
|
|
|
|