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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
@Code ' VB code block – notice Dim, no semicolons, End If / End For Dim name As String = "Webliance" Dim city As String = "Hyderabad" Dim temperature As Integer = 33 Dim isWeekend As Boolean = Now.DayOfWeek = DayOfWeek.Saturday OrElse _ Now.DayOfWeek = DayOfWeek.Sunday Dim foods As New List(Of String) From { "Hyderabadi Biryani", "Haleem", "Pathar ka Gosht", "Irani Chai", "Osmania Biscuit", "Double Ka Meetha" } Dim greeting As String If Now.Hour < 12 Then greeting = "Good Morning" ElseIf Now.Hour < 17 Then greeting = "Good Afternoon" Else greeting = "Good Evening" End If Dim message As String = "Welcome to " & city & "! It's " & temperature & "°C today." End Code <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Razor VB Variables Example</title> </head> <body> <h1>@greeting, @name from @city! 🌟</h1> <p>@message</p> <p>Current server time: @Now.ToString("dddd, dd MMMM yyyy - hh:mm tt")</p> <h3>Hyderabad Must-Try Foods:</h3> <ul> @For Each food In foods @<li>@food.ToUpper()</li> Next </ul> @If foods.Count >= 4 Then @<p style="color: green; font-weight: bold;">Wow — full Hyderabadi menu today!</p> End If </body> </html> |
C# Version (same page – Index.cshtml) – for comparison
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
@{ var name = "Webliance"; var city = "Hyderabad"; var temperature = 33; var isWeekend = DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday; var foods = new List<string> { "Hyderabadi Biryani", "Haleem", "Pathar ka Gosht", "Irani Chai", "Osmania Biscuit", "Double Ka Meetha" }; string greeting; if (DateTime.Now.Hour < 12) { greeting = "Good Morning"; } else if (DateTime.Now.Hour < 17) { greeting = "Good Afternoon"; } else { greeting = "Good Evening"; } var message = $"Welcome to {city}! It's {temperature}°C today."; } <!-- HTML same as VB version above --> <h1>@greeting, @name from @city! 🌟</h1> <!-- ... rest identical ... --> |
4. Common VB Variable Patterns You’ll See
Declaring with explicit type
|
0 1 2 3 4 5 6 7 8 9 10 11 |
@Code Dim price As Decimal = 399.99D ' D suffix for Decimal literal Dim rating As Double = 4.7 Dim isAvailable As Boolean = True Dim today As Date = Now.Date End Code |
Using Option Infer (VB default – lets you skip type)
|
0 1 2 3 4 5 6 7 8 |
Dim count = 42 ' infers Integer Dim name = "Rahul" ' infers String Dim items = New List(Of String) From {"A", "B"} |
Arrays & Collections
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Code Dim numbers() As Integer = {1, 2, 3, 4, 5} Dim cities As New List(Of String) From { "Hyderabad", "Bangalore", "Chennai", "Mumbai" } End Code <ul> @For Each city In cities @<li>@city</li> Next </ul> |
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! 🚀🇮🇳 😊
