Category Archives: VB.NET

String.Format to set a constant in .NET

Today I began working on the base framework for a new project. In the past we would just copy an existing project and remove what we don’t need, so I thought this would be a good time to make the framework more generic to speed up this part of the process for future projects. This also would prevent having to copy and replace for constants, variables, functions, class names, etc. based on the name of the project as we’ve done in the past. I say all of this to point out something I found that was new to me.

There were constants in the framework that needed to be changed, for example:

Private Const cUserName As String = "ProjectUserName"

Where “Project” in “ProjectUserName” would need to be changed based on the name of the project, this happen for several other constants. So I tried setting the constant like this:

Private Const cName As String = String.Format("{0}UserName", AppSetting.ProjectID)

And got “Constant expression is required.” which wasn’t clear to me. After some research I found that a constant has to be set by a compile time constant and the result of String.Format isn’t a compile time constant.

I finally just changed the constant to a shared readonly variable.

Private Shared ReadOnly cUserStoreName As String = String.Format("{0}UserName", AppSetting.ProjectID)

Collection Types Commonly Used in .NET

This is a question that was asked during an interview and I didn’t know the correct answer, so I thought I would add the information I found to “My Online Notepad“. Maybe it can also help someone else.

  • Hash Tables – A collection of key/value pairs that are organized based on the hash code of the key. The Dictionary(Of TKey, TValue) and ConcurrentDictionary(Of TKey, TValue) classes have similar functionality as the Hashtable. The elements of Hashtable are of type Object, so boxing and unboxing typically occur when you store or retrieve a value type.
  • Queues – A first-in, first-out collection of objects. This is useful if you want to access the information in the same order that it is stored in the collection. There are three primary operation that can be performed on Queues and its element: Enqueue, Dequeue, Peek
  • Stacks – A simple last-in-first-out (LIFO) non-generic collection of objects. There are three primary operation that can be performed on Stacks and its element: Push, Pop, Peek
  • Dictionaries – A collection of keys and values. Typically provides better performance than a Hashtable for value types.
  • Lists – A strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

XML documentation parse error (warning in Visual Studio)

I was working with a VB.NET project today and noticed the following warning:

XML documentation parse error: Whitespace is not allowed at this location. XML comment will be ignored.

After a quick search on the web I found the problem was using the ‘&’ character which is a reserved character for XML documentation. So my solution was to change ‘&’ to ‘and’ but I also found that placing the comment inside of <![CDATA[ Comments ]]> works. Here’s an example of my comment:

''' 
''' This is a comment with an ampersand & causes a warning in Visual Studio.
''' 
''' 
''' 
''' 
Public Property s1() As String
	Get
		Return ""
	End Get
End Property

Error while trying to run project: Unable to start debugging on the web server

Open your Internet Information Services manager and from the [your machine]/Web Sites/ Default Web Site node select the virtual folder which stores your project. Right Click and select Properties to select the ASP.NET tab page. Make sure the correct version ASP.NET Version is selected.

I have VS.NET 2003 and 2005 beta 1 running side-by-side on my box and noticed ASP.NET Version 2.0 is targeted by default, even for VS.NET 2003 projects.

Inline IF’ Statement

C#

string s; 
s = (true ? "True" : "False"); //s will be "True"

VB.NET

Dim s As String
s = If(True, "True", "False") 's will be "True"
'In the following you cannot rely on a particular function 
'not being called if the other argument is selected by Expression
'(notice IIf vs. If)
s = IIf(True, "True", "False") 's will be "True"

JavaScript

var s = (true) ? 'True' : 'False'; //s will be "True"