Option strict disallows late binding - newbie

J

John Perkins

I'm a newbie and this is for a school assignment. I've
created a class and assigned a property in the class, but
when I try to access the property, it says that option
strict disallows late binding. Please help!

Here is the code

Public Class PayTotal
Public Shared mdecCommission As Decimal

Sub New()
'Constructor with empty arguments

End Sub

Sub New(ByVal WeeklySales As Decimal)
'Assign property values

Me.WeeklySales = WeeklySales
End Sub

#Region "Properties"

Protected mdecWeeklySales As Decimal

Property WeeklySales() As Decimal
Get
WeeklySales = mdecWeeklySales
End Get
Set(ByVal Value As Decimal)
mdecWeeklySales = Value
End Set
End Property

Overridable ReadOnly Property Commission() As Decimal
Get
Commission = mdecCommission
End Get
End Property

#End Region

#Region "Methods"

Public Overridable Function TotalPay() As Decimal
'Calculate the pay for salesperson

Dim decTotalPay As Decimal
Const decBASE_PAY As Decimal = 250D
Const decCOMMISSION_RATE As Decimal = 0.15D
Const decSALES_QUOTA As Decimal = 1000D

If mdecWeeklySales > decSALES_QUOTA Then
mdecCommission = mdecWeeklySales *
decCOMMISSION_RATE
Else
mdecCommission = 0
End If

decTotalPay = decBASE_PAY + mdecCommission
End Function
#End Region
End Class


Here is where I call for the data:
mSupervisorPay = New SupervisorPayTotal(CInt
(txtWeeklySales.Text))

If mSupervisorPay.commission > 0 Then
lblCommission.Text = FormatCurrency
(mSupervisorPay.commission)
lblTotalPay.Text = FormatCurrency
(mSupervisorPay.totalpay)
Else
lblTotalPay.Text = FormatCurrency(mSupervisorPay.totalpay)
End If


Any help would be greatly appreciated.
 
J

Jan Tielens

At which line does the exception happen? I assume the SupervisorPayTotal
inherits from the PayTotal class.

Keep in mind that when you want to access a member of an inherited class,
you need to cast to that class (when option stricit is on):
Dim a As PayTotal = New SupervisorPayTotal()
Textbox1.Text = a.PayTotal.PropertyOfSupervisorPayTotal
-> Will fail

Textbox1.Text = CType(a,
GetType(SupervisorPayTotal)).PropertyOfSupervisorPayTotal

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
A

Armin Zingler

John Perkins said:
I'm a newbie and this is for a school assignment. I've
created a class and assigned a property in the class, but
when I try to access the property, it says that option
strict disallows late binding. Please help!

Code:
Here is where I call for the data:
mSupervisorPay = New SupervisorPayTotal(CInt
(txtWeeklySales.Text))

If mSupervisorPay.commission > 0 Then
lblCommission.Text = FormatCurrency
(mSupervisorPay.commission)
lblTotalPay.Text = FormatCurrency
(mSupervisorPay.totalpay)
Else
lblTotalPay.Text = FormatCurrency(mSupervisorPay.totalpay)
End If[/QUOTE]

It tried the code, but the declerations for mSupervisorPay and
SupervisorPayTotal are missing. (also the 3 controls but they could be
declared as what they obviously are).
 
H

Herfried K. Wagner [MVP]

* "John Perkins said:
I'm a newbie and this is for a school assignment. I've
created a class and assigned a property in the class, but
when I try to access the property, it says that option
strict disallows late binding. Please help!

Add an 'Option Strict Off' on top of your source file.

SCNR
 
G

Guest

Solved - Kind of!

I'm still not sure why the late binding thing happens, but I turned strict off and it works. My other problem was that there was no return statement in my base PayTotal class for the values I was looking for.

Thanks for the very quick and helpful respones, it is greatly appreciated.

John
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?Sm9obiBQZXJraW5z?= said:
Solved - Kind of!
;-)

I'm still not sure why the late binding thing happens, but I turned
strict off and it works.

My response was sort of "joke". It's better ("recommended") to work
with 'Option Strict On'.
 
G

Guest

-----Original Message-----
* =?Utf-8?B?Sm9obiBQZXJraW5z?= <[email protected]> scripsit:

My response was sort of "joke". It's better ("recommended") to work
with 'Option Strict On'.

Yeah, I caught that. It's weird that it works with strict
off though. I'm still very new to the whole idea of
creating classes though, so I could be doing something
wrong. If anyone is interested, I would like to show you
the whole code so I can learn what I did wrong (if
anything).

I do suspect that VB.net is just plain screwy sometimes
though. I have had a problem with updating my database as
well, and I know for a fact that the code is 100% correct,
it just doesn't work. Strange.

Thanks again for all the help.
 
H

Herfried K. Wagner [MVP]

* said:
Yeah, I caught that. It's weird that it works with strict
off though. I'm still very new to the whole idea of
creating classes though, so I could be doing something
wrong. If anyone is interested, I would like to show you
the whole code so I can learn what I did wrong (if
anything).

I think it's better to split it up to some smaller questions, it's
easier for the reader to answer small questions than reading 100 KB of
code.

;-)
 

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