|
Good Day,
Say I have an array of numbers. For instance:
int[] myNumber = {1,11,21,42,32};
and I would like to randomly remove 2 items from that array. It is easy to implement, but mine was sort-of inefficient (I'm working with large arrays). So I'm asking for some advice.
Thanks.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
|
|
|
|
|
Ian Uy wrote: like to randomly remove 2 items from that array. It is easy to implement, but mine was sort-of inefficient (I'm working with large arrays).
Let's see the code, how can where is it being inefficient?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Well, what I do is first I get the size of the array, say 10, then randomly generate a number from 1 to 10, then check if the number generated has already been removed, if not, generate again, if yes, remove it. So it's like a guessing game.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.
|
|
|
|
|
If you can't change it to a List as Christian suggests, then instead of generating a new value, simply increment (or decrement) until you find a non-removed value (be sure to wrap if you hit the end of the array).
|
|
|
|
|
There's no efficient way to remove from the middle of an array, use a list instead.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I have been doing C# (WinForms) development for the past 5 years. I have just started an ASP project. From my experience I have always programmed to type safe objects that implemented the needed interfaces to allow generics. The current projects is using DataTables as the primary object for storing all information and does not use type safe objects that implement generic interfaces. Today, I had a discussion with the programmer who wrote the system that uses only tables and was unable to give him a list of the true benefits that type safe objects using generics provide. Can anyone list what advantage using the type safe objects have over using table objects handling everything.
Thanks,
Steve
|
|
|
|
|
Well - the first one is typesafety. You know - letting the compiler catch errors rather than having to rely on runtime. Secondly, DataTables - yukk..... if you change a fieldname in your database then you are going to have one hell of a job catching all of the instances of the DataTable throughout the application. Also - it's not interoperable with none .NET systems without a great deal of work, and it's a hell of a big structure (serialization is a bitch with this).
There - is that enough to get on with?
|
|
|
|
|
hy everyone!
i was wondering if it is possible to combine number formating and dateformating in one function.
formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}.
i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this.
public static string ConvertFormats (string input, string formatstring)
{
if (formatstring.Contains(":")
{
}
else
{
}
return input;
}
So what if this function should format stringlength as well? then this won't work anymore.
Anyone a better idea?
thanks!
Stephan.
|
|
|
|
|
stephan_007,
Why would you want to do this? there are already methods in place to do this.
Regards,
Gareth.
|
|
|
|
|
Why take a string as input? Even an object would be better:
if ( input is int ) ((int) input).ToString ( formatstring ) ;
else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;
But would calling the appropriate ToString directly help?
someint.ToString ( "#,##0.0000" ) ;
somedate.ToString ( "dd/MM/yyyy" ) ;
(Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)
|
|
|
|
|
i parse from a textfile which contains infos like
|value|{#,##0.0000}
|date|{0:dd/MM/yyyy}
when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring.
so the input is always a string.
i do also provide the culture info which corresponds to the string format so it is formated correctly.
<br />
input.ToString(FormatString, new CultureInfo(LocalString));<br />
|
|
|
|
|
Sounds like a bad design; go slap the person responsible.
Why doesn't the system that writes the file perform the format?
|
|
|
|
|
Hi frnds,
I am working with System.Timers.Timer
I have configured timer to trigger every 60 mins, its working fine on my machine but getting triggered every 7 minutes on server.
Is it an issue with the Timer class? I dont see any reason for this to happen...
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
Could you please post some code?
that would help to resolve your issue..
|
|
|
|
|
With a long cycle like that I'd just use a Scheduled Task.
|
|
|
|
|
60 minutes or 60 seconds?
|
|
|
|
|
I wouldn't set up a timer with an interval as large as 60 minutes, or any minutes really. If you need to use a timer, set it up to fire every second, and count the time yourself. There's a couple of ways you can check how much time has passed, and here they are:
int counter = 0;
void Timer()
{
if(counter == (60*60)) {
its been a hour, omg!
counter = 0;
}
}
And i prefer this way, since you don't have to increase a counter yourself
DateTime now = DateTime.Now;
DateTime start = DateTime.Now;
void Timer()
{
TimeSpan huh = start-now;
if(huh.Hours >= 1) {
Its been an hour, again!
start = DateTime.Now;
}
}
I'm sure there are other things you can do instead of using a timer, but if you have to...
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Thanks guys,
Sorry i was busy whole day to check your replies. Dont really know what was the problem but after reinstalling the msi it started working correctly.
SK,
Your suggestion looks interesting but i dont see any reason to do this.
Why would i like to trigger an event every second when it to take a rest for an hour??
Thanks.
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
Hi guys,
Please help! I am trying to show an animated gif on button click. in my click event I have a while loop and until the while loop condition is met I would like to show an animated gif to inform the user that the application is searching...
I have tried different methods, threads, a new form with the picturebox and image and all of them show the image but the transition/rotation of the image is not smooth. Please help on how to go about this.
Thank you in advance
Sameer
|
|
|
|
|
Sounds like your search needs to go into another thread, so that the gif code can run smoothly.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian for your response. Can you please give me an example or something, I am quite new to threading. Thanks in advance.
Sameer
|
|
|
|
|
BackgroundWorker[^] is the class I use for threading.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thank you Christian. I will give this a shot.
Sameer
|
|
|
|
|
Have you already tried to use the ImageAnimator class?
|
|
|
|
|
no I haven't, it required me to change quite a bit of my code so I have left it for now. I wish there was an easier solution
Sameer
|
|
|
|