Public Class MySingletonCls
Private Shared objSingle As MySingletonCls
Private Shared bIsCreated As Boolean
Private Sub New()
'Override the default constructor
End Sub
Public Shared Function CreateInstance() As MySingletonCls
If bIsCreated = False Then
objSingle = New MySingletonCls()
bIsCreated = True
Return objSingle
Else
Return objSingle
End If
End Function
End Class
Tuesday, May 5, 2009
Singleton class
Friday, May 1, 2009
Tip to fetch XML using LINQ
Function FetchXML(ByVal url As Uri) As XElement
Dim x = WebRequest.Create(url)
Using r = x.GetResponse, rs As New StreamReader(r.GetResponseStream)
Return XElement.Parse(rs.ReadToEnd)
End Using
Call the function with the location of the xml file to get the corresponding XML element, that can be used for needed purpose.
Tip to handle XML based info project
Many times we need to manage the projects that just process specific information. Like if we consider a project that handles information about student(name,rno,course,class). So each student will have this information. To very easy way, if we create an XML file with one of the student. Creating a xml with this info is not very tough job.
Upto this point, half is done. Now we just need to get a class which can carry this information for various purposes and which can be written directly to xml file and also can read data from xml file( easiest way is serialize/deserialize mechanism can be very useful)
Now to get exact class that can be serialized directly to xml can be easily obtained from xsd utility of Visual Studio. just open the Visual Studio Command Prompt, and get the xsd file for your xml file and then convert this file directly to required class file. So the steps that can be needed to get the class are as below:
1. Create xml file say 'MyXml.xml'
2. run xsd MyXml.xml, will result MyXml.xsd
3. run xsd MyXml.xsd /c /l:vb, will result MyXml.vb
[the class can be constructed in various ways by using xsd, for details refer xsd documentation.]
note, in step 2 and 3, if current directory for running the command is same that is containing the MyXml.xml file then use command as such, else specify the full path of the file(s). Also target for the resulting file also can be specified(refer xsd documentation).
now we are with a class for which an object can be easily serialized/deserialized. So create collection of objects to carry all the records, applly any type of manipulations and serialize with XMLSerializer to write it to xml directly without worring about most of the validations as most are handled by serializers.
Upto this point, half is done. Now we just need to get a class which can carry this information for various purposes and which can be written directly to xml file and also can read data from xml file( easiest way is serialize/deserialize mechanism can be very useful)
Now to get exact class that can be serialized directly to xml can be easily obtained from xsd utility of Visual Studio. just open the Visual Studio Command Prompt, and get the xsd file for your xml file and then convert this file directly to required class file. So the steps that can be needed to get the class are as below:
1. Create xml file say 'MyXml.xml'
2. run xsd MyXml.xml, will result MyXml.xsd
3. run xsd MyXml.xsd /c /l:vb, will result MyXml.vb
[the class can be constructed in various ways by using xsd, for details refer xsd documentation.]
note, in step 2 and 3, if current directory for running the command is same that is containing the MyXml.xml file then use command as such, else specify the full path of the file(s). Also target for the resulting file also can be specified(refer xsd documentation).
now we are with a class for which an object can be easily serialized/deserialized. So create collection of objects to carry all the records, applly any type of manipulations and serialize with XMLSerializer to write it to xml directly without worring about most of the validations as most are handled by serializers.
Thursday, April 30, 2009
Protecting Assembly/DLL from illegal access tips
Sometimes we want to protect the access to the various functions of our assembly, I have identified that if the functions of the assembly made as friend and assembly is also made friend assembly, then the results will be as desired. As
A friend assembly is an assembly that is permitted to access another assembly's Friend types and members.
So Its possible to protect illegal access to our secure functionality.
This is one of the solution that can be helpful in some situation.
The info of Friend Assemblies can found at the link http://msdn.microsoft.com/en-us/library/bb384772.aspx
A friend assembly is an assembly that is permitted to access another assembly's Friend types and members.
So Its possible to protect illegal access to our secure functionality.
This is one of the solution that can be helpful in some situation.
The info of Friend Assemblies can found at the link http://msdn.microsoft.com/en-us/library/bb384772.aspx
Wednesday, April 29, 2009
Some Simple LINQ Examples for existing examples
Here are some simple examples to understand better to LINQ
creating an iterator that will give you the letters "a" to "z" as you enumerate it:
Dim letters(0 To 25) As Char
For i As Int32 = 0 To 25
letters(i) = ChrW(&H61 + i)
Next
By Using LINQDim letters = From i In Enumerable.Range(0, 26) _
Select ChrW(&H61 + i)
To read from a stream line-by-line,
search for a substring, and return the first couple of matches.strLine = myStreamReader.ReadLine
Do While strLine IsNot Nothing
If strLine.Contains(value) Then
count += 1
resultList.Add(strLine)
If count >= 3 Then Exit Do
End If
strLine = myStreamReader.ReadLine
Loop
Unfortunately, StreamReader doesn't have an
iterator that returns each line. Without something that is enumerable,
you can't form the basis of a LINQ query, so you're forced to write
your query imperatively. That's not as bad as it might first sound;
Building the query into the imperative procedural block does work,
although it's kind of on the ugly side.
If you have an extension method named Lines that
returns an IEnumerable with each item a line, then you can create a
query like this:Dim result = From line In myStreamReader.Lines _
Where line.Contains(value) _
Take 3
Tuesday, April 28, 2009
Tip for Alphablended area of image in list view
In List View, while displaying the images for the various files/folders, sometimes we face the problem that the alpha blended area of the image is not displayed properly with the default configuration of the list view. So to show the image with the correct display, I've found that if we set the ColorDepth of the image list (to be used to store the images to be shown in list view) to appropriate values, it provides the results as desired. So if smallImgLst and largeImgLst are the two image lists used for list view then if we need two views of the list i.e. Small images(16x16) and Large imges(32x32) then we can set the values as below:
smallImgLst.ColorDepth=ColorDepth.Depth32Bit
largeImgLst.ColorDepth=ColorDepth.Depth32Bit
smallImgLst.ImageSize=New System.Drawing.Size(16,16)
largeImgLst.ImageSize=New System.Drawing.Size(32,32)
If there are some other tips to resolve such kind of issue then comments are invited for discussion.
smallImgLst.ColorDepth=ColorDepth.Depth32Bit
largeImgLst.ColorDepth=ColorDepth.Depth32Bit
smallImgLst.ImageSize=New System.Drawing.Size(16,16)
largeImgLst.ImageSize=New System.Drawing.Size(32,32)
If there are some other tips to resolve such kind of issue then comments are invited for discussion.
Friday, April 24, 2009
Display Image in ComboBox
In case we need to display a list of images in ComboBox we can do it by caring just few things about combobox. Firtsly set the 'DrawMode' of the ComboBox to 'OwnerDrawFixed' if all the images are of same size or set 'OwnerDrawVariable' if images are of variable size. and also for the variable sized images set the width and height during the 'MeasureItem' event of the combobox. If imgList1 contains all the needed images of variable and let number of items in the combobox is equal to number of images in the imagelist and each index of image specifies the corresponding item in combo box, Then I guess the following will be fine to get the list of images in the combobox.
1. Set the property 'DrawMode' to OwnerDrawVariable
2. In the method that implement event 'DrawItem' of the combo box, for the case if index of current item is non empty then then draw the image. i.e.
If e.Index<>-1 Then
e.Graphics.DrawImage(imgList1.images(e.Index),e.Bounds.Left,e.Bounds.Top)
End If
3. In the method that implements the 'MeasureItem' event of the combo box the following can be used
e.ItemHeight =imgList1.ImgaeSize.Height
e.ItemWidth = imgList1.ImageSize.Width
These are the basic settings that are needed to display images in combo box, rest are depends on the requirements.
Drop the lines if you think that can be help better for similar kind of requirements.
1. Set the property 'DrawMode' to OwnerDrawVariable
2. In the method that implement event 'DrawItem' of the combo box, for the case if index of current item is non empty then then draw the image. i.e.
If e.Index<>-1 Then
e.Graphics.DrawImage(imgList1.images(e.Index),e.Bounds.Left,e.Bounds.Top)
End If
3. In the method that implements the 'MeasureItem' event of the combo box the following can be used
e.ItemHeight =imgList1.ImgaeSize.Height
e.ItemWidth = imgList1.ImageSize.Width
These are the basic settings that are needed to display images in combo box, rest are depends on the requirements.
Drop the lines if you think that can be help better for similar kind of requirements.
Subscribe to:
Posts (Atom)