|
Thanks, but unfortunately it was not helpful.
It doesn't matter if I call ActualizeView() out of TabControl_IndexChanged() or out of MyList_Load().
I see the debugger running into that code. I also see it run over the lines, that look like
this.myList.dgView.Rows[index].DefaultCellStyle.ForeColor = Color.Red;
But after the method terminates the contents are there, but every line is drawn black.
btw: This problem disappears when I draw myList again after the tabControl changed for the first time. But I want to fix it without having to draw again.
Greets
Harry
no plan,
no signature
|
|
|
|
|
|
Hi,
I am having 50 MB(Approx. after counting from editplus editor) of data's in a stringbuilder and when i am converting in to Stream it was throwing error as "System.OutOfMemoryException was thrown".
The code below:
StringBuilder strCmdBuild; // Holds the 50 MB values;
// Converting Stringbuilder to Stream.
Stream stCommand = new MemoryStream (ASCIIEncoding.Default.GetBytes (strCmdBuild.ToString()));
// Here I am using the StreamReader to read..
StreamReader objReader = new StreamReader (stCommand);
And loop through till all values getting read..
string str = objReader.ReadLine ();
Mainly the error("System.OutOfMemoryException") throws before the completion of reading...
How to rectify this error?
With reg,
Subbu
|
|
|
|
|
Can you post the full code?
|
|
|
|
|
Thanks a lot...I gave sample values of strTemp and it has so many SET_VALUE 01 and other values and Adding the values in to CmdBuilder StringBuilder and return it to called function.
strTemp (it holds 50 MB of datas) = \r\n<START_COMMAND>\r\nSET_VALUE 10\t >W\r\nSET_VALUE 11\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\n.............
..................SET_VALUE 11\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\nSET_VALUE 10\t >W\r\nRESP_BLOCK\r\nDo_DR_END \r\n\r\n\r\n<END_COMMAND>
------------------------------------------------------------------
// Code Starts here just copy and paste it in notepad and view it...
static public StringBuilder GetCommand(StringBuilder strTemp) // string strTemp
{
StringBuilder CmdBuilder = new StringBuilder ();
StringBuilder CmdLine = new StringBuilder ();
try
{
int TDILength = 0;
if (strTemp.ToString().Trim() != "")
{
Stream stCommand = new MemoryStream (ASCIIEncoding.Default.GetBytes (strTemp.ToString()));
StreamReader objReader = new StreamReader (stCommand);
//strTemp.ToString() = ""; //strTemp
//RESP_BLOCK
while(true)
{
string str = objReader.ReadLine ();
if (str == null)
break;
if (str.IndexOf (">W") > 0 )
{
if (str != "")
{
str = str.Replace(" >W","");
//CmdBuilder.Append(str.Trim () + "\r\n");
CmdLine.Append(str.Trim () + "\r\n");
}
TDILength++;
}
else if (str.Trim () == "RESP_BLOCK")
{
if (CheckForMaximumSize (TDILength)) // Check Whether we have reached max. size or not
{
TDILength = 0;
CmdBuilder.Append("<END_COMMAND>\r\n"); // If we reached say END_VALUE -> to issue BULK TRANSFER
CmdBuilder.Append("RESET\r\n");
CmdBuilder.Append("<START_COMMAND>\r\n"); // Start-up new Block
}
CmdBuilder.Append(CmdLine.ToString ());
CmdLine = new StringBuilder ();
}
else if (str.Trim () == "RESP_BLOCK_START")
{
while(true)
{
str = objReader.ReadLine ();
if (str.IndexOf (">W") > 0 )
{
if (str != "")
{
str = str.Replace(" >W","");
CmdLine.Append(str.Trim () + "\r\n");
}
TDILength++;
}
else if ( str =="RESP_BLOCK_END")
{
if (CheckForMaximumSize (TDILength)) // Check Whether we have reached max. size or not
{
TDILength = 0;
CmdBuilder.Append("<END_COMMAND>\r\n"); // If we reached say END -> to issue TRANSFER
CmdBuilder.Append("FIFO_RESET\r\n");
CmdBuilder.Append("<START_COMMAND>\r\n"); // Start-up new Block
}
CmdBuilder.Append(CmdLine.ToString ());
CmdLine = new StringBuilder ();
break;
}
}
}
else if (str.IndexOf ("WAIT") == 0)
CmdBuilder.Append(str.Trim () + "\r\n");
if (str.Trim () == "<START_COMMAND>")
CmdBuilder.Append(str + "\r\n");
if (str.Trim () == "<END_COMMAND>")
CmdBuilder.Append(str + "\r\n");
if (str.Trim () == "START ")
{
while(true)
{
str = objReader.ReadLine ();
if (str.Trim () == "END " || str == null)
break;
else if (str.Trim () != "")
CmdBuilder.Append(str + "\r\n");
}
}
}
objReader.Close ();
stCommand.Close ();
}
else
throw new Exception ("There is no Commands to Transfer....");
}
catch(Exception objException)
{
//throw objException;
MessageBox.Show(objException.Message);
}
if (CmdBuilder != null)
return CmdBuilder.Replace(">W","");
else
return null;
}
Thanks,
Subbu
|
|
|
|
|
Delete all of your code and don't use a memory stream or any streams and break your logic out into different methods. You may be surprised at how quickly trying a different approach from scratch works. That is what the experienced dev's do.
|
|
|
|
|
Did you know you can use strings in a switch ?
|
|
|
|
|
Poor form, I always use String.Compare instead of a switch.
I also believe strings are over and misused in modern programming.
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote:
Poor form
Opinion
I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)
|
|
|
|
|
I believe the complexity of both is O(1) and you may be confusing a run time estimate with complexity.
Yes it is my opinion, but then again I like not having to know about the culture when doing string comparisons, http://msdn.microsoft.com/en-us/library/hyxc48dt.aspx[^]
But you are right, it is just my opinion and both will run fine, and in .NET, the switch will be faster.
|
|
|
|
|
Actually they are both O(k) (*), strange eh? They thought it would be a good idea to make a dictionary that maps all the strings in the cases to integers, and then switch on that. And that dictionary, is a local variable! Argh! So it's remade every time, so that'll be k Dictionary.Add's every time.. great..
The if/else if chain is trivially O(k), there are k cases (definition of k) and if no match is found at all then all cases were tried so k string comparisons (which aren't O(1) by themselves, unless both strings are interned).
Both ways make no sense.
1) There is a total order over strings, so a tree-based search could be used.
2) That "temporary" dictionary should not be temporary, so the cost of adding the entries can be amortized.
Anyway, it's true that it's O(1) if you look at the resulting exe/dll, but you can change the number of cases in the source so it's really a "variable" after all
|
|
|
|
|
Removing the loop removes the n, which does not seem logical except that Big Oh is not a measure of complexity and not running time.
for(int i=0;i<n;i++){
SomeAction(i);
}
is O(n)
but
SomeAction(0);
SomeAction(1);
...
SomeAction(n-1);
is O(1)
|
|
|
|
|
I really have to disagree. You have something of non-constant length n, so you can't simplify the n away.
It's really just a special case of unrolling
And this n doesn't suddenly become constant just because you said "ok so let's not change it anymore" - that's like saying sorting an array is constant time for any given array
|
|
|
|
|
In analyzing the complexity of algorithms you need to examine the code as written, not the compiler output. While I try to be forgiving in the matter of opinion this is a matter of fact. I am not going to get pedantic on the matter and I am not going to try and prove why this is the case. You don't have to believe me but I would suggest doing a lot more research in the matter before deciding to disagree further.
|
|
|
|
|
Come one, be flexible, the number of cases is the real variable here, if you start treating it as a constant (and YES I am very well aware that it actually is a constant) it becomes impossible to say anything interesting about it anymore.
So I say O(k), and k is a constant as usual (it's a k, not an n or m or whatever), but if you do that last simplification to O(1) you just lose the necessary detail without gaining anything.
|
|
|
|
|
And not if you write it like this. You'd have to fix the n. The "..." is not of fixed length now.
edit: yea ok sorry that was just useless. There is a difference between what is right and what is useful. Actually that applies to this whole argument so if you don't mind I'll just quit. Last modified: 31mins after originally posted --
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: I also believe strings are over and misused in modern programming.
Agreed
harold aptroot wrote: Opinion Smile
I don't like huge if/else if/else if/else if/else if/else if blocks (and then there is the O(n) time complexity of it..)
Agreed
[This is a pet peeve of mine as well]
Simple solution to work a string value into a switch statement without working with the strings directly -
Create an enumeration that KEYS your valid values (therefore, your Enum keys would have to be an exact representation of the string you expect) and simply
switch(stringValue)
{
case enumerator.Value1.ToString():
break;
case enumerator.Value2.ToString();
break;
}
This is not even close to the most robust solution along these lines, but it I highly prefer it to switch cases containing arbitrary string values or endless
if/else if/else if/else statements
I actually have a more robust solution which involves creating(among other things):
public static T Parse<T>(string value)
{
if (value.Equals(String.Empty)){
Parse<T>("NonDeterminant");
}
return (T)Enum.Parse(typeof(T), value);
}
within my translation layer that will allow you to specify which type of Enumerator you are attempting to cast the string to and return out the type-safe Enumerated value of the string - essentially maps your string to a list of acceptable values and disregards that it ever existed as a string
"I need build Skynet. Plz send code"
|
|
|
|
|
Why on Earth would even want to have 50MB in a StringBuilder? I would suggest you revist your design.
only two letters away from being an asset
|
|
|
|
|
I have to agree with Mark.
It looks like you've already loaded data into memory (in the guise of a StringBuilder), presumably from an IO stream. So I don'understand why you then perform a .ToString() then stream into memory.
If you are using a stream to populate the string builder it would be MUCH more efficent to process each line as you read it from source.
Additionally this stuff is asking for trouble:
while (true)
{
string str = objReader.ReadLine();
}
You have too many nested levels of code going on, making the code hard to read & understand and hiding any potential errors in the logic. I suggest you have a look into refactoring the code(Martin Fowler has written a book on this subject). Hopefully others will suggest more resources on this topic.
|
|
|
|
|
Why do you want to load 50MB of string into StringBuilder ?
At Tuesday, you want to load 2MB string into StringBuilder, it was not a good thing. And now you want to load 50MB string into StringBuilder ?
I think you must redesign your application.
|
|
|
|
|
As the others said.
And don't you ever post chunks of code without using PRE tags.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
I've a tab control that has for ex. 2 members at the TabPages.
The first calles "one and the other "second".
i want to check if at my TabPages i have already a tab with some name
and if not' i want to add new one with that name.
this is my foreach loop:
foreach (TabControl item in tabControl1.TabPages)
and this my my code to add new tab:
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = "one";
tabControl1.TabPages.Add(tab);
Someone have an idea how to do this?
|
|
|
|
|
Did not get any idea, what you want actually..
|
|
|
|
|
like in a chat,
if a user get a private massage it opens a new tab,
but if the tab is already open the message written in the open (exist) one...
|
|
|
|
|
Probably you have to change your foreach data type. Verify the following code and let me know if you need any further details.
bool exist = false;
foreach (TabPage page in this.tabControl1.TabPages)
{
if (page.Text == "one")
{
exist = true;
break;
}
}
if (!exist)
{
}
|
|
|
|