Option Strict On

bigboywasim

Member
Joined
Sep 29, 2006
Messages
18
Programming Experience
Beginner
I have the option strict on but it says that I can't convert string to decimal. The ticket price has to be as string. That is how my professor wants it. Please tell me how I can fix my code. Thanks guys.

VB.NET:
Public Function TicketPrice() As String
If mstrDestination = "Washington,DC" Then
TicketPrice = mdecWASHINGTON_DC_PRICE * mdecTAX_RATE
ElseIf mstrDestination = "Vancouver,WA" Then
TicketPrice = mdecVANCOUVER_WA_PRICE * mdecTAX_RATE
ElseIf mstrDestination = "Chicago,IL" Then
TicketPrice = mdecCHICAGO_IL_PRICE * mdecTAX_RATE
End If
Return FormatCurrency(TicketPrice())
End Function

VB.NET:
Option Strict On
Public Class clsRevisedFlightInformation
Inherits clsFlightInformation
#Region "Final Ticket Price Caculation"
 
'CALCULATES FINAL TICKET PRICE
'***************************************************************************************************************************************************
' -- Calculates Final Ticket Price.
'***************************************************************************************************************************************************
 
Public Function FinalTicketPrice() As String
FinalTicketPrice = TicketPrice() + 10D
Return FormatCurrency(FinalTicketPrice)
End Function
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dec [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Decimal[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDec[/COLOR][/SIZE](thestring)
 
Formatted your code to make it easier to read and added some comments (suggestions).

VB.NET:
Public Function TicketPrice() As String ' <---- this is the return type
       If mstrDestination = "Washington,DC" Then
            ' ************
            ' Don't return like this, use Return <something>
            TicketPrice = mdecWASHINGTON_DC_PRICE * mdecTAX_RATE '<- this returns whatever type those 2 variables are
            ' Return (mdecWASHINGTON_DC_PRICE * mdecTAX_RATE).ToString("C2") <- converts to currency string
       ElseIf mstrDestination = "Vancouver,WA" Then
            TicketPrice = mdecVANCOUVER_WA_PRICE * mdecTAX_RATE
       ElseIf mstrDestination = "Chicago,IL" Then
            TicketPrice = mdecCHICAGO_IL_PRICE * mdecTAX_RATE
       Else
            ' Handle case that doesn't match any of the previous cases
       End If
       ' Get rid of this statement
       Return FormatCurrency(TicketPrice())
End Function

This should work.
 
Sorry to bring back this old thread, but I'm having an issue with this assignment. I tried the code that SDavied suggested but it didn't work. So this is what I have:

clsFlightInformation
VB.NET:
Overridable Function TicketPrice() As String

        Dim TotalPrice As String = ""


        If mstrDestination = "Washington, DC" Then
            TotalPrice = FormatCurrency(mdecWASHINGTON_DC_PRICE + (mdecWASHINGTON_DC_PRICE * mdecTAX_RATE))
        ElseIf mstrDestination = "Vancouver, WA" Then
            TotalPrice = FormatCurrency(mdecVANCOUVER_WA_PRICE + (mdecVANCOUVER_WA_PRICE * mdecTAX_RATE))
        ElseIf mstrDestination = "Chicago, IL" Then
            TotalPrice = FormatCurrency(mdecCHICAGO_IL_PRICE + (mdecCHICAGO_IL_PRICE * mdecTAX_RATE))
        End If


        Return FormatCurrency(TotalPrice)


    End Function

clsRevisedFlightInformation and it inherits clsFlightInformation
VB.NET:
Overrides Function TicketPrice() As String        
        Dim FinalPrice As String = ""
        FinalPrice = MyBase.TicketPrice() + 10D
        Return FormatCurrency(FinalPrice)
    End Function

When Option Strict is On I get error: "
Option Strict On disallows implicit conversions from 'String' to 'Double'." When Option Strict is Off I get no error. I've spent hours and I cannot figure out how to get rid of the error.
 
Thanks, is there a way to convert MyBase.TicketPrice() into a decimal so that 10D can be added to the value?

You can but I'd suggest that that is the wrong way to go about it. The fact that you need the value as a Decimal shows that your method should be returning a Decimal in the first place rather than a String. The formatting as currency is something you should be doing outside the object, where you actually need the currency displayed. At the very least, you should be breaking the original method into two, where one does the calculation and one does the formatting. You can then call the one that does the calculation to get the Decimal value, then do your extra calculation, then call the one that does the formatting.
 
Back
Top