VB 2010: DIM...

  • Thread starter Thread starter Eric van Dijken
  • Start date Start date
E

Eric van Dijken

Dim Hundred as Integer
Hundred = 100

won't work.

What's wrong??
(I'm a vb6 programmer.)

Eric
 
Hello Eric,
Dim Hundred as Integer
Hundred = 100
won't work.

What's wrong??
(I'm a vb6 programmer.)

It should be fine, but needs to happen within a class / method.

also ... In Vb.Net you can comine them
 
Am 09.03.2010 17:44, schrieb Eric van Dijken:
Dim Hundred as Integer
Hundred = 100

won't work.

What's wrong??
(I'm a vb6 programmer.)

Yes, what's wrong? Which error message do you get?
Assignment must be inside a method.
 
Should work,

However, I assume that you are not using this in a method (a Sub or a
Function)

In VB2008 this is the same with Option Infer On like

Dim Hundred = 100

It is not like VB6 where the datatype was resolved at runtime.

This infers what it has to be and is

Dim Hundred as Integer = 100

At designe Time

Cor
 
Rory Becker said:
Hello Eric,


It should be fine, but needs to happen within a class / method.

also ... In Vb.Net you can comine them
-------------------------------------------------------------
Dim Hundred as Integer =
100 -------------------------------------------------------------
in later versions you can trun on Option infer and you'll be able to
let the compiler figure out the Integer bit.
OK, thank you!
I put that code at the beginning at formload.
But I'll look for, as you said: it needs to happen within a class /
method.
Probably youcan tell how I make a class / method?
I have a lot to learn for VB2010...!

Thanks.
Eric
 
It is not like VB6 where the datatype was resolved at runtime.

Huh ??

If you code with the implicit option in VB6 the datatypes are variants

In VB.Net the counterpart would be a object datatype wit implicit coding


Wich IMHO is sloppy coding

HTH

Michel
 
Eric van Dijken said:
OK, thank you!
I put that code at the beginning at formload.

Do you mean "inside" the Form_Load sub such as :

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Hundred As Integer
Hundred = 100
End Sub


Then it should work.

Dim Hundred As Integer
Hundred = 100
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub

won't work as Hundred=100 is not inside a method (i.e. a sub or function).

Telling us the exact error message you get is always helpfull...
Probably youcan tell how I make a class / method?
I have a lot to learn for VB2010...!

Same as in VB6, you use the function or sub function. If you are quite
advanced with VB6, it should be fairly eazsy. The more difficult is to
familizarize yourself with the huge class library made available...
 
Rory Becker said:
Hello Eric,


It should be fine, but needs to happen within a class / method.
This what I did:
Public Class Form1

?

Dim A1, B1, C1, D1, E1, F1, G1, H1 As Integer

Dim A2, B2, C2, D2, E2, F2, G2, H2 As Integer

Dim A3, B3, C3, D3, E3, F3, G3, H3 As Integer

Dim A4, B4, C4, D4, E4, F4, G4, H4 As Integer

Dim A5, B5, C5, D5, E5, F5, G5, H5 As Integer

Dim A6, B6, C6, D6, E6, F6, G6, H6 As Integer

Dim A7, B7, C7, D7, E7, F7, G7, H7 As Integer

Dim A8, B8, C8, D8, E8, F8, G8, H8 As Integer

a1=21

?

End Class



Please tell me what is exactly wrong...
thank you,

Eric
 
This what I did:
Public Class Form1

?

Dim A1, B1, C1, D1, E1, F1, G1, H1 As Integer

Dim A2, B2, C2, D2, E2, F2, G2, H2 As Integer

Dim A3, B3, C3, D3, E3, F3, G3, H3 As Integer

Dim A4, B4, C4, D4, E4, F4, G4, H4 As Integer

Dim A5, B5, C5, D5, E5, F5, G5, H5 As Integer

Dim A6, B6, C6, D6, E6, F6, G6, H6 As Integer

Dim A7, B7, C7, D7, E7, F7, G7, H7 As Integer

Dim A8, B8, C8, D8, E8, F8, G8, H8 As Integer

a1=21

?

End Class



Please tell me what is exactly wrong...
thank you,

Eric

It's been years since I've looked at VB6, but even in that, did you not
have to put this code in a sub or function?
 
It's been years since I've looked at VB6, but even in that, did you not
have to put this code in a sub or function?

I believe in VB6 that was legal - and was equivalent to the Private keyword.
VB.NET no longer allows that - and has deprecated the use of DIM at the
class/module level.
 
I believe in VB6 that was legal - and was equivalent to the Private keyword.
VB.NET no longer allows that - and has deprecated the use of DIM at the
class/module level.

You are probably right Tom, but what about the line a1 = 21?
 
Eric van Dijken said:
OK, thank you!
I put that code at the beginning at formload.
But I'll look for, as you said: it needs to happen within a class /
method.
Probably youcan tell how I make a class / method?
I have a lot to learn for VB2010...!

Classes and Methods really are less about VB 2010 and more about
object-oriented programming in general. If you want to write VB (of any
version), you really should learn and understand the basic concepts of
object-oriented programming, which don't change regardless of which
object-oriented language you use.

-Scott
 
Am 09.03.2010 18:08, schrieb Cor Ligthert[MVP]:
It is not like VB6 where the datatype was resolved at runtime.

This was not the case in any of my VB6 projects thanks to 'Option Explicit'.
 
Tom,

Am 10.03.2010 01:00, schrieb Tom Shelton:
Dim A8, B8, C8, D8, E8, F8, G8, H8 As Integer

a1=21

?

End Class

[...]

I believe in VB6 that was legal - and was equivalent to the Private keyword.
VB.NET no longer allows that - and has deprecated the use of DIM at the
class/module level.

It's still legal, as is 'Private Dim A8 As Integer'.
 
Tom,

Am 10.03.2010 01:00, schrieb Tom Shelton:
Dim A8, B8, C8, D8, E8, F8, G8, H8 As Integer

a1=21

?

End Class

[...]

I believe in VB6 that was legal - and was equivalent to the Private keyword.
VB.NET no longer allows that - and has deprecated the use of DIM at the
class/module level.

It's still legal, as is 'Private Dim A8 As Integer'.

Is it?... trying it... blushing... Wow! all these years, and I was under
the impressions that Dim was now restricted to method scope only. Not that I
would have used it in any other way anyway :)
 
Thank you for the selective quoting which placed my reply in a complete
different context than meant by me.

Or would I understand from you that an in VB6 accepted

Dim Hundred = 100 would always give an assignment of 100 in an integer

Always glad that I can learn something

Cor
 
Thank you for the selective quoting which placed my reply in a complete
different context than meant by me.

Or would I understand from you that an in VB6 accepted

Dim Hundred = 100 would always give an assignment of 100 in an integer

Always glad that I can learn something

Cor
 
Cor ,

I answer to your question ( as seen below ) a i do not dare to quote
anything of you now as it might be seen as "selective"

The answer is No you would not be able to declare a variabel like that at
all

Michel
 
Actually Cor ,


If you use the "professional" coders settings in VB6 ( as described in the
official core refence guide of VB6 ) that is Option explicit on and the
"require variable declarion setting"switched to the on position in the
General tab of the options dialogbox the below would not function at all as
VB6 wil then enforce a type declaration .

But now comes the big surprise

If you do this in VB6 without the above mentioned options


Dim MyVariable

Myvariable=100

Guess what happens ? ,,,,,,, well VB6 then creates a Variant Data Type

A Variant is a special kind of datatype Bytes 0 and 1 hold the Vartype Bytes
2 through 7 are unused and bytes 8 through 15 hold the value
Bytes 0 and 1 hold an integer value that states which type of data is stored
in bytes 8 through 15 bytes 2 - 7 are unused with only one exception (
the decimal subtype )

if a variant holds an Integer value as in the above example the first two
bytes hold the value 2-VbInteger bytes 8 and 9 hold the actual 16 bit value
and all other bytes are unused

A Variant variable holds a value in its original format , when Visual basic
adds numbers hold in 2 Variants it checks their types and uses the most
efficient math routine possible
also if you are adding two Variants that hold one Integer and one Long
Visual Basic 6 promotes the Integer to a Long and the invokes the routine
for addition between Longs


So conclusion

Dim Hundred
Hundred=100

Wil actually give you a integer , wich can be proven in VB6 wit this simple
line of code

Print Vartype(Hundred)

wich wil return 2-VbInteger

In the Netherlands we have a saying "selling old wine in new bags" guess
what i though of the infer option when it was introduced ? ( although
ofcourse it is an evolution )

Regards

Michel Posseth
 
Back
Top