|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why Randomize() won't work?This code found at the Reference page of Randomize() at MSDN won't run
om my installation ' Initialize the random-number generator. Randomize() ' Generate random value between 1 and 6. Dim value As Integer = CInt(Int((6 * Rnd()) + 1)) Why? The compiler don't like the line "Randomize()"? Do I have to add some module or something? How do I know that that I have VBA.Math in my include paths? Regards, Sebastian "Newbee" in VBA > This code found at the Reference page of Randomize() at MSDN won't run You can't declare a variable's type and assign it a value in a single> om my installation > ' Initialize the random-number generator. > Randomize() > ' Generate random value between 1 and 6. > Dim value As Integer = CInt(Int((6 * Rnd()) + 1)) > > Why? statement. Try this and see if it works for you... Dim value As Integer Randomize value = CInt(Int((6 * Rnd()) + 1)) I don't program in VBA, so I am not completely familiar with how it works; but the Randomize statement should only be run one time per running of your application. In the compiled VB that I work in, I would put the Randomize statement in the form's Load event and use the other parts of the code where required. Rick Hi Rick,
You are right, I also noticed that I couldn't declare an assign a variable at the same time but my concern was that the call "Randomize()" didn't compile. You wrote "Randomize" without any paranthesies is that how it should be done? Anyway, I am confused why the code from MSDN doesn't compile?? I'll try to just write Randomize instead of Randomize(). Thanks /Sebastian "di98mase" <di98m***@hotmail.com> wrote in message You haven't posted a link to the Microsoft code you mention, but perhaps news:1138608596.029122.15530@g14g2000cwa.googlegroups.com... > I'll try to just write Randomize instead of Randomize(). whatever it is was implying that you can if you wish follow Randomize with a number, as in Randomize(some value). VB uses that number to "seed" the randon number generator, and if you omit the number (and also omit the parentheses) then VB will use the value returned by the system timer. Normally you would use Randomize just once at the start of your code (otherwise if you repeatedly use it you will upset the "randomness" of the generator). Using a number in parenthesis with Randomize is useful because you can use it to "seed" the random number generator to a specific point (so that it repeats the same sequence of random numbers every time). To do that you use the *Rnd* function with a negative argument immediately followed by Randomize (the same value as before). Mike Based on your original post, the code MSDN sample is for VB.Net. In VB.Net
(VB 2005), all method and sub functions have the parens. VB 6 ' Initialize the random-number generator. Randomize ' Generate random value between 1 and 6. Dim value As Integer value = Int((6 * Rnd()) + 1) VB 2005 ' Initialize the random-number generator. Randomize() ' Generate random value between 1 and 6. Dim value As Integer = Int((6 * Rnd()) + 1) Mike Ober. Show quoteHide quote "di98mase" <di98m***@hotmail.com> wrote in message news:1138608596.029122.15530@g14g2000cwa.googlegroups.com... > Hi Rick, > > You are right, I also noticed that I couldn't declare an assign a > variable at the same time but my concern was that the call > "Randomize()" didn't compile. You wrote "Randomize" without any > paranthesies is that how it should be done? Anyway, I am confused why > the code from MSDN doesn't compile?? > > I'll try to just write Randomize instead of Randomize(). > > Thanks > > /Sebastian > di98mase wrote:
> This code found at the Reference page of Randomize() at MSDN won't run This code is VB.NET, not VB6. The giveaway is the second code line -> om my installation > ' Initialize the random-number generator. > Randomize() > ' Generate random value between 1 and 6. > Dim value As Integer = CInt(Int((6 * Rnd()) + 1)) > > Why? declaration and assignment in one line is a VB.NET feature. > The compiler don't like the line "Randomize()"? Empty parentheses for methods with no parameters are obligatory inVB.NET (and will be supplied by the IDE if you don't type them), but not allowed in VB6 (and won't be ignored by the compiler if you do type them). I'm not sure if you can still find help for VB6 in the online MSDN. -- Larry Lard Replies to group please Thanks everybody,
I think you helped me out here. So, the issue here is that I am using VB6 and the online reference manual is for .NET! /Sebastian "di98mase" <di98m***@hotmail.com> wrote in message That's enough to drive you nuts... eh? Hunting for VB Classic info on MSDN news:1138620317.843434.80710@g49g2000cwa.googlegroups.com... > Thanks everybody, > > I think you helped me out here. So, the issue here is that I am using > VB6 and the online reference manual is for .NET! > > > /Sebastian is getting similar to hunting for DoDo birds. -- Ken Halter - MS-MVP-VB - Please keep all discussions in the groups.. DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
How to best resize a form?
Using Control Array in User Control Why sending email programmatically must be so complicated? VB 2005 Express..NOT OT...really! File Copy Without Win Buffering VB6---.NET Proper way to resize a Form Problems with vbCtrlMask Passing data from a dll to the main VB program cRegistry problems read/writing to (Default) |
|||||||||||||||||||||||