Question:You are creating a class named Temperature. The Temperature class
contains a public field named F. The public field F represents a
temperature in degrees Fahrenheit. You need to ensure that users can
specify whether a string representation of a Temperature instance
displays the Fahrenheit value or the equivalent Celsius value. Which
code segment should you use?
 

A Public Class Temperature
Implements IFormattable
Public F As Integer
Public Function ToString ( ByVal format As String, _ ByVal fp As IFormatProvider ) As String _
Implements IFormattable.ToString If (format = "F" Or format = Nothing) Then
Return F.ToString ()
End If
If format = "C" Then
Return ((F - 32) / 1.8). ToString ()
End If
Throw New FormatException ("Invalid format string")
End Function End Class 

B Public Class Temperature
Implements ICustomFormatter
Public F As Integer
Public Function Format( ByVal formatString As String, _ ByVal arg as object, ByVal fp As
 IFormatProvider ) _ As String Implements ICustomFormatter.Format
If formatString = "C" Then
Return ((F - 32) / 1.8). ToString ()
End If
If formatString = "F" Then Return arg.ToString ()
End If
Throw New FormatException ("Invalid format string")
End Function
End Class 

C Public Class Temperature Public F As Integer Public Function ToString ( ByVal format As String, _
ByVal fp As IFormatProvider ) As String If format = "C" Then Return ((F - 32) / 1.8). ToString ()
Else
Return Me.GetType (). ToString ()
End If
End Function End Class 

D Public Class Temperature
Public F As Integer
Protected format As String Public Overrides Function ToString () As String
If format = "C" Then
Return ((F - 32) / 1.8). ToString ()
End If
Return F.ToString ()
End Function End Class 

+ Answer
+ Report
Total Preview: 750

Copyright © 2024. Powered by Intellect Software Ltd