|
Small tip, multiply by 60000 instead, bit faster.
Since you're turning it into an int anyway, and considering the range of expected values, you could use a float without trouble (several orders of magnitude faster than a decimal )
Something like:
int someFunctionName(string input)
{
return (int)(float.Parse(input) * 60000);
}
warning: untested
In RealLife(tm) you might want to use float.TryParse and do something clever in case it returns false.
|
|
|
|
|
harold aptroot wrote: multiply by 60000 instead, bit faster.
Actually QueryTime * 60 * 1000 is OK, the compiler optimizes that to *60000 while the reader can better see the intent.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Depends on how you write it of course, exactly like "QueryTime * 60 * 1000" it would be ok but most other ways to write it wouldn't be
|
|
|
|
|
Hi Matt,
Everything you tried there, including ((int)QueryTime * 60)* 1000; is wrong; the heart of the problem is (int)x where x is float/double/decimal is rounding towards zero, so if x is between -1 and 1, it will be rounded to zero. You should perform the entire calculation in the highest precision that is present, and only then convert it to an integer.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi I'm Tim and this is my first post. I just started coding in C# a few days ago. I'm developing a piece of software to manage some things at work. If anyone here has a few minutes, can you go over my code so far and let me know if it looks solid and please please let me know if I'm doing anything that throws up "warning flags" as far as coding style etc? Thanks in advance!
2 Forms so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ArrayList vins = new ArrayList();
public Form1()
{
InitializeComponent();
}
public void CreateDataTable()
{
string s1 = "Date";
string s2 = "System.String";
DataTable dt = new DataTable();
dt.TableName = textBox1.Text;
while (s1!= "Stop")
{
DataColumn dc = new DataColumn();
dc.ColumnName = s1;
dc.Caption = dc.ColumnName;
dc.DataType = System.Type.GetType(s2);
dc.DefaultValue = null;
dt.Columns.Add(dc);
switch (s1)
{
case "Date":
s1 = "Member#";
s2 = "System.Int32";
break;
case "Member#":
s1 = "VIN#";
s2 = "System.Int32";
break;
case "VIN#":
s1 = "Location Name";
s2 = "System.String";
break;
case "Location Name":
s1 = "Address";
s2 = "System.String";
break;
case "Address":
s1 = "City";
s2 = "System.String";
break;
case "City":
s1 = "State";
s2 = "System.String";
break;
case "State":
s1 = "Zip";
s2 = "System.Int32";
break;
case "Zip":
s1 = "Phone";
s2 = "System.String";
break;
case "Phone":
s1 = "Fax";
s2 = "System.String";
break;
case "Fax":
s1 = "Email";
s2 = "System.String";
break;
case "Email":
s1 = "Stop";
break;
}
}
DataRow dr;
dr = CreateTableRow(dt);
}
public DataRow CreateTableRow(DataTable dtPass)
{
DataRow myRow;
myRow = dtPass.NewRow();
DateTime today = DateTime.Now;
string date;
int year = today.Year;
int month = today.Month;
int day = today.Day;
date = month + "\\" + day + "\\" + year;
myRow[0] = date;
myRow[1] = int.Parse(textBox1.Text);
myRow[2] = int.Parse(textBox2.Text);
return myRow;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void AddTruck()
{
try
{
if (textBox2.Text != null)
{
long vinNumber = long.Parse(textBox2.Text);
vins.Add(vinNumber);
textBox2.Text = "";
label3.Text = label3.Text + vinNumber + Environment.NewLine;
}
}
catch (FormatException)
{
textBox2.Text = "";
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
AddTruck();
}
private void OpenContactInfo()
{
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text != null)
{
CreateDataTable();
}
}
catch (FormatException)
{
textBox1.Text = "";
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 f;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 fr1)
{
InitializeComponent();
f = new Form1();
f = fr1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
Overall, it looks pretty good, especially for a beginner. I've seen worse code in production.
Couple thoughts:
Don't use ArrayList. That's old .NET 1.0 junk that boxes every value type you put in it (thus making more memory allocations). It makes your code uglier because when pulling values out, you have to cast it to the correct type. It makes your code more prone to error because you can put other objects besides long s in it.
Since you're just putting VIN#s of type long in that list, use a List<long> instead. The compiler will make sure you don't put anything besides long s in it, it performs better than ArrayList, your code will be cleaner, no boxing required.
Another thought related to int.Parse . int.Parse is OK, however, you should be aware of TryParse alternative method. TryParse won't throw an exception, and will return a value indicating whether the parse was successful:
int value;
bool successfullyParsed = int.TryParse("123", out value);
if (successfullyParsed)
{
}
Since exceptions are costly, you may want to consider TryParse instead of Parse if that fits your code OK. If Parse fits better, feel free to continue using it.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Thanks for the quick response and compliment Judah. I had no idea ArrayList was an outdated item. Goes to show that the internet is getting older.
I don't have time to delve into how List<long> works right now, but it sounds like a more solid option that I will look into. I would assume it behaves similar to ArrayList in that if you delete an item (ie myList[3]), that myList[4] would be copied into myList[3] and so on and so forth. Is this correct? If so, the change will be a no brainer.
|
|
|
|
|
stonebergftw wrote: I would assume it behaves similar to ArrayList in that if you delete an item (ie myList[3]), that myList[4] would be copied into myList[3] and so on and so forth. Is this correct?
Yep, that's right, same semantics as ArrayList.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Welcome.
You should use more descriptive names for your variables and classes.
e.g consider renaming s1 to columnName and s2 to columnDataType, etc. Also, change the default names for your text boxes and buttons. You'll quickly forget what textBox1 & button1 are when you app grows. Same for forms, give them more descriptive names than Form1 and Form2 like DataEntryForm or whatever make sense for each one.
Keep it up
Simon
|
|
|
|
|
Simon,
I expected a response such as yours like a firefighter expects there to be heat around the flames.
Now that you mention it, I'm going to change all those things before this program gets any bigger. Thanks.
|
|
|
|
|
At least your expecting it. For someone who's only been coding a few days you already seem to be more aware of good practises than some pros I can think of. Nice one.
Simon
|
|
|
|
|
Thanks guys.
I'm really confused how this language relates to SQL(I need to set up a database). Am I correct in assuming that my code actually does set up an SQL database? If so, I need to write it so that the data is not lost upon program termination. Any ideas of what I should be looking for(search wise)? I'm feeling pretty lost right now.
|
|
|
|
|
Hi Guys!
How can I change the Visual Settings of a form or a Button in the form. Will Microsoft Expression Blend(MEB) help? However, thats a separate software and I've heard that it can only be implemented on Web Applications. What about Microsoft Silverlight (is that too meant for only Web applications)? How can I interactively decorate a form from within VS.NET with the controls available? Please Help......
Your help will be appreciated....
Thanx, Rajdeep.NET!
|
|
|
|
|
Rajdeep.NET wrote: Please Help
Why? Are you helpless? Are you incapable of reading the information provided by Microsoft about Expression Blend and Silverlight to understand what they are?
|
|
|
|
|
Thats not exactly what I asked for?
|
|
|
|
|
Rajdeep.NET wrote: Thats not exactly what I asked for?
Really? Then what does this mean?
Rajdeep.NET wrote: Will Microsoft Expression Blend(MEB) help?
In case you aren't aware how these forums work, we can only know what you post, not what you are thinking. I addressed what you posted.
|
|
|
|
|
hey are you trying to do something with the look & feel of your windows programme.
oh yes one thing the blend and silverlight are tasks of web as they use javascript type codings and xmls .
and one thing you cant apply look & feel for a single object like buttons , labels etc.
its changing in whole.
try google with dotnet skins because i forget the link.
--------------------------------------------
work is yours, world is yours
|
|
|
|
|
|
|
Hi to All,
I have a datagridview with some textbox and combobox columns. This shows some data from postgresql.
It works fine bud when im trying to set the DataPropertyName of the combobox column, im getting the error:
System.ArgumentException: The value of DatagridviewComboboxCell is invalid
Here is a part of my code:
NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
da.Fill(dt);
bs.DataSource = dt;
if(dt.Rows.Count != 0)
{
qDesigner.dgvFilters.AutoGenerateColumns = false;
qDesigner.dgvFilters.DataSource = bs;
qDesigner.dgvFilters.Columns["dgvColTable"].DataPropertyName = dt.Columns[0].ColumnName;
qDesigner.dgvFilters.Columns["dgvColCriteria"].DataPropertyName = dt.Columns["Criteria"].ColumnName;
qDesigner.dgvFilters.Columns["dgvColOr1"].DataPropertyName = dt.Columns["ColOr1"].ColumnName;
qDesigner.dgvFilters.Columns["dgvColOr2"].DataPropertyName = dt.Columns["ColOr2"].ColumnName;
}
Can anyone help me with this issue?
Thanks
modified on Thursday, April 2, 2009 10:53 AM
|
|
|
|
|
Your Code is not completely visible.
|
|
|
|
|
Hi Rajdeep,
Here the complete code:
NpgsqlCommand com = new NpgsqlCommand("SELECT tablee AS Table, columnn AS Column, sort AS Sort, " +
" criteria AS Criteria, or1 AS ColOr1, or2 AS ColOr2 FROM queryfilters "+
" WHERE category='" + category + "' AND queryname='" + descr + "' AND modifier='"+modifier+"'", mycon);
NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
da.Fill(dt);
bs.DataSource = dt;
if(dt.Rows.Count != 0)
{
qDesigner.dgvFilters.AutoGenerateColumns = false;
qDesigner.dgvFilters.DataSource = bs;
qDesigner.dgvFilters.Columns["dgvColTable"].DataPropertyName = dt.Columns[0].ColumnName;
qDesigner.dgvFilters.Columns["dgvColCriteria"].DataPropertyName = dt.Columns["Criteria"].ColumnName;
qDesigner.dgvFilters.Columns["dgvColOr1"].DataPropertyName = dt.Columns["ColOr1"].ColumnName;
qDesigner.dgvFilters.Columns["dgvColOr2"].DataPropertyName = dt.Columns["ColOr2"].ColumnName;
}
|
|
|
|
|
1.What is the equivalent settings for option strict on and option explicit on in c# ?
2.I have a class in another project ( VC++). I want to access the methods and properties in VC++ class from my project in C#. The VC++ is a standalone EXE project and hence I cant add any
reference to my project. But in VB I found a method named GetObject( "AssemblyName") to retrieve the object in runtime. As VB.Net supports early binding by setting option explicit ON , the
methods and properties of the VC++ class can be accessed by creating an instance of system.object. Can anyone please tell me how can we do the same in C#.net ?
3.Is there any C# equivalent function for "ON ERROR RESUME NEXT" in VB ? We can use Try catch but catch block will be excuted if any error occur in try block.
|
|
|
|
|
BINOVAR wrote: 1.What is the equivalent settings for option strict on and option explicit on in c# ?
There isn't one. These constraints are always enforced and cannot be disabled.
BINOVAR wrote: 2.I have a class in another project ( VC++). I want to access the methods and properties in VC++ class from my project in C#. The VC++ is a standalone EXE project and hence I cant add any
reference to my project. But in VB I found a method named GetObject( "AssemblyName") to retrieve the object in runtime. As VB.Net supports early binding by setting option explicit ON , the
methods and properties of the VC++ class can be accessed by creating an instance of system.object. Can anyone please tell me how can we do the same in C#.net ?
Look into p/invoke; I know you can access native dll's that way, I've never tried abusing an exe like that. Worst case would be writing a VB.net wrapper dll and calling that from C#.
BINOVAR wrote: 3.Is there any C# equivalent function for "ON ERROR RESUME NEXT" in VB ? We can use Try catch but catch block will be excuted if any error occur in try block.
No. And you shouldn't be using it in VB either unless you like corrupting your state and data.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
BINOVAR wrote: 3.Is there any C# equivalent function for "ON ERROR RESUME NEXT" in VB ? We can use Try catch but catch block will be excuted if any error occur in try block.
Someone will slap me for saying this, but you can also nest try catch blocks, and there's no requirement to have code inside the catch portion of the try block. At some point you will be causing the hari kari of whatever dev has to look at the code later though.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|