Saturday, September 29, 2007

Sleep in VB.NET and C#

The sleep is sometimes hard to find in .NET. Here is an example for VB.NET and C#:

VB.NET

Imports System.Threading
.
.
.

Dim waitTime as Integer = 300

Thread.Sleep(waitTime)


C#

using System.Threading;
.
.
.
int waitTime = 300;

Thread.Sleep(waitTime);


The Sleep method can be used with both an integer and a TimeSpan parameter. An integer value can be used to specify the wait time in milliseconds, or the wait time can be specified as a TimeSpan like this:

In VB.NET:

Dim waitTime As New TimeSpan(0, 0, 0, 0, 300)

or in C#:

TimeSpan waitTime = new TimeSpan(0, 0, 0, 0, 300);


There is also a pretty good example at the following web site:

http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep(VS.71).aspx

Tuesday, September 4, 2007

Conditional Compilation in VB.NET

Like C and C++, you can perform Conditional Compilation in VB.NET:

#If COMMAND_LINE = True Then
Dim AxGTCreate1 As GTCREATELib.GTCreate = Nothing
#Else
Dim AxGTCreate1 As AxGTCREATELib.AxGTCreate = Nothing
#End If


The #if, #else, and #end if can be used.

In the example above, the variable COMMAND_LINE is used to determine which type to use. The COMMAND_LINE value is set either by using:

#Const COMMAND_LINE = True

in the code, or by setting it as a Custom Constant on the Compile table of the Project's property page. Press the Advanced Compile Options button to get to the form to set the Custom Constants.

Getting Command-Line arguments in a VB.NET Console Application

It is pretty simple to get a string of the command-line arguments in VB.NET:

Dim cl As String = Command()

It is still up to you to parse the string into something usable. Here is a link that show one approach:


http://www.averagecoder.com/article_detail.asp?query=passing_arguments_to_your_vb_net_console_application

Friday, August 31, 2007

Database Connection Strings

Getting the right connection string syntax can chew up hours of experimentation. Here is a site that can save that time for you: http://www.carlprothman.net/Default.aspx?tabid=81

Sunday, August 19, 2007

Finding the Size of a file while Writing when using StreamWriter

In VB 6, you could always use LOF() to find the length of the file you were writing; however, in VB.NET or C# is not as obvious.


Dim s As StreamWriter = New StreamWriter(filename)
.
.
.
s.BaseStream.Flush()
Dim offsetPosition as Long = s.BaseStream.Length



One thing to note, you will have to do the Flush before using the length property or you will not get accurate results. You can also set the AutoFlush property on the StreamWriter object.


For more info, see:

http://www.thescripts.com/forum/thread266352.html

Monday, August 13, 2007

Using a ListView in VB.NET

Here is a basic example of how to use the Detail View with a ListView in VB.NET.
See also AutoFitListView and CopyListViewToClipboard.

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

' Set ListView Properties
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.FullRowSelect = True
ListView1.HideSelection = False
ListView1.MultiSelect = False

' Create Columns Headers
ListView1.Columns.Add("Column A")
ListView1.Columns.Add("Column B")
ListView1.Columns.Add("Column C")

Dim tempValue As Integer = 0

For i As Integer = 0 To 9

' Create List View Item (Row)
Dim lvi As New ListViewItem

' First Column can be the listview item's Text
lvi.Text = "Row " + i.ToString

' Second Column is the first sub item
tempValue = tempValue + 1
lvi.SubItems.Add(tempValue.ToString)

' Third Column is the second sub item
tempValue = tempValue + 1
lvi.SubItems.Add(tempValue.ToString)

' Add the ListViewItem to the ListView
ListView1.Items.Add(lvi)

Next

End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles ListView1.SelectedIndexChanged

If ListView1.SelectedIndices.Count = 0 Then Return

Dim lvi As ListViewItem = _
ListView1.SelectedItems(0)

MessageBox.Show(lvi.Text + " has been selected.")

End Sub

Monday, August 6, 2007

Critical Section in C#

Here is a good article on using the lock statement to create critical sections in C#:

http://www.calcaria.net/c-sharp/2006/10/c-equivalent-csinglelock-critical.html

Here is the gist:

Object lockobj = new Object();

lock (lockobj)
{
// Do your locked work
}