By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,743 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,743 IT Pros & Developers. It's quick & easy.

data converstion from long to int

P: n/a
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
Dec 4 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Dec 4 '05 #2

P: n/a
"wb" <none> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 4 '05 #3

P: n/a
wb
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ez****************@TK2MSFTNGP10.phx.gbl...
"wb" <none> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 5 '05 #4

P: n/a

wb wrote:
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.

Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.