|
Thanks mate but maybe I am a little stupid but how? Sorry bout this.
|
|
|
|
|
ok i know this must be a silly question, but im new to c# and still learning.....
I am trying to display a float value to the console, but it only appears as a whole number. (I am trying to display the average of an array of numbers)
This is what i have;
float result = Total/NumberArray.Length; //to store avg value in flaot var
.
.
Console.WriteLine("Average " + result); //to show value on screen
Where Total is The sum of the numbers
NumberArray.Lenghth is lenghth of array
For example (20+20+10)/3 = 16.66666
What i see on the screen is 16
Help would be appreciated???
Cheers
|
|
|
|
|
Try this:
float result = (Total*1.0)/(NumberArray.Length*1.0);
.
.
Console.WriteLine("Average " + result);
If you divide 2 integers, even if you store the result in a float, the result is also integer, so try a small "cast"
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
a real cast is more efficient than your kludge. In any event you don't need to cast both numbers, just the one.
--
If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
|
|
|
|
|
Thanks Andrei
How silly of me, i should of thought of type 'casting';)
this also works
float result = ((float)Total/NumberArray.Length);
Cheers
|
|
|
|
|
I know that casting also works, it was just another way of doing things
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
I assume total is also an integer. IF so what's happening is that you're doing integer division, getting an integer result and then converting the int into a float.
What you need to do is cast one of the ints into a float before doing the division.
float result = (float)Total/NumberArray.Length;
Depending on order of operations you might need a second parenthesis to get the desired result.
float result = ((float)Total)/NumberArray.Length;
--
If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
|
|
|
|
|
Yes thanks Dan
Cheers
|
|
|
|
|
Also, you may want to limit the number of decimal places shown, you can do that like this:
float fTest = 123.456789f;
string sTest = fTest.ToString("N3");
that would put it at 3 decimal places, N5 would show 5 decimal places and so on. Just thought you might like to know. Incase you didn't already.
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
Ooh - you're hitting an issue that regularly causes confusion and gets a lot of people hot under the collar about .NET. Basically, the issue is that the Total value is an integer. You need to cast the result to a float so this would become float result = (float)Total/NumberArray.Length;
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello
I am kind of new to C#(2.0) and MS SQL 2005, I am battling to write code (windows app C#), that can check Sql table to see if a record already exist. I have a table in sql called [Resources] with a PK called(Personnelno), i am trying to write a stored proc that can check if a [personnelno] exist in the table. if it exist it shouldn't save the record, else save it.
The application is three tiered [DAL, Business object and Presentation], can anyone please show me how do i call this stored proc through this layers until to the presentation layer. i'm trying to show messagebox to let user know that the record already exist, so he or she can change it.
Thanks.
N. Gazide
|
|
|
|
|
Here is a small example to get you started. You can find more in System.Data.SqlClient namespace.
SqlConnection connection = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand(connection);
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ProcedureName";
SqlDataReader reader = command.ExecuteReader();
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
Additional to what Andrei submitted, you could check the return value from your stored procedure to see if the record exists. For example in your SP you could use:
IF EXISTS(Select Pinfo from
Where Pinfo = @info)Begin
RETURN 2
END
Else
BEGIN
<insert stuff="" here.......="">
END
If the return value is 2 then you know the record exists. Check out SQLParameters and ParameterDirection.ReturnValue for help.
HTH
F
|
|
|
|
|
Hi there, I am currently in negotiations with a customer that wishes to purchase an application that I have developed for him in C#. My dilemma is that although I trust him I wish to be able to give him a working copy of the application knowing that he will not just be able to 'steal' it. Seeing as the application needs to be online to function in any case I thought that maybe I could incorporate a request into the application to verify whether or not it should run so that I can disable it if something smells funny rendering the application useless.
I have attempted to use a HttpWebRequest request to try and retrieve the string of a file on my website and authenticate it that way but it seems that it caches somewhere or something but it takes a very long time for it to take effect. Here is my current Verification method:
private bool allowedToRun()<br />
{<br />
StringBuilder sb = new StringBuilder();<br />
<br />
byte[] buf = new byte[8192];<br />
<br />
HttpWebRequest request = (HttpWebRequest)<br />
WebRequest.Create("urlOfMyWebPage.html");<br />
<br />
HttpWebResponse response = (HttpWebResponse)<br />
request.GetResponse();<br />
<br />
Stream resStream = response.GetResponseStream();<br />
<br />
string tempString = null;<br />
int count = 0;<br />
<br />
do<br />
{<br />
count = resStream.Read(buf, 0, buf.Length);<br />
<br />
if (count != 0)<br />
{<br />
tempString = Encoding.ASCII.GetString(buf, 0, count);<br />
<br />
sb.Append(tempString);<br />
}<br />
}<br />
while (count > 0);
<br />
MessageBox.Show("Allowed to run: " + sb.ToString());<br />
return Convert.ToBoolean(sb.ToString());<br />
<br />
}
Ideas?
|
|
|
|
|
Have you thinked to use a webservice?
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
I'm not familiar with the webservice. Could you point me to a tutorial or sample application?
|
|
|
|
|
Try Google first and also try this article. And search codeproject for articles and google also when you receive an idea because you'll find everything you'll need.
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
I have consulted google and I've reviewed the projects on the site but unfortunately I'm not sure how to integrate those tutorials into what I'm trying to do here. I basically need to call a function and return it's value to my program. And I also need to be able to modify it's value.
|
|
|
|
|
Nokoff wrote: I basically need to call a function and return it's value to my program. And I also need to be able to modify it's value.
You can write a webservice with 2 methods. A GetValue method and a SetValue method. You will be able to call these functions in your app to read a value and set a value.
It's just an idea.
Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
Hello All,
I have a datagrid view on a windows form that I have working pretty well. I did not have the "MultiSelect" set to true so naturally the user was only able to make one row selection at a time and this worked fine.
Now the users have come up with a solution to trim their work time by being able to select multiple rows and have the processing done on the rows selected.
Here is my problem. I know how to retrieve the value in the first cell of the selected row but how do I go about selecting the values from the first cell in the subsequent rows that have been selected. I thought of using a 'for' statement but I am not sure how to move to the next row to retrieve the value of the next cell.
Thank you in advance for any help provided.
|
|
|
|
|
In general, things with a SelectedItems property can just be used like an array. So to get the first selected row, you just use
SelctedItems[0]
and for the second
SelectedItems[1]
etc. Im not sure if its called SelectedItems in a datagrid but it should work the same. I could be mistaken. If you've not got an answer by the time i get home, i'll check it out.
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
The SelectedRows property gives you an DataGridViewSelectedRowCollection on which you can iterate.
Hope it helps
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
Andrei,
I am sorry to be so bold as to state that I am not sure how I would implement the collection to iterate through. I am quite new to C# and coming from VB, collections are a different breed (for me at least.) I haven't gotten that far yet. Could you find yourself to provide a very small example of how to implement this particular collection. I did look to see if the SelectedRows Property included the Collection but couldn't find it. I did see something about GetEnumerator but I'm lost.
Thanks though.
F
|
|
|
|
|
I will give you an example, supposing you have a DataGridView called dgv
DataGridViewSelectedRowCollection collection = dgv.SelectedRows;
foreach (DataGridViewRow row in collection)
{
}
row is a DataGridViewRow with which you have work before...right? Of course you will have to place this code somewhere in an event, or function...you'll figure that part out.
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
Andrei,
That was the ticket. After your response I researched it and now I have a good understanding of how this works. Again, thank you both for your responses.
Best regard,
F
|
|
|
|