Multiline strings in VB.NET

You can use XML Literals to achieve a similar effect: Imports System.XML Imports System.XML.Linq Imports System.Core Dim s As String = <a>Hello World</a>.Value Remember that if you have special characters, you should use a CDATA block: Dim s As String = <![CDATA[Hello World & Space]]>.Value 2015 UPDATE: Multi-line string literals were introduced in Visual Basic … Read more

#if Not Debug in c#?

You would need to use: #if !DEBUG // Your code here #endif Or, if your symbol is actually Debug #if !Debug // Your code here #endif From the documentation, you can effectively treat DEBUG as a boolean. So you can do complex tests like: #if !DEBUG || (DEBUG && SOMETHING)

How should I cast in VB.NET?

Those are all slightly different, and generally have an acceptable usage. var.ToString() is going to give you the string representation of an object, regardless of what type it is. Use this if var is not a string already. CStr(var) is the VB string cast operator. I’m not a VB guy, so I would suggest avoiding … Read more

Is String.Format as efficient as StringBuilder

NOTE: This answer was written when .NET 2.0 was the current version. This may no longer apply to later versions. String.Format uses a StringBuilder internally: public static string Format(IFormatProvider provider, string format, params object[] args) { if ((format == null) || (args == null)) { throw new ArgumentNullException((format == null) ? “format” : “args”); } … Read more

Linq to Sql: Multiple left outer joins

This may be cleaner (you dont need all the into statements): var query = from order in dc.Orders from vendor in dc.Vendors .Where(v => v.Id == order.VendorId) .DefaultIfEmpty() from status in dc.Status .Where(s => s.Id == order.StatusId) .DefaultIfEmpty() select new { Order = order, Vendor = vendor, Status = status } //Vendor and Status properties … Read more

.NET Configuration (app.config/web.config/settings.settings)

Any configuration that might differ across environments should be stored at the machine level, not the application level. (More info on configuration levels.) These are the kinds of configuration elements that I typically store at the machine level: Application settings Connection strings retail=true Smtp settings Health monitoring Hosting environment Machine key When each environment (developer, … Read more

Mixing C# & VB In The Same Project

No, you can’t. An assembly/project (each project compiles to 1 assembly usually) has to be one language. However, you can use multiple assemblies, and each can be coded in a different language because they are all compiled to CIL. It compiled fine and didn’t complain because a VB.NET project will only actually compile the .vb … Read more

Classes vs. Modules in VB.NET

Modules are VB counterparts to C# static classes. When your class is designed solely for helper functions and extension methods and you don’t want to allow inheritance and instantiation, you use a Module. By the way, using Module is not really subjective and it’s not deprecated. Indeed you must use a Module when it’s appropriate. … Read more

What is the C# version of VB.net’s InputDialog?

Add a reference to Microsoft.VisualBasic, InputBox is in the Microsoft.VisualBasic.Interaction namespace: using Microsoft.VisualBasic; string input = Interaction.InputBox(“Prompt”, “Title”, “Default”, x_coordinate, y_coordinate); Only the first argument for prompt is mandatory

How to loop through all the properties of a class?

Use Reflection: Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine(“Name: ” + property.Name + “, Value: ” + property.GetValue(obj, null)); } for Excel – what tools/reference item must be added to gain access to BindingFlags, as there is no “System.Reflection” entry in the list Edit: You can also … Read more