Visual Basic Friend Error

前端 未结 1 1371
陌清茗
陌清茗 2021-01-26 07:10

Im having trouble working out making a converter for mutliple currencys using multiple subs. I keep receiving an error saying that number is a friend , and therefore cannot be u

相关标签:
1条回答
  • 2021-01-26 07:35

    Not a direct answer, but this requires more space than would work in a comment.

    You have a fundamental design error in your code. You really want to structure it more like this:

    Function CANtoUSD(Number As Decimal) As Decimal
        Dim USDConversion as Decimal = 1.0141
        Return USDConversion * Number
    End Function 
    
    Function CANtoJAP(Number As Decimal) As Decimal
        Dim JAPConversion as Decimal = 79.9392
        Return JAPConversion * Number
    End Function 
    
    Sub Main()
        Console.Writeline("Enter the CAN amount: ")
        Dim input As Decimal = Console.ReadLine()
    
        Console.WriteLine(CANtoUSD(input))
        Console.WriteLine(CANtoJAP(input))
    End Sub
    

    You don't want to mix responsibilities for you methods. The input/output should be strictly separated from the code that manipulates the data. If nothing else, this makes it easier to test that your specific conversion methods work exactly like they are supposed to, and could not be the source of your bug.

    Later on, you'll learn how to also ahave a single method that accepts a key value for both source and destination types, and does a table lookup to convert any currency to any other by knowing the conversion factor to a common currency.

    0 讨论(0)
提交回复
热议问题