0
sub Main()
dim x as integer = 23
dim y as String = "323"
console.writeline(x + y)
console.readline()
end sub

This should give me an error right? cuz i cant add strings with integers.

But why is it giving me the added value with no errors? When i give a true string like "some name" it gives me error. Is VB auto converting string to integer in case of numbers?

  • 5
    Because you have not set [Option Strict](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement) to ON (which should be default) – Tim Schmelter Jul 17 '21 at 13:48
  • It depends on how do you have set the Option Strict in your project settings. If yes you get an error, if not you get the VB6 behavior – Steve Jul 17 '21 at 13:49
  • @TimSchmelter oh i am new. where can i find it? – Saratchandra MV Jul 17 '21 at 13:50
  • @SaratchandraMV: it's explained in my link above or [here](https://stackoverflow.com/questions/2454552/what-do-option-strict-and-option-explicit-do) TL;DR: "Project" menu -> " Properties..." item -> "Compile" tab -> set "Option strict" to "On" – Tim Schmelter Jul 17 '21 at 13:52
  • @Got it! Thanks! Could u also tell how to get an empty text editor at the start of a new project. cuz everytime i start a new project i get some default program. – Saratchandra MV Jul 17 '21 at 13:57
  • 1
    Firstly, how to get an empty test editor has nothing to do with the subject of this question, so it is irrelevant. Secondly, don't ask questions in comments. That's what questions are for. Finally, you absolutely don't want an empty text editor anyway, even if you think you do. – jmcilhinney Jul 17 '21 at 15:21
  • With regards to `Option Strict`, you should absolutely set it to `On` in the properties of any existing projects. You should also set it to `On` in the VS options, so that it is `On` by default for all future projects. I'm not going to tell you where to find it because you should find it for yourself, because you should have a thorough look through the options to see what's available and what you might want/need to change. Loads of people ask questions that would be answered for themselves if they did this. ALL users should look through the options of ALL new software, ESPECIALLY developers. – jmcilhinney Jul 17 '21 at 15:23
  • 1
    @jmcilhinney Ive set it ON in tools > VB defaults .. which didnt work when i opened in new project. So ive kept it ON in project settings. Also i could add the line in the code i am writing. Thank you! – Saratchandra MV Jul 17 '21 at 15:35
  • If you have set it `On` in the `VB Defaults` section of the VS options then it will be `On` by default in the properties of all new projects. That's the exact point of that section: to set the default `Option` values for VB projects. That will have no effect on any existing projects though. You should NEVER set `Option Strict Off` at the project level. Even in projects that require it for late binding, you should set it `On` for the project and then `Off` in only the files that need it. Also, use partial classes to keep the code in those files to the absolute minimum. – jmcilhinney Jul 17 '21 at 15:54
  • @jmcilhinney I donot know late binding or about files that require Strict OFF. Also dont know about partial classes. Me still learning. Will keep in mind though thanks! – Saratchandra MV Jul 17 '21 at 16:36

0 Answers0