VB 2005 Type conversion

  • Thread starter JimmyX via DotNetMonster.com
  • Start date
J

JimmyX via DotNetMonster.com

Does anyone know why I get an error message when I use the following code (it
works with VB6):


Dim J as integer
If J And (2^0) then goto ........

I get an message that implicit conversions from Double to Long are not
allowed. What is the proper
VB2005 way to implement this statement. I don't want to have to keep Option
Strict Off in order for
my program to work!

JimmyX
 
S

Scott M.

Hi Jimmy,

Before I get to your conversion code, I must just say that you've got to
approach VB .NET 2005 with the understanding that it is not just a new
version of VB 6. It is a completely different beast, it runs on an entirely
new runtime and although some of its' syntax is similar to VB 6, there
really isn't much it has in common with VB 6. That's why you'll find lots
of things that worked in VB 6 that don't in VB .NET.

Also, you should turn Option Strict on and leave it on forever in VB .NET.
This setting makes VB .NET a type-safe language like C# and although it will
mean that you will have to write more code at times, it will ensure that the
code you have is much more bullet proof come runtime.

Now, addressing your If...Then logic:

First, you should lose the "goto" as this was never considered good
programming practice (unless you go back to QBASIC). Place the code you
want to run if your condition is true in the true section of the If
construct or create a separate sub/function and simply call it from the true
portion of the If construct.

Next, your conversion problem stems from the compound statement. Your If
statement, it translates to:

If j and 2 then

Which doesn't make much sense and the compiler sees two different data types
trying to be compared. Since one side of your compound statement will
return a Double and the other side will return a Long and the compiler won't
do impicit conversions for you, you must make sure both sides of the
compound statement have the same type. This wouldn't be necessary if both
sides were expressions (if j <> 0 And 2^0 = 2 then your code would work just
fine)

If you can explain what it is you are realy trying to test, I can help you
write the If logic more clearly.

-Scott
 
S

Stephany Young

Close but no cigar.

The result of exponention {^} is always a Double.

Prior to a bitwise And operation, one of the operands MAY be converted to
another type.
If one of the operands is an Integer and the other is Decimal, Single,
Double or String then that one is converted to a Long and the result of the
bitwise And operation will also be a Long

The documentation provides a definitive table for this rule for any
combination of operand types.

The big clue is that an 'implicit conversion' is not allowed so therfore one
needs to do an explicit conversion.

Dim J as integer

If J And Convert.ToInt64(2^0) Then ... etc.



Scott M. said:
Hi Jimmy,

Before I get to your conversion code, I must just say that you've got to
approach VB .NET 2005 with the understanding that it is not just a new
version of VB 6. It is a completely different beast, it runs on an
entirely new runtime and although some of its' syntax is similar to VB 6,
there really isn't much it has in common with VB 6. That's why you'll
find lots of things that worked in VB 6 that don't in VB .NET.

Also, you should turn Option Strict on and leave it on forever in VB .NET.
This setting makes VB .NET a type-safe language like C# and although it
will mean that you will have to write more code at times, it will ensure
that the code you have is much more bullet proof come runtime.

Now, addressing your If...Then logic:

First, you should lose the "goto" as this was never considered good
programming practice (unless you go back to QBASIC). Place the code you
want to run if your condition is true in the true section of the If
construct or create a separate sub/function and simply call it from the
true portion of the If construct.

Next, your conversion problem stems from the compound statement. Your If
statement, it translates to:

If j and 2 then

Which doesn't make much sense and the compiler sees two different data
types trying to be compared. Since one side of your compound statement
will return a Double and the other side will return a Long and the
compiler won't do impicit conversions for you, you must make sure both
sides of the compound statement have the same type. This wouldn't be
necessary if both sides were expressions (if j <> 0 And 2^0 = 2 then your
code would work just fine)

If you can explain what it is you are realy trying to test, I can help you
write the If logic more clearly.

-Scott
 
S

Scott M.

I'm pretty sure that's what I said:

"your conversion problem stems from the compound statement"

"Since one side of your compound statement
will return a Double and the other side will return a Long and the
compiler won't do impicit conversions for you, you must make sure both
sides of the compound statement have the same type."

-Scott

Stephany Young said:
Close but no cigar.

The result of exponention {^} is always a Double.

Prior to a bitwise And operation, one of the operands MAY be converted to
another type.
If one of the operands is an Integer and the other is Decimal, Single,
Double or String then that one is converted to a Long and the result of
the bitwise And operation will also be a Long

The documentation provides a definitive table for this rule for any
combination of operand types.

The big clue is that an 'implicit conversion' is not allowed so therfore
one needs to do an explicit conversion.

Dim J as integer

If J And Convert.ToInt64(2^0) Then ... etc.
 
J

Jan Hyde (VB MVP)

"Scott M." <[email protected]>'s wild thoughts were
released on Fri, 12 Oct 2007 23:36:34 -0400 bearing the
following fruit:

Next, your conversion problem stems from the compound statement. Your If
statement, it translates to:

If j and 2 then

If j and 1 Then
 

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

Top