Chapter 24: Razor VB Variables

Razor – VB Variables — that is, how you declare, use, and work with variables inside .vbhtml files when using VB.NET as the Razor language (instead of the much more common C#).

This is the VB.NET counterpart to the C# variables lesson we did earlier. Everything works almost the same conceptually, but the syntax is very different — and that’s where most people get confused when they see VB examples on W3Schools.

I’ll explain it slowly, like we’re sitting together comparing two open editors side-by-side: one C# .cshtml and one VB .vbhtml — same page, same logic, different language.

1. Quick Reminder – Why VB.NET in Razor Exists (2026 Context)

  • VB.NET Razor was very popular 2010–2015, especially for teams coming from Classic ASP (VBScript) or VB6/VBA
  • It’s still fully supported in ASP.NET Core (you can write .vbhtml files today)
  • But in 2026: almost no new projects start with VB Razor — Microsoft pushes C# heavily
  • W3Schools still shows both so you can see the difference (and for legacy maintenance)

Bottom line: Learn VB syntax here for understanding — but use C# for real work unless forced otherwise.

2. VB.NET Variables in Razor – The Big Differences from C#

Feature C# (.cshtml) VB.NET (.vbhtml) Notes
Code block @{ … } @Code … End Code VB is more verbose
Variable declaration var name = “value”; Dim name = “value” VB requires Dim keyword
Statement terminator ; (required) None No semicolons in VB
Case sensitivity Yes No (case-insensitive) Name = name in VB
Boolean literals true / false True / False VB capitalizes
String concatenation $”Hello {name}” or + “Hello ” & name VB uses & operator
Implicit typing var Dim with no type = Object (but usually inferred) VB infers type too
Array / List creation new List<string> { “a”, “b” } New List(Of String) From { “a”, “b” } VB uses Of and From

3. Side-by-Side Example – The Same Page in VB vs C#

Goal: Greet user, show server time, list Hyderabad foods, conditional message.

VB.NET Version (Index.vbhtml)

HTML

C# Version (same page – Index.cshtml) – for comparison

HTML

4. Common VB Variable Patterns You’ll See

Declaring with explicit type

HTML

Using Option Infer (VB default – lets you skip type)

HTML

Arrays & Collections

HTML

5. Teacher Summary – VB Variables in Razor

ASP.NET Razor – VB Variables means:

  • Declare with Dim name [As Type] = value (type optional – VB infers)
  • Code blocks use @Code … End Code (not @{})
  • No semicolons, no curly braces for blocks
  • Use & for string concatenation (not +)
  • True/False (capitalized)
  • Now instead of DateTime.Now
  • Collections use Of and From keywords
  • Case-insensitive — Name, name, NAME all same

Everything else (using @name in HTML, sharing via Page.Name, scope, etc.) is identical to C# Razor.

6. Final Advice from Hyderabad

  • Learn VB syntax here — good for understanding legacy code or W3Schools examples
  • Write new code in C# — better ecosystem, more examples, future-proof
  • If you ever maintain old VB Web Pages sites → you’ll thank yourself for knowing Dim, If … Then … End If, For Each … Next

Next questions?

  • Want to convert a full product page (with loops + conditions) from C# to VB?
  • Or see VB in forms (reading Request, validation)?
  • Or move forward to modern ASP.NET Core Razor (which is almost always C#)?

Just tell me — you’re doing excellent work! Keep rocking from Hyderabad! 🚀🇮🇳 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *