Option Strict On and Ubound

  • Thread starter Thread starter Cindy Meister -WordMVP-
  • Start date Start date
C

Cindy Meister -WordMVP-

Hi all

Generally, it seems Option Strict ON is recommended
practice? How, then, does one reconcile
--
Option Strict On

Public MyArray(6) Single

'Some Code here

For i = 0 to Ubound(MyArray)
--
I get the error "Option Strict on disallows implicit
conversions from 'Integer' to 'Short'" on the last line
for Ubound. CType or a conversion function doesn't seem to
be the right thing, either.

Cindy Meister
 
Cindy,
How did you define i?

You can solve this two ways:

' VB.NET 2003 syntax
For i As Short = 0 to CShort(Ubound(MyArray))
Next

For i As Integer = 0 to UBound(MyArray))
Next

I normally use MyArray.Length - 1 instead of the UBound function.

Hope this helps
Jay
 
Cindy Meister -WordMVP- said:
Generally, it seems Option Strict ON is recommended
practice? How, then, does one reconcile
--
Option Strict On

Public MyArray(6) Single

'Some Code here

For i = 0 to Ubound(MyArray)

Are you sure that 'i' is declared as 'Integer'?
 
Hi Jay / Herfried

Thanks to you both. It never occurred to me <duh> that the data
types within the For...to had to match. Now that you've pointed it
out to me, it's clear that option Strict would require this. But my
VB(A)-steeped instincts did't recognize it!
I normally use MyArray.Length - 1 instead of the UBound function.
And thanks for this, Jay :-) I'm trying to convert "me" (the object
typing on the keyboard), but I suspect I picked up the wrong book
for acquiring the NET syntax (Step-by-step). I'd have thought such
"baby-step" exercises would be an excellent place to introduce .NET
syntax alternatives, but the author certainly hasn't done so!

--Cindy
 
Cindy,
typing on the keyboard), but I suspect I picked up the wrong book
for acquiring the NET syntax (Step-by-step). I'd have thought such
I'm sure its a style thing. I have enough OOP language background (C++,
Java) that using Framework directly makes sense.

I'm not sure of any specific books that "introduce .NET syntax
alternatives".

Charles Petzold's book "Programming Microsoft Windows with Microsoft Visual
Basic .NET" has 3 appendixes in the back which covers Files & Streams, Math
Classes, and String Theory. All three appendixes are good intros into the
".NET Syntax alternatives". I wish the three appendixes were available
separately! The rest of the book does a good job of covering Windows Forms,
from a perspective that does not rely on VS.NET.

Matthew MacDonald's book "Microsoft Visual Basic Programmer's Cookbook"
mostly does a good job of providing the ".NET syntax". Plus its a good
cookbook on how to do things, at least get started doing things in .NET.

Hope this helps
Jay
 
Back
Top