|
Aljaz111 wrote: Always removes 0 from first place..
Is the decimal separator in your country by any chance a comma? How about this for verification;
string s = "0.55555";
double d = Convert.ToDouble(s, System.Globalization.CultureInfo.InvariantCulture);
string s2 = d.ToString("#,#0.0");
Console.WriteLine(s2);
I are Troll
|
|
|
|
|
|
Good thinking
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thanks
I are Troll
|
|
|
|
|
Ok it doesn't work without System.Globalization.CultureInfo.InvariantCulture but with it in Convert.Double(s, System.Globalization.CultureInfo.InvariantCulture) works just fine. Can you explain me please what does this have to do with rounding numbers?
Thx
|
|
|
|
|
It interprets the decimal point wrong, if you replace the point with a comma then all goes well. There's also a System.Globalization.CultureInfo.CurrentCulture .
The invariant culture will use the decimal-point, no matter what's been set currently by the user. If you convert the double "2.0" from invariant to your CurrentCulture[^], then you'd probably get "2,0"
I are Troll
|
|
|
|
|
I'm not quite sure how you're approching this but it's quite simple
public static double StringToRoundedDouble(string s, int decimals)
{
double result;
if (double.TryParse(s, out result))
result = Math.Round(result, decimals, MidpointRounding.AwayFromZero);
return result;
} Useage
double d =(StringToRoundedDouble("0.5555", 1);
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
hi im coding c# sockets in Asynchronous mode.
my app is running but i need to limit the number clients to one. so if a connection already exists i want the server to refuse any new connection. but using async methods i cant stop it from listening.
the server will have to be restarted everytime the unique connection is terminated, so i dont need it to serve next new connection.
how could i refuse new connections when async does not support it?
thank you very much.
|
|
|
|
|
Whether you're using synchronous or asynchronous methods you have to explicitly tell the TcpListener or Socket to accept a new connection using one of the BeginAccept or Accept methods. If you don't want more than one client connected then don't call those methods more than once.
|
|
|
|
|
perfect! i understand it now. thanks.
|
|
|
|
|
are you sure you still want asynchronous operation if all that is going on is a single connection?
seems to me you're paying the bills but not getting the goods.
|
|
|
|
|
|
I will answer them only if you can answer yes to any of the following:
Have you gone to class once this semester?
Are you looking forward to bagging my groceries next year?
Do you know how annoying it is to have kids come here before mid-terms and ask basic questions they should have known by class #3?
Do you understand that this series of mock questions is your last call to go see your teacher and get a tutor before you flunk out of your program?
Seriously, this is your last chance.
|
|
|
|
|
I did think of giving him single word, truthfull but unhelpfull answers, but decided he would probably talk to me if I did. Perhaps Google has stopped working for him?
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
This could be a take home interview and he is a senior .NET consultant with 20 years experience charging $7 an hour.
|
|
|
|
|
Yes and no. You decide which questions these apply to.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I have a groupBox that has N PictureBox controls on it.
Each PictureBox is named thmb1-N e.g thmb26.
I have a list of paths, of thumbnail images, which I want to iterate through and set PictureBox control ImageLocation properties for each PictureBox .
The code I have written below is a first rather messy and quick attempt - which is cumbersome and I really don't want to be iterating through every object.
Is there a better way of doing this?
If I could do something like thmb + N.ImageLocation = ... but i realise I cannot do this in C# so any help in directing me towards how to do this would be much appreciated.
Guy
for (int i = 0; i < bubble.thumbs.Count; i++)
{
foreach (Control ctrl in this.groupBox8.Controls)
{
if (ctrl is PictureBox)
{
if (LeftRightMid.Left(ctrl.Name.ToString(), 4) == "thmb"
&& Convert.ToInt32(LeftRightMid.Right(ctrl.Name.ToString(), 1)) == i)
{
((PictureBox)(ctrl)).ImageLocation = bubble.thumbs[i];
}
}
}
}
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
You could do this:
PictureBox pic;
for (int i = 0; i < bubble.thumbs.Count; i++)
{
string ctrlName = "thmb" + i;
pic = this.groupBox8.Controls[ctrlName] as PictureBox;
if (pic != null)
{
pic.ImageLocation = bubble.thumbs[i];
}
}
But I would probably re-think my whole approach! If you have to use .Controls, then you are hard-wiring your code to a specific implementation - not at all good for re-usabillity!
All those who believe in psycho kinesis, raise my hand.
|
|
|
|
|
Thanks - your code is much more elegant than mine.
I agree with you about re-thinking my approach.
I use SAS in my work, C# being a hobby, and in SAS you can do something like:
n=1<br />
thmb + n .imagelocation = "C\image.jpeg" <br />
Which is the dynamic equivalent of thmb1.imagelocation = "C\image.jpeg"
I have googled and looked on code project but I think it is a case of not knowing which question to ask - as I have not found anything similar to my example above for C#.
Thanks for your help - I will use what you suggest until I find what I am looking for.
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
How about a List<string, System.Windows.Forms.Control> ? You could iterate through the components when the form is done loading, adding the PictureBoxes to a List. Then you could use a for/each construct to loop through the pictureboxes
I are Troll
|
|
|
|
|
Thanks
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
Hi,
You have two nested loops, which is one too many for the job at hand.
You either drop the loop over controls (that is what Griff suggested), or even simpler, you drop the one looping over the bubbles, like so:
foreach (Control ctrl in this.groupBox8.Controls) {
pb = ctrl as PictureBox;
if (pb != null && LeftRightMid.Left(ctrl.Name, 4) == "thmb") {
int i = Convert.ToInt32(LeftRightMid.Right(ctrl.Name, 1));
pb.ImageLocation = bubble.thumbs[i];
}
}
Note:
- I used "as" instead of "is", removing the need for later casts
- I dropped some ToString() calls, as the object already was a string
- I use "i" as an index, no need to repeat that in a loop
- this code should be faster than Griff's one, as his Controls[name] lookup takes much more effort than a simple array index operation.
BTW: I doubt ANY of the versions shown will work correctly for N>=10 as you only are using one character to extract the thmb number from the control's name. The fix for that would be to take the substring that starts at the position (hence counting from the left side, not the right side); string.SubString() is what you would use then.
|
|
|
|
|
Thank you for all of your help.
Yes I was thinking the ToString was a bit ott on my part in some places.
Good point on the N>=10 issue - you have written the code for me without me even having to say 'plz help meez'
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
You're welcome.
|
|
|
|
|
hello
im making my final year project i.e. speech recognition.but i dont have any idea how to start?what will be the first step?i intend to work on c#.plz can anyone guide me.
thnx
|
|
|
|