|
OK, I start to get the picture.
Your basic information consists of some 10000 points (that could be 80KB of data).
So it seems very wasteful to expand that to a full distance matrix (some 50MB).
Let us assume that distance simply equals SQRT(dx*dx+dy*dy).
I did a simple experiment: the following code
double z=0.0;
for (int dx=0; dx<10000; dx++) {
for (int dy=0; dy<10000; dy++) {
z+=Math.Sqrt(dx*dx+dy*dy);
}
}
Console.WriteLine(z);
took less than 4 seconds on a 1.7GHz Pentium M.
Using a file on disk would certainly be faster (A large part of your file would
typically be in the file cache in memory, which gets refilled at the disk's
transfer rate, typically 100 MB/sec or more). So I expect consuming 50MB would take
under 1 second (you could try that with another 50 lines of code...)
On the other hand, the (lack of) speed of the above code is mainly due to the
Math.Sqrt() method; if we replace that line by z+=dx*dx+dy*dy; it runs in less
than 0.7 seconds.
Preliminary conclusions:
- if you are lucky and only need the square of the distances, go for direct calculation;
- if you are unfortunate and need a more complex calculation for distances (the earth
being round, remember), go for the file on disk.
- it may be worthwhile to investigate faster SQRT methods (maybe int based, maybe
slightly less accurate, whatever).
My suggestion:
I would certainly start of with direct calculation and see how it is behaving
before introducing a file-based optimization (the rule is: think first, then code
and get it working without unnecessary complications, then observe and
only then make changes, and introduce complexity, to improve).
Luc Pattyn
|
|
|
|
|
This helps a lot, thanks!
I hadn't considered that the SQRT function was so time consuming, but given the iterations that I have, that is a big factor. I'll try what you suggest.
Cheers.
|
|
|
|
|
Hi,
I'd like to write a basic parser in XML to write the values of various fields across several forms to a text file. The idea is that custom settings of all these fields can be stored as a "user config" that can be recalled.
Could you help point me towards information online that will help me get started? I would like to start with a test app that writes several values to a text file and then reads then back into the form.
Thanks!
|
|
|
|
|
See this[^] set of tutorials.
/ravi
|
|
|
|
|
Thank you! Do you know of a similar set of tutorials for C#?
|
|
|
|
|
The tutorial supports both VB and C#.
/ravi
|
|
|
|
|
Hi,
I have an instance of data table named Bank_Details with the
columns "Bank_Code, Name, Acc_No",
sitting inside a dataset MyDataSet.
I have a BindingSource associated to the MyDataSet/Bank_Details.
Without creating another instance of the table, I want to bind
it to a datagridview, and only have Bank_Code column
show up in the DataGridView.
One way I know I can do it is to do a Visible=false on DataBindingComplete event for columns
that I don't want displayed.
But I am trying to avoid all that unnecessary adding of columns
and hiding them when my requirement is to not deal with those columns
at all.
Just wondering if there is a way to bind a DataGridView to an
already available instance of a DataTable and limit the databinding
to only specific columns?
I appreciate you all sharing any insights regarding this.
|
|
|
|
|
I've encountered this as well. If there is a solution that someone else has found that works better, by all means use it, but here's the workaround that I've used:
What I have done to get around this is to manually enter all the columns into the DataGridView in the forms designer, and set the "DataPropertyName" property on each column to the appropriate DataTable column that will be in the dataset. I can then set the visible on these to false, and bind to them normally, and have only the desired columns show up. What this also allows me to do is use the hidden values in rows for calculations and whatnot, but also set up formatting on the individual cells at design time instead of during runtime.
It works for my means (I use DataGrids to display a lot of line-driven real estate calculations and different views on the same value ($/SF/Mo vs $/SF/Yr in unbound columns based on read-only data in visible columns and other data in !visible columns).
Hope this helps
##EDIT
Edited to be a little more specific
|
|
|
|
|
Are you actually adding the columns one by one? Or are you just doing:
myDataGridView.DataSource = myDataTable
I haven't looked at it yet, but couldn't you actually just do:
myDataGridView.DataSource = myDataTable.Columns[x], to only show that one column?
|
|
|
|
|
I have a string that whenever it finds \r\n in the string i need it to be split, i tried using Text.Split("\r\n") but it doesnt like that. This is the only thing that is causing me errors with my printing, can anyone help me fix this? or does anyone know how i can count the number of times that \r\n appears in a string?
|
|
|
|
|
Right after i posted this i had an epiphany, i just replaced \r\n in the string with ` and split using that becasue i i was trying to find an odd character that no one uses very often and that solved my problem
|
|
|
|
|
You don't have to reserve a special character, just use text.Replace("\r", "").Split("\n")
|
|
|
|
|
Hello,
Instead, you should use:
Text.Split(System.Environment.NewLine);
All the best,
Martin
|
|
|
|
|
I do not agree.
First of all, System.Environment.NewLine results in a string, but String.Split requires
a char or char array.
Second, the logic you follow is correct only if the system that generates the file,
and the system that processes the file, both have the same opinion on how to
terminate lines.
e.g. if you generate the file on Linux, and want to process it on WinXP, it would
not work.
Luc Pattyn
|
|
|
|
|
I do agree with you in every point.
Luc Pattyn wrote: First of all, System.Environment.NewLine results in a string, but String.Split requires
a char or char array.
I shout test before I post!
Luc Pattyn wrote: Second, the logic you follow is correct only if the system that generates the file,
and the system that processes the file, both have the same opinion on how to
terminate lines.
It's not important in the application I'm working on, but it's good to inform about that it's possibility!
Thanks
All the best,
Martin
|
|
|
|
|
Is there a way of doing this? so i could have different registry on my memory stick.
|
|
|
|
|
A different registry on your memory stick? What are you trying to do here? circumvent the existing one?
|
|
|
|
|
I mean have a program, which loads up and then it takes over the regdistry tempoarly, then it sets it back to how it was.
(Have a app like mojopac)
|
|
|
|
|
I am creating custom control.I am hiding all base class properties and displaying only custom properties.But one property ->"(Name)" under design category is getting displayed .
Design category has 2 properties :(Name) & Locked
I am able to hide "Locked" property but not "(Name)" property.By hiding Name property it doesnt work.
CAn anyone suggest the solution...
|
|
|
|
|
How are you attempting to hide Name? By deriving from the type and hiding it from there? If you use the new keyword and create a Name property with that keyword on your derived type, then make that property non-public, it should be hidden.
|
|
|
|
|
snoby wrote: CAn anyone suggest the solution...
Yes, you can't, (Name) refers to the name which the variable is declared in code with. E.g. if you have a textbox called txtUsername then if you look in the code generated by the designer you get:
private System.Windows.Forms.TextBox txtUsername;
So you can't get rid of the (Name) property because if defines how the code is generated.
I have no idea what I just said. But my intentions were sincere.
|
|
|
|
|
I've made custom shell for purposes of one application. However I have problems with slow printing. I've found solution on:
link to post
but still can't make C# equivalent of code on that location. Can someone help me on making this hidden "PrintTray_Notify_WndClass" window?
Thanks in advance.
|
|
|
|
|
I have created the following, based on PInvoke and unmanaged code in Windows dlls.
It always gives the error "ALREADY_EXISTS", probably since I am run Windows Explorer...
private void mniCodeProject_Click(object sender, System.EventArgs e) {
int handle=CreateWindowEx(
0,
"PrintTray_Notify_WndClass",
"",
0x80000000,
0,0,10,10,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero);
log("handle="+handle.ToString("X8"));
if (handle==0) {
int i=GetLastError();
string s="";
if (i==183) s=" ERROR_ALREADY_EXISTS";
log("last error="+i.ToString("X8")+s);
}
}
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern int GetLastError();
[DllImport("user32.dll", EntryPoint="CreateWindowEx", CallingConvention=CallingConvention.StdCall)]
public static extern int CreateWindowEx(uint style2, string className,
string windowName, uint style, int x, int y, int w, int h, IntPtr parent,
IntPtr menu, IntPtr instance, IntPtr lpParam);
You will have to add some #using statements, and to replace my log function by yours
(could be Console.WriteLine).
I am not sure how long the Window will live, and/or whether you should delete it
when it is no longer needed...
Good luck with it.
Luc Pattyn
|
|
|
|
|
First of all thanks for your code.
Now, I'm having trouble using it, as I always get error code: 1400 ("Invalid window handle") exception. I tried on three computers and it's happens on all of them. Any idea how I can solve this problem?
Thanks in advance!
|
|
|
|
|
I need more info to form an opinion on this.
Is 1400 hex or decimal ?
is it exactly my code that reports "handle=00000000" and "last error=00001400" ??
Please provide exact code and output.
What is your OS (winXP ? any service pack ?) and .NET version (1.1, 2.0 ? any
service pack?)
Furthermore, I have some ideas.
Idea 1:
I am not sure, maybe it is necessary to prefix my code by the following:
SetLastError(0);
together with the declaration:
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern int SetLastError(int err);
Rationale: a lot of system calls change the "last error" value only when something
goes wrong, but don't clear it beforehand. This is what SetLastError(0) is about.
(So it may have been set by CreateWindow or any call executed earlier).
Idea 2:
there might be a problem with the className/windowName arguments (ASCII/Unicode
confusion). If so, this could be fixed by specifying CharSet=CharSet.ASCII or Unicode
inside the DllImport line of CreateWindowEx declaration, and/or by changing
the EntryPoint to either CreateWindowExA or CreateWindowExW.
Luc Pattyn
|
|
|
|
|