|
i'm getting some trouble dividing 2 double values:
double a = 1.0;
double b = 7.0;
double x = a/b;
x having to be 0,14... instead it is 0.0 WHY?
|
|
|
|
|
Your example doesn't demonstrate the problem.
using System;
class Test
{
static void Main(string[] args)
{
double a = 1.0;
double b = 7.0;
double x = a/b;
Console.Write(x);
Console.Read();
}
}
Outputs 0.14...
Your problem sounds like a and b are integers, in which case integer division will be carried out, resulting in 0 and the 0 being cast to a double.
|
|
|
|
|
i dunno but that is my code, these are double but the result is 0.0
What could be happened?
|
|
|
|
|
You've messed something up in the code. I tried the same thing in C# 2003 and 2005 and couldn't replicate your results. Perhaps copy/pasting the code will help?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I need to get the .exe name within the same application running...
I've tried to use Application.ExecutablePath but the whole Application class doesn't work inside my console application.
So how can i do?
|
|
|
|
|
Hi Sasuko,
You can use Reflection to get the file name of the exe file. You have two options:
System.Reflection.Assembly.GetExecutingAssembly().Location returns the path to the file the actual code is in. So if you put this line in a class library, it will give you the .dll file.
System.Reflection.Assembly.GetEntryAssembly().Location returns the path to the file which was first run to start the application. So whether you put this in your console app, or a class library it references, it will always return the path to the .exe.
Hope this helps
Philip Cole
|
|
|
|
|
Hi,
I'm writing a small program for friends that keeps track of their pet stock, and I cant find a solution to a problem with this code. I am updating an xml file through the DataSet.WriteXml method. Here is some code:
// This is the double click function for the list box control.
private void lvwTarantula_DoubleClick(object sender, System.EventArgs e)
{
try
{
ListViewItem selTarantula = this.lvwTarantula.SelectedItems[0];
int indexOfTarantulaSelected = selTarantula.Index;
DataRow rowTarantula = this.tblTarantulas.Rows[indexOfTarantulaSelected];
NewTarantula dlgTarantula = new NewTarantula();
dlgTarantula.txtScientificName.Text = (string)(rowTarantula["ScientificName"]);
dlgTarantula.txtCommonName.Text = (string)(rowTarantula["CommonName"]);
dlgTarantula.cboGenders.Text = (string)(rowTarantula["Gender"]);
dlgTarantula.dtpDatePurchased.Text = (string)(rowTarantula["DatePurchased"]);
dlgTarantula.txtPetName.Text = (string)(rowTarantula["PetName"]);
dlgTarantula.txtSize.Text = (string)(rowTarantula["Size"]);
dlgTarantula.txtStage.Text = (string)(rowTarantula["Stage"]);
dlgTarantula.dtpLastMolt.Text = (string)(rowTarantula["LastMolt"]);
dlgTarantula.MoltHistoryTbox.Text = (rowTarantula["MoltHistory"].ToString());
dlgTarantula.CommentsTbox.Text = (rowTarantula["Comments"].ToString());
dlgTarantula.PurchaseTbox.Text = (string)(rowTarantula["Purchase"].ToString());
dlgTarantula.PriceTbox.Text = (string)(rowTarantula["Price"].ToString());
dlgTarantula.SerialTbox.Text = (string)(rowTarantula["Serial"].ToString());
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
string strNotify = (rowTarantula["CaptiveBred"].ToString());
bool bNotity = false;
if( strNotify == "True" ){bNotity = true;}
dlgTarantula.chkCB.Checked = bNotity;
dlgTarantula.btnCreate.Text = "Update";
if( dlgTarantula.MoltHistoryTbox.Text == "") // If CommentBox empty, enter first date.
{
dlgTarantula.MoltHistoryTbox.Text = dlgTarantula.dtpLastMolt.Value.ToString("d");
}
strLastMolt = dlgTarantula.dtpLastMolt.Value.ToString("d");
int found = dlgTarantula.MoltHistoryTbox.Find(strLastMolt); // Found equals string index.
if(found == -1 && found != 0) // If not found in CommentBox, add new date.
{
dlgTarantula.MoltHistoryTbox.AppendText(" " + strLastMolt);
}
if( dlgTarantula.ShowDialog() == DialogResult.OK )
{
rowTarantula["ScientificName"] = dlgTarantula.txtScientificName.Text;
rowTarantula["CommonName"] = dlgTarantula.txtCommonName.Text;
rowTarantula["Gender"] = dlgTarantula.cboGenders.Text;
rowTarantula["DatePurchased"] = dlgTarantula.dtpDatePurchased.Text;
rowTarantula["PetName"] = dlgTarantula.txtPetName.Text;
rowTarantula["Size"] = dlgTarantula.txtSize.Text;
rowTarantula["Stage"] = dlgTarantula.txtStage.Text;
rowTarantula["LastMolt"] = dlgTarantula.dtpLastMolt.Value.ToString("d");
rowTarantula["CaptiveBred"] = dlgTarantula.chkCB.Checked.ToString();
rowTarantula["MoltHistory"] = dlgTarantula.MoltHistoryTbox.Text;
rowTarantula["Comments"] = dlgTarantula.CommentsTbox.Text;
rowTarantula["Purchase"] = dlgTarantula.PurchaseTbox.Text;
rowTarantula["Price"] = dlgTarantula.PriceTbox.Text;
rowTarantula["Serial"] = dlgTarantula.SerialTbox.Text;
this.dsAddressBook.WriteXml(this.strFilename);
RefreshTarantula();
}
}
catch(System.ArgumentOutOfRangeException) // Catch exception ...
{
// ... do nothing.
}
// This function adds an Image path to the "Path" element in XML file.
private void mnuAddPic_Click(object sender, System.EventArgs e)
{
ListViewItem selTarantula = this.lvwTarantula.SelectedItems[0];
int indexOfTarantulaSelected = selTarantula.Index;
//DataRow rowTarantula = this.tblTarantulas.Rows[indexOfTarantulaSelected];
string strPath;
if( indexOfTarantulaSelected >= 0 )
{
DataRow rowTarantula = this.tblTarantulas.Rows[indexOfTarantulaSelected];
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter =
"Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
openFileDialog1.Title = "Open an Image file";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
strPath = openFileDialog1.FileName;
rowTarantula["Path"] = strPath;
this.dsAddressBook.WriteXml(this.strFilename);
RefreshTarantula();
}
}
}
The "Path" node in the XML file does not appear, and I cant figure out why.. Any help would be greatly appreciated.
|
|
|
|
|
i have been using C# for quite some time now and i was wondering if J# offered the same funnctionality. Which is better C# or J#? does it make any difference wether i use VB, C#, or J# since all the code eventually gets compiled into MSIL>
|
|
|
|
|
i didn't use J# before but J# was mad to make it easy for java developer to move to .Net and minimize learning curve so if you are not java developer i think c# is good choice as you already started with it
as you said all .Net Language genertate il but they are not have the same features there some deference between them
MCAD
-- modified at 14:04 Wednesday 7th September, 2005
|
|
|
|
|
Do you see a J# forum here ? That answers the question
J# is only semi supported, as far as I can see. C# is where the action is.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
C# has many more features than J#. Enums, property syntax, shorthand event declarations (compared to J#), structs, etc.
Having to continually convert between J# 'primitives' and .NET types is really a pain (i.e., 'double' is not identical to System.Double in J#). You must pass .NET types to .NET methods, but they return J# 'primitive' types in J# - you'll never get used to this quirk...
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
-- modifed at 21:09 Wednesday 7th September, 2005
|
|
|
|
|
I want to retrive the selected row into a datagrid, and afther andding more rows to the control (through the datasource), to select the previous retrived row.
I retrive the row with the BindingContext property... but how can I select it in the datagrid with new datasource?
I hope I understand...because is a rough world out there...
|
|
|
|
|
how can i get the serial number of any kinde of hardware(i.e. HDD,CPU,RAM,graphic card,..etc)
for example in delphi, i use the GetVolumeInformation() to get the serial number of HDD.
so is there such function or class can do this..
|
|
|
|
|
|
I have a multithreaded app which creates several objects. Inside the object class is something like:
public ArrayList Associations
{
get
{
if(this.assocs == null)
{
lock(this)
{
if(this.assocs == null)
{
this.assocs = new ArrayList(500);
return this.assocs;
}
}
}
else return this.assocs;
}
}
Is this kind of lazy initialization safe to do in a multithreaded environment?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Cops & Robbers
Judah Himango
|
|
|
|
|
I'd use single locking:
<pre>
public ArrayList Associations
{
get
{
lock(this)
{
if(this.assocs == null)
{
this.assocs = new ArrayList(500);
return this.assocs;
}
return this.assocs;
}
}
}
</pre>
unless performance testing shows you need to do double locking, then you need to add a memory barrier,
<pre>
get
{
// use lazy initialization to create the associations:
if(this.assocs == null)
{
lock(this)
{
if(this.assocs == null)
{
ArrayList result = new ArrayList(500);
System.Threading.Thread.MemoryBarrier();
this.assocs = result;
return this.assocs;
}
}
}
return this.assocs;
}
</pre>
You can also make assocs volatile instead and use the double locking.
<a href = "http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx" rel="nofollow">http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx</a>[<a href = "http://blogs.msdn.com/brada/archive/2004/05/12/130935.aspx" target = "_blank" rel="nofollow">^</a>]
<hr><div style="font-size:xx-small;font-family:arial;color:grey;"><font size=1> I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon</font></div>
|
|
|
|
|
I was concentrating on your lazy initalization question so much, I missed the bigger issue. You can't just expose a dynamic array list to multiple threads.
Use
this.assocs = ArrayList.Synchronized(new ArrayList(500));
instead.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
Actually, your other post answered the question; I used ArrayList as an example only, my actual code uses a custom List<MyObject>.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Cops & Robbers
Judah Himango
|
|
|
|
|
Hi, i made a windows service, i added the service installer, the service process installer, etc..Then, i did the installer and installed the windows service. It appears in my add/remove programs window, but it DOESNT appear in my Services window, so i cant start it or stop it, or whatever..Does anybody know why? what am i missing? thanks
|
|
|
|
|
Did you make sure to put the [RunInstaller(true)] attribute above your service installer class? Also, make sure you close and re-open the services window, since it will cache and not notice things sometimes.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Does someone have experience reading WM_INPUT info? I already know I need to use user32.dll, I only have no idea HOW. There are masses of tutorials in c or c++ on this subject, but just a few in C#. I'm a programmer newbe, so I like code with some explanation
Thanks in advance.
|
|
|
|
|
Can somebody tell me the best way to implement the logic similar to SQL Composite Key Constraint.
|
|
|
|
|
Where do you want to implement this?
---
b { font-weight: normal; }
|
|
|
|
|
I have an array of objects of a class having 10 fields. The first 5 fields should behave like composite keys of a sql table. So only objects conforming to the constraint can be added.
I know that this can be achieved using DataSets and DataRelations. I dont want to use it.
I just need a simple logic to implement this in an ArrayList.
|
|
|
|
|
Slow method:
Loop through all the records and compare the values in the fields to the new values you want to add.
Fast method:
Create a hash value for the fields and store in the object. Create a hash value for the new values and loop through the records and look for the hash value.
Even faster method:
Use a hash table instead of an array, create a composite value of the fields and use as key in the hash table. Create a composite value of the new values and check if it exists in the hash table.
---
b { font-weight: normal; }
|
|
|
|