2

This is ok in C#:

private int total;
public int Total {
    get {
        return total;
    }
}

So the backing field total is spelled the same as the Property Total with the only thing telling them apart being the case of the T.

With VB.NET VS complains if I try to do the following. In fact it won't even let me write Return total with a lower case t and auto-corrects to an upper case T

enter image description here

But if I spell the backing field differently then it seems to be ok:

Private fTotal As Integer
Public ReadOnly Property Total() As Integer
    Get
        Return fTotal
    End Get
End Property

I realize they are different languages but is there a logical reason behind this difference? (EDIT originally said "apparent inconsistency" rather than "difference")

Also - I assume even though Visual Studio auto-corrects the case of my VB.NET but in reality this language is not case-sensitive?

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • Have a look at this http://stackoverflow.com/questions/2300983/is-vb-really-case-insensitive – V4Vendetta Dec 13 '12 at 09:11
  • 1
    Actually, VB.Net will compile to CLR and CLR is case-sensitive, but the VS IDE compiles in the bg and will "auto-convert" things to be the same case. http://stackoverflow.com/questions/2300983/is-vb-really-case-insensitive – Alvin Wong Dec 13 '12 at 09:11
  • "In reality" C# and VB.NET are compiled to the same code. Btw, if the compiler would allow your first snippet you would have produced an **infinite loop**. To prevent you from doing so it don't allow it in VB (also because VB.NET developers expect VB to be case-insensitive). – Tim Schmelter Dec 13 '12 at 09:12
  • You can write a Library in C#, then reference it in VB.Net. I believe things that causes confusion will be "quoted" with `[` and `]`. (e.g. `Shared` is a reserved word in VB.Net so you need to use `[Shared]` for a variable/field/identifier) – Alvin Wong Dec 13 '12 at 09:12
  • 1
    You may use `_total` in VB – Teejay Dec 13 '12 at 09:40
  • @TimSchmelter ....that first snippet was after I'd typed `total` into the IDE and it auto-changed it to `Total` so I wasn't aiming for a stack overflow – whytheq Dec 13 '12 at 12:04

5 Answers5

5

I realize they are different languages but is there a logical reason behind this apparent inconsistency?

The original reason is simply historical: VB is based on BASIC which, like other languages at the time (FORTRAN) was case insensitive (but was usually written in all-uppercase).

Furthermore, I don’t see any inconsistency: inside VB, the casing is entirely consistent. In particular, it’s not “sort of case-sensitive” as your title asks.

There is a logical reason to be case insensitive, even today: it makes it harder to introduce bugs due to name clashes; consider the following C# code:

private int total;

public int Total {
    get { return total; }
    set { Total = value; }
}

Did you spot the error immediately? If so, not bad (the syntax highlight here helps). In VB, this class of errors cannot happen. But in practice I think this class of bugs isn’t all that problematic because once identified they are easily eliminated. So while this is a reason for case insensitivity, it’s not a very strong one.

Finally, notice that Windows and OS X file systems use the same convention as VB here: the file system is case insensitive (filename case doesn’t matter) but case aware – meaning the file system preserves the original casing of a filename and displays it correctly but when comparing, it doesn’t consider case.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • what would be consistent then would be if VS changed all `VB.NET` commands to either uppercase or lowercase; one or the other but not propercase - maybe I'm just being a pedant? – whytheq Dec 13 '12 at 09:44
  • 1
    @whytheq I don’t see why VB should do that. When you write text you probably also write in proper case, alternating between capitals and lower case as appropriate, and still expect other people to be able to read this, am I right? A proper spelling is simply more readable than all uppercase or all lowercase. – Konrad Rudolph Dec 13 '12 at 09:47
  • @KonradRudolph ....ok agreed (although we could then go back to C# and ask why doesn't VS display all C# in proper case if that makes things more readable); thanks for the help on this; I think I just need to accept that apples and pears are different. – whytheq Dec 13 '12 at 11:40
1

VB.NET is not case-sensitive, it was designed to be easy to learn for Visual Basic programmers, and shares this trait with Visual Basic.

C# is case-sensitive, it's a different language than VB.NET. Even if they are both used for .NET development, they don't have to be consistent. You can use many languages for .NET development, and the differences between these languages are many and not limited to case-sensitivity.

Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
1

There are several points to address:

  • The language Visual Basic is always case-insensitive. The compiler doesn't care if you declare a variable one way and use it another.

  • The IDE (Visual Studio) will helpfully fix the case of variable usages to match the actual declaration. You may be able to turn this off in the settings, but I've never tried so I don't know if it's actually possible.

Now back to your code:

Private total As Integer
Public ReadOnly Property Total() As Integer
    Get
        Return Total
    End Get
End Property

There are actually two bugs here:

  1. You have two members with the same name, a field called total and a property called Total. They're the same name because they're compared case-insensitively (the compiler shows you an error for this - the blue squiggly line in your screenshot).

  2. The IDE auto-corrects 'total' to 'Total' inside the property, because you're actually referring to the property, not the field. The compiler won't show you an error because of this, but if you were to remove the field (so that your program compiles), you will run into a stack overflow at runtime because the property would call itself. Note that even if you did manage to turn of the IDE's auto-correct, you'd still be referring to the property, since it's looked up case-insensitively.

Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
0

The VB.Net compiler is case insensitive in so far that for all intent and purposes it forbids the use of fields with the same name and the only difference of a upper case or lower case letter.

The underlying CLI (common language interface) and CLR (common language runtime) do support case differences. However the c# example given is not CLS valid.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
0

Actually, the problem here is nothing to do with the different case field named 'total'. You'll see the same problem if you remove that field.

The problem is that VB allows you to optionally set the value to be returned by a property or a function via a hidden local variable named the same as the property or function and then to return that hidden variable.

e.g., this will work:
Public ReadOnly Property Total() As Integer
    Get
        Total = 3  'setting the hidden VB variable
        Return Total 'returning the hidden VB variable
    End Get
End Property

VB even allows omitting the return statement in this case:

Public ReadOnly Property Total() As Integer
    Get
        Total = 3  'setting the hidden VB variable
    End Get 'since the hidden VB variable was set, it is returned implicitly
End Property

These sort of VB-isms can make it very frustrating to interpret what is really happening in VB code.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28