Anas
Anas

Reputation: 21

Why do we need Explicit Conversion

Dim Num As Integer = Console.ReadLine

If above line Console.ReadLine returns String and Num is Integer then what is the significant importance to do explicit conversion? Why not everytime accept the value in desired DataType then keep calm?

Upvotes: 1

Views: 133

Answers (2)

miroxlav
miroxlav

Reputation: 12184

Imagine you are searching a bug in program which has

  • thousands lines of code
  • 10 contributing authors, 5 of them no more available
  • hundreds of automatic conversions between strings and numbers

With those automatic conversions, you give unnecessarily wide space to bugs. On the other hand, by disallowing automatic conversions you force programmers think at every place where they put them:

  • I need a conversion. Why? Am I really using most suitable data types for the task?
  • Can the input contain characters which will cause the conversion fail?
  • What if the input is null, empty string or contains only whitespace?
  • What if the input has correct characters, but is too long?
  • What if the input exceeds value range allowed for destination type?
  • If client's computer is switched to locale which uses specific digit symbols (not 0-9, but Arabic or Tamil digits) will the conversion still work?
  • Should I use invariant format in the given place or national one, affected by selected locale?
  • Where flow of the program will continue if conversion fails here? (E.g. if there is an exception.)

Can you see how many things you can neglect by performaing automatic conversions and how many bugs can arise from that?

Even debugging the code with explicit conversions is usually faster, because after automatic conversion you can think: Was it done correctly? So you can feel you always have to check one more thing. Because automatic conversion is much more a blackbox than explicit conversion is. Who can remember all its behaviors?

Considering aspects of each conversion (as shown in questions above) and implementing thoughtful decisions (although through explicit notation) dramatically improves the quality of source code. This is crucial factor in large applications.

So VB pros use at least these two options:

Option Explicit On
Option Strict On

And usually also

Option Infer Off

By this approach, your are investing more work at the beginning, this will pay off later. And as Steve said in comments, It depends if you want to be professional or not.

Upvotes: 3

CypherPotato
CypherPotato

Reputation: 230

It is not necessary if you know the value that is being sent is an integer, but if you don't know what he really entered is good to create a CType to create a type conversion. Example:

 'OK
 Dim x As Int32 = "1246"
 'Error
 Dim y As Int32 = "ABC"

Automatically, the Visual Basic Parser converts the values of one type to another type (That of the Option Strict Off)

 Dim A As Int32 = "3587"  'String type => Integer

 ' Automatically, the Parser will do this:
 Dim B As Int32 = CType("3587".ToString, Int32)

This is done in each structure or class, all of which have a CType Narrow and Wide, that converts the type of structures, example:

 Public Shared Widening Operator CType(ByVal a As String, ByVal b As Int32) As Int32
     Dim TMP% = a.ToString 'Cast
     Return CInt(TMP)
 End Operator

There is a Boolean function that returns true if the string is derived from an integer and can be converted, this will make your code and will exempt many Try...End Try:

 Try
     Dim n As Int32 = "abc"
 Catch ex As Exception
     MsgBox("Invalid cast")
 End Try

 'replace with:

 Dim yourInput$ = "AbcDef447"
 Dim _yourInput$ = "3867"
 Dim myNum As Int32? = 0

 If IsNumeric(yourInput) Then
     Console.WriteLine("yourInput can be an integer.")
     myNum = yourInput
 ElseIf IsNumeric(_yourInput) Then
     Console.WriteLine("_yourInput can be an integer.")
     myNum = _yourInput
 Else
     Console.WriteLine("No one can be an integer.")
     GC.SupreessFinalize(myNum)
 End If

 'Output:
 '_yourInput can be an integer.
 'then finally: myNum is 3867.

Anyway, it's always good to use methods that can check whether the type can be converted, try with the TryParse method (available in all types, e.g. Int32.TryParse) or with CType operators.

Upvotes: 2

Related Questions