data converstion from long to int

  • Thread starter Thread starter wb
  • Start date Start date
W

wb

Newbie at .net having trouble with data conversion.

I am using the random constructor and trying to pass in seed value of time
as instructed by help.

rvalue = new random(int)

dim x as long
dim z as integer
x = datetime.now.tick

z = int(x)

when the coversion takes place an exception error is thrown. maybe it isn't
possible to convert a long to an int. But that is what the constructor
takes and the help says that time is a good value to pass in.

suggestions?

WB
 
WB

Where did you get this code?
dim x as long
dim z as integer
x = datetime.now.tick

z = int(x)
It should be in VB.Net
\\\
Dim x As Long = Now.Ticks
Dim z As Integer = CInt(x)
///
I hope this helps,

Cor
 
wb said:
dim x as long
dim z as integer
x = datetime.now.tick

z = int(x)

when the coversion takes place an exception error is thrown. maybe it
isn't possible to convert a long to an int. But that is what the
constructor takes and the help says that time is a good value to pass in.

'Int' will return a floating point number. It's typically get used to
remove the fractional part of a number. You may want to use 'CInt' instead
which will perform a type conversion. Note that this conversion will fail
if 'Ticks'' value cannot be represented in an 'Integer'.
 
I have tried all possible combinations of data conversion; all with the same
outcome. The exception gets thrown when the Random constructor takes a seed
parameter such as Cint(Now.Tick). As you state "...the conversion will fail
if "Ticks" value cannot be represented in an Integer" how am I supposed to
pass in the value? The MSDN documentation on Random() uses the code I am
trying to replicate.

WB
 
wb said:
I have tried all possible combinations of data conversion; all with the same
outcome. The exception gets thrown when the Random constructor takes a seed
parameter such as Cint(Now.Tick). As you state "...the conversion will fail
if "Ticks" value cannot be represented in an Integer" how am I supposed to
pass in the value? The MSDN documentation on Random() uses the code I am
trying to replicate.

Maybe you'd want to "mask" just the least-significant 32 bits (that is,
the bits that change more often) from the value returned by Now.Ticks
before performing the conversion:

Dim Seed As Integer = CInt(Now.Ticks And &HFFFFFFFF)

HTH.

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top