Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why in VB.Net I’m getting this error

Status
Not open for further replies.

piscis

Programmer
Jun 26, 2003
29
PR
Could anyone let me know why in VB.Net I’m getting this error on the third line of code below?


ERROR:

Option Strict On disallows implicit conversions from 'System.Object' to 'VBNET.HowTo.Address'.

CODE:
ReadOnly Property CurrentAddress() As Address
Get
Return AddressBook.Items(CurrentAddressIndex - 1)
End Get
End Property

I declare like this:
Public AddressBook As AddressBook
Private _currentAddressIndex As Integer

Thanks
 
Because your declaration includes an underscore and your code doesn't. Hence Option Strict is finding an undeclared variable and refusing the compile.

Craig
 
Craig0201:

That did not resolve the problem; the error comes from this line;

Return AddressBook.Items(CurrentAddressIndex - 1)

Any idea?
 
Private _currentAddressIndex As Integer doesn't match

Return AddressBook.Items(CurrentAddressIndex - 1)

unless there is more to your code.

Craig
 
Craig:

Here is the code:Let me know what you think?

Public Class frmMain
Inherits System.Windows.Forms.Form

' members...from addressbook
Public AddressBook As AddressBook
Private _currentAddressIndex As Integer


' CurrentAddressIndex - property for the current address...
Property CurrentAddressIndex() As Integer
Get
Return _CurrentAddressIndex
End Get
Set(ByVal Value As Integer)

' set the address...
_currentAddressIndex = Value

' update the display...
PopulateFormFromAddress(CurrentAddress)

' set the label...
lblAddressNumber.Text = "Record Number is: " & CurrentAddressIndex & " of " & AddressBook.Items.Count

End Set
End Property


' CurrentAddress - property for the current address...
ReadOnly Property CurrentAddress() As Address

Get
Return AddressBook.Items(CurrentAddressIndex -

End Get
End Property
 
OK.....

So how about the AddressBook collection. What is the return type on the Item property? Might not be set correctly.

Craig
 
Try Strict requires strict typing,
You are returning an object for Address...and that line seems worng with a trailing hyphen/
but Try
Return DirectCast(AddressBook.Items(CurrentAddressIndex), Address)
OR
Return CTyoe(AddressBook.Items(CurrentAddressIndex), Address)



Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top