-1

I have activated Option Explicit and Option Strict, and it appears to me that I get some errors:

Option Strict On disallows implicit conversion from 'String' to 'Double'

and

Option Strict On disallows implicit conversion from 'Double' to 'String'.

Why do I get these errors?

If I remove Option Strict, I have no more errors. Some errors may not be fixed, so what should I do? Why use Option Strict if these errors occur?

TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1
TxtCheckList.Text = TxtBoxIntDrawsCount.Text - 1

Code:

Private Sub BttImport_Click(sender As Object, e As EventArgs) Handles BttImport.Click
    TxtBoxIntDraws.Clear()
    TxtBoxIntDraws.Text = System.IO.File.ReadAllText(My.Application.Info.DirectoryPath + ("\itemInfo.txt"))
    Dim sr As New IO.StreamReader(My.Application.Info.DirectoryPath + ("\itemInfo.txt"))
    Dim strLines() As String = Strings.Split(sr.ReadToEnd, Environment.NewLine)
    TxtBoxIntDrawsCount.Text = strLines.Length
    sr.Close()
    TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1
    TxtCheckList.Text = TxtBoxIntDrawsCount.Text - 1
    TxtBoxIntDraws.Text = String.Join(Environment.NewLine, TxtBoxIntDraws.Lines.Select(Function(l) String.Join(",", l.Split(",").Select(Function(s) Integer.Parse(s)))))
End Sub
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You should be parsing `TxtBoxIntDrawsCount.Text` into an integer before taking 1 away from it. Because if text was put into this textbox, it would cause an error – Cal-cium Oct 01 '19 at 15:39
  • 1
    If you hover over the problem lines, does it offer suggestions to correct them? – Andrew Morton Oct 01 '19 at 15:42
  • You need to be doing some error checking and, as @Cal-cium noted above, conversion from string to integer. Look at `Convert.ToInt32()` for your conversion. But you should first look at the text string in your `TxtBoxIntDrawsCount.Text` or use a `Try Catch` block to assure that the string only has numbers that fit in the range you require. – daShier Oct 01 '19 at 15:47
  • 1
    ... such as `TxtCheckDraws.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()`. – Andrew Morton Oct 01 '19 at 15:47
  • @AndrewMorton I would only add that there needs to be some error checking on the user input in `TxtBoxIntDrawsCount.Text` or the program will crash. – daShier Oct 01 '19 at 15:49
  • 1
    @daShier It's worse than that: `TxtBoxIntDrawsCount.Text = strLines.Length` is only two lines up. – Andrew Morton Oct 01 '19 at 15:52

2 Answers2

4

Look at the individual components of

TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1

TxtBoxIntDrawsCount is a TextBox. The Text property is a string. You try to subtract 1 from a string. This is not right. First, convert the text to an integer. The following will result in an integer.

Integer.Parse(TxtBoxIntDrawsCount.Text) - 1

We have another TextBox TxtCheckDraws and another Text Property which is also a string. We can't assign an integer Integer.Parse(TxtBoxIntDrawsCount.Text) - 1 to a string. So we should convert it back to a string first.

TxtCheckDraws.Text = (Integer.Parse(TxtBoxIntDrawsCount.Text) - 1).ToString()

This works as long as TxtBoxIntDrawsCount really had an integer in it. If it had, for example, the string "four" instead of the string "4", then you will have a problem. You will want to add some validation. There are many ways to do this and they are outside the scope of the question.

djv
  • 15,168
  • 7
  • 48
  • 72
  • Please also check out @OliverJacot-Descombes answer because he included a good way to add the validation I had mentioned. – djv Oct 01 '19 at 16:02
4

Option Explicit On forces you to declare all variables. Option Strict On forces you to specify conversions explicitly. According to Option Strict Statement:

When Option Strict On or Option Strict appears in a file, the following conditions cause a compile-time error:

  • Implicit narrowing conversions
  • Late binding
  • Implicit typing that results in an Object type

The advantage is, that it results in a code that explicitly states what the programmer had in mind. You can still perform implicit widening conversions. e.g. an implicit conversion from Integer to Double still works.

Change your code to

TxtCheckDraws.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()
TxtCheckList.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()

This makes clear that you want to interpret the content of the textbox as an Integer, then want to subtract 1 from it and finally want to convert the result back to a String.

It is also a good idea to use Integer.TryParse, in case the user entered an invalid number

Dim number As Integer

If Integer.TryParse(TxtBoxIntDrawsCount.Text, number) Then
    TxtCheckDraws.Text = (number - 1).ToString()
Else
    ' Tell the user to enter a correct number
End If

See also:

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188