Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Give the Visual Studio Snippet Format a Whirl with 10 Pre-packaged Snippets : Page 3

You aren't limited to using the snippets that ship with Visual Studio .NET 2005; you can convert your own. As a start, here are two in-depth examples showing how to convert existing tip code to snippets, as well as eight other useful tips—already "snippetized" and ready for you to use.


advertisement
Tip 3: Trimming Strings
The following line of code is a simple and compact way of trimming strings by using VB.NET's new trimming functions. I chose this example because it can be turned into a cool little function where you can pass in the characters you want trimmed from a given string; a feature not available in VB6. You can find the original code here.

' S is the string to be trimmed. Dim cArr() As Char = { " ", Chr(9), Chr(13), Chr(10) } s = s.TrimStart(cArr)

Tip 4: Splitting Strings at Multiple Characters
This For Each loop and its declarations are a quick way to split string into substrings when the separator points may be any of a number of characters. This is good opportunity to take advantage of the <Object> declaration in your snippet file (hint: for the RegEx object). You can find the original code and explanation here.

Dim input As String = _ "This is{sep}my input string{sep}to test the " _ & "RegEx's{sep}Split method!" Dim output As String() = _ System.Text.RegularExpressions.Regex.Split( _ input, "{sep}") For Each token As String In output Debug.WriteLine(token) Next

Tip 5: Using the Clipboard Programmatically
The code displayed here shows you how to use the clipboard programmatically. I chose this one because it's the first sample where the snippet is a complete sub procedure. You can view the original code here, along with an explanation of how to make the object you copied to it available after the program terminates.



Sub CopyFromTextBox(ByVal tb As TextBox) ' Copy the TextBox's selected text to ' the clipboard. Dim t As String = tb.SelectedText ' Copy the entire Text, if no text is selected. If t.Length = 0 Then t = tb.Text ' Proceed only if there is something to be copied. If t.Length > 0 Then Clipboard.SetDataObject(t) End If End Sub

Tip 6: Reverse For Each Loop
Here's a For Each loop that iterates in reverse order, over any IEnumerable class. The snippet code is a complete class that's simple to use. Be sure to check out the original code listing for an example.

Class ReverseIterator Implements IEnumerable ' a low-overhead ArrayList to store references Dim items As New ArrayList() Sub New(ByVal collection As IEnumerable) ' load all the items in the ArrayList, but in ' reverse order Dim o As Object For Each o In collection items.Insert(0, o) Next End Sub Public Function GetEnumerator() As _ System.Collections.IEnumerator Implements _ System.Collections.IEnumerable.GetEnumerator ' return the enumerator of the inner ArrayList Return items.GetEnumerator() End Function End Class



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap