|
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)
{
}
|
|
|
|
|
If I understand correctly what you are asking, you can do something like:
private void AddTabPageIfNotPresent(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
right. And better yet the method should return the TabPage, either the matching one or the new one.
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.
|
|
|
|
|
Yep, after reading Tamir's answer to Arindam's post it's clear that he needs to either create a new tab (and return it) or return the existing one:
private TabPage GetOrAddTabPage(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return item;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
return tab;
}
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
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 ment when a user reveic a private message a new tab opens,
unless there is already one open.
i can't do it...
|
|
|
|
|
Hi, well the answer is in my previous post: have a look at the GetOrAddTabPage function, it will give you an idea about how you can do that.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Moreno Airoldi wrote: have a look at the GetOrAddTabPage function, it will give you an idea about how you can do that.
I have found the Add function (tab.Controls.Add),
but i can't find the Get function.
only:
tab.Controls.GetChildIndex
tab.Controls.GetEnumerator
tab.Controls.GetHashCode
tab.Controls.GetType
what did you exactly ment?
|
|
|
|
|
The function I showed takes care of creating and/or getting the tab you need:
private TabPage GetOrAddTabPage(string TabName, TabControl Control)
{
foreach (TabPage item in tabControl1.TabPages)
{
if (item.Text == TabName) return item;
}
TabPage tab = new TabPage();
RichTextBox document = new RichTextBox();
document.Dock = DockStyle.Fill;
document.ReadOnly = true;
tab.Controls.Add(document);
tab.Text = TabName;
Control.TabPages.Add(tab);
return tab;
}
You can use it in your code this way:
TabPage UserPage = GetOrAddTabPage(UserName, MyTabControl);
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Hi
i want to export my datagridView to excel. i found some example to accomplish this like this[^] and this[^]
but at runTime i got this error :
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
for solving this problem i found this link[^] but my problem does not solved. can anybody give me a better way to convert my datagridview to excel without any error ?
thanks
|
|
|
|
|
Try this[^]
PLease inform if any errors
|
|
|
|
|
Thanks Tamer
i try your component, but in this line :
extendedDataTableObject.ToExcel(@"c:\test.xlsx");
i got this exception :
Exception Occured While Generating Excel Document
my another goal is to save my dataTable in excel 2003.
|
|
|
|
|
Hello there,
I wrote a tool for importing and exporting data into ExcelSheets (better said: workbooks).
Some relevant parts of my code are:
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
excelApp.ScreenUpdating = false;
excelApp.DisplayAlerts = false;
then I open some workbooks, open some sheets, write some stuff into them and close all of them. I also dereference them if they are no more needed (mysheet = null).
But after finally shutting down the ExcelApp doing so:
excelApp.Quit();
excelApp = null;
... I have to wait quite a long time (about half a minute) until I see the EXCEL-thread diappear in my ProcessExplorer.
The problem is, that I can not open the just written xls-files if the application did not end, because it still has got its hands on the files.
So I have to know when this happens.
My questions are:
Why does it take so long?
When does the Excel-thread really end?
How do I end it correctly?
How can I provoke the thread to be ended?
Or is it just a matter of garbage collection? (a field I did not yet explore)
Thanks in advance,
Harry
no plan,
no signature
|
|
|
|
|
Put simply, there is no way to completely shut down Excel (close the task so that you don't see it in the task list anymore) when you want to. It's a COM<->gargabe collector issue.
In earlier versions of the framework, it could be done with a direct call. GC.Collect() after quitting and nulling, but starting from 2.0 or 3.0 (can't remember cleary) it's no use.
The bright side is if you open more than one Excel app, only one will stay in memory for a long time, while all others will close immediately.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|