|
that's exactly why i give my loop counters meaningful names whenever i have to nest loops - those mistakes can be a pain to find
for finding the min and max what you have now seems ok. in your min function the if statement contains a & , recognize the difference between & (bitwise and) and && (logical and)
|
|
|
|
|
|
The error around line 50 is because your outer loop is going until i = 10,000; whereas the array is only indexed to 1,000
elements. So, when i = 1,000 you'll get the error as you've gone beyond the bounds of the first dimension of the array.
Just my personal opinion, but I always look for places to make small performance and memory savings. Since your values
are 3-digit numbers you are wasting a lot of memory using int as it is a 32-bit value covering numbers that are
way larger than you'll need. You can use ushort (UInt16 ) that will cover the range of numbers you're
working with and use half the memory.
Also, I believe that the maximum value parameter to Random.Next(int) is exclusive for the
upper bound, meaning the maximum value will never be the number you supply; if you want 999 to be a possible value, you
should supply 1,000 as the maximum and then 999 will be the largest value that it will return.
The performance is bad, currently, because you are looping through all elements of the array with each random number you
produce. If you changed your logic to index into the array on the random number instead of using a loop to get to the
element it would greatly improve the performance. In other words, replace this:
currentNum = randomNumbers.Next(999);
for (int j = 0; j < 1000; ++j)
{
if (lotteryNumbers[j, 0] == currentNum)
{
lotteryNumbers[i, 1]++;
}
}
with this:
currentNum = randomNumbers.Next(1000);
lotteryNumbers[currentNum]++; <--- assuming the array's redefined to just one dimension as suggested below
I noticed that the array is 2 dimensional; I didn't really see any need for having 2 dimensions; I'd just create it
as ushort[] lotteryNumbers = new ushort[1000]; . The index position will simulate the values 0 - 999 and
the value of each element would be its number of occurrences. Also, with this change there is no need to initialize any
values in the array as all the elements will default to 0 when it is created.
Last, you'll need a way to sort the results to get the 10 with the least and 10 with the most occurrences. I'd recommand
sorting the results into a single collection and then pulling the first 10 and last 10 elements; I'll leave that as an
exercise to the reader .
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Thanks for the tips. I'm relatively new to C# so your tips are definitely helpful.
|
|
|
|
|
Hope that someone could help me on this...
By this simple line I can define filderID with value selected in Combobox
int folderID = (int)folderComboBox.SelectedValue;
,but how can I define that value when have instead a comoBox a datagridview on my form and i want some selected row to become a folderID ?
plz help
|
|
|
|
|
By handling a similar event, for the selected row changing in your grid.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
how to get a value from a datagridview cell?
led mike
|
|
|
|
|
yess I have a datagridview that conzist of two columns FOlderID, and Folder name, I want to get Folderid for selected row in datagridview and store it in new variable like on a example with combobox, but here is datagrid and i have problem to get that value
|
|
|
|
|
Ain't me babe wrote: but here is datagrid
Ain't me babe wrote: I have a datagridview
First, DataGridView is not the same thing as DataGrid. So make sure you know exactly which you are using.
Second, there exists perfectly fine documentation for both, freely available, on the MSDN site. See this for DataGridView.[^] You should have no difficulties finding your solution by reading that documentation. Good luck.
led mike
|
|
|
|
|
Hi all,
i have arequirement to import excel sheet to postgresql using C#.
i wrote a code:
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + txtfilename.Text + ";" + "Extended Properties=Excel 8.0;");
mycommand = new OleDbDataAdapter(@"select * from [Ranjit$]", connection);
mydataset = new DataSet();
mycommand.Fill(mydataset, "ExcelInfo");
try
{
dataGridView1.DataSource = mydataset.Tables["ExcelInfo"].DefaultView;
}
catch (Exception ex)
{
//System.Diagnostics.Debug.WriteLine(ex.GetBaseException().ToString());
//MessageBox.Show(ex.GetBaseException().ToString());
throw new Exception(ex.Message);
}
So mydataset is displaying data in datagridview. this is working fine
No i have to send data to postgresql database
For that i wrote code
NpgsqlConnection strconn = new NpgsqlConnection(@"server=localhost;user id=postgres;password=thinksoft10@;database=Test;SyncNotification=true ");
strconn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("COPY \"Test\" FROM STDIN", strconn);
NpgsqlCopyIn cin = new NpgsqlCopyIn(cmd, strconn);
cin.start();
but this is copying files to the database.
So please tell me how to copy that information in the postgresql database.
in my application i am able to show the excel sheet data in datagrid.
but i am not aable to send the excel sheet data to postgresql Test database test table.
Any help is appreciated!!!
thanks in anticipation
Ranjit.balu
|
|
|
|
|
If you already have the data correctly in a datagrid, can't you simply iterate the data in the datagrid and using INSERT INTO statement put the data into the database.
Note: If that's a real username and password in your connection string, I believe you don't want to share them with us.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Hello,
Where are the button images (move first, move last etc)
associated with the bindingNavigator buttons ?
I've tried a search, but cannot find them.
thanks
pcjd
|
|
|
|
|
They are set in designer generated file. The images are stored in resx file.
|
|
|
|
|
Thank you for your reply Giorgi,
but let me explain a little further.
I have one project where the bindingnavigator button
images are present, and yes they reside in the resx file.
However, I have a second project which has a bindingnavigator
and the button images are not present. So how can I load them ?
With the project that has the images, the image property of
each button has System.Drawing.Bitmap as the value.
How (or where) can I load these images into my new project ?
(without cutting and pasting from the original resx file)
thanks
pcjd
|
|
|
|
|
Hi,
I have a AxWebBrowser hosted inside a form. How can I notify the form whenever the user changes the selections in the comboBoxes hosted inside the browser?
thanx
|
|
|
|
|
Does anybody know how I can give my application a custom color that is not available in visual studio (Sort of like how you do it in html).
Prateek
|
|
|
|
|
If you know RGB code of the color you can use Color.FromARGB() method to get the color you want. If you want to allow user choose color they need, you can use ColorDialog.
|
|
|
|
|
It took me a while to get the rgb value, but that worked.
Thanks again
Prateek
|
|
|
|
|
Glad to help you
|
|
|
|
|
I'm trying to generate an Excel 2003 compatible XML file using the XMLSerializer. I've seen a couple articles on this but I've not seen one yet that shows how to create the <"mso-application progid="Excel.Sheet"?> line just after the line. The XMLSerializer will create the version line and pretty much everything else I need but I can't seem to find anything in it that will let me generate the line that tells the OS to open the file with Excel.
I would really like to use the XMLSerializer as my only other choice seems to be just building the XML totally from scratch.
Any help is greatly appreciated.
Thanks,
TxPomeroy
|
|
|
|
|
Just modify the output manually after using the XMLSerializer.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
I have a series of combo boxes (17 in all) each with a list of names for selection. Each name has its own colour. The names reflect who is/could be on duty during a particular time period.
So when I select Barbara from the list in combo 1 I want that combo's text background colour to be set to light blue. If Gretchen is selected in combo 2 I want to use light green in combo 2. I have a click event which manages this part. using a case statement to detemine name/colour.
When I load the combo box at program start up, everything is fine,each person has their own colour.
If I now change the name in combo 2 from gretchen (green) to Sabrina (light salmon) The text changes but the colour does not. If I click on another combo box or again in the same combo box then the colour for Sabrina changes.
Am I missing something.
|
|
|
|
|
Here is the code in the Event Handler
private void cboCMs_EventHandler(object sender, EventArgs e)
{
ComboBox box = sender as ComboBox;
box.SuspendLayout();
switch (box.Text)
{
case "Barbara":
box.BackColor = System.Drawing.Color.LightBlue;
break;
case "Gretchen":
box.BackColor = System.Drawing.Color.LightGreen;
break;
case "Sabrina":
box.BackColor = System.Drawing.Color.LightYellow;
break;
case "Joanne":
box.BackColor = System.Drawing.Color.LightPink;
break;
case "Not Played":
box.BackColor = System.Drawing.Color.LightSalmon;
break;
case "No CM":
box.BackColor = System.Drawing.Color.Magenta;
break;
case "No BR":
box.BackColor = System.Drawing.Color.MediumOrchid;
break;
default:
break;
}
box.Update();
box.ResumeLayout();
}
|
|
|
|
|
I know what I'm doing is a big no-no on the forums, but I feel my original thread got pushed back a couple of pages to due recent spam (html template crap). I did get one answer, but I feel a little short sighted to be able to make proper use of it, so this thread can actually be seen as a clarification of it...
Problem:
my GUI thread calls a worker thread which has an infinite loop monitoring for changes / commands which may trigger another GUI Form. Obviously I want to keep all my GUI stuff on the same thread, so how would be the proper / best way to get that form activated on the original GUI thread?
Suggested answer: look into .Invoke which left me shortsighted, as stated above looking for any clarification.
|
|
|
|
|