Option Strict On and Ubound

  • Thread starter Cindy Meister -WordMVP-
  • 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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
H

Herfried K. Wagner [MVP]

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'?
 
C

Cindy Meister -WordMVP-

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

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

Similar Threads

option strict 5
Option strict 4
Option Strict On 22
Option Strict and .Save with an Image 4
Error when 'option strict' 2
Conversion issue with option strict 5
option strict on !? 2
Option Strict On 8

Top