GetFields Operator - vb.net

I'm using a function to get the field data from a list. I need to know if the .GetFields operator will just return the fields, or if it will actually populate them with the data stored there? I think it's the latter after reading on msdn but I have no clue and I've never used these "test methods" before :(. Any help is appreciated! (or even if you can tell me how to do a test method would help!)
Here is code:
''' <summary>
''' This function will return all of the fields for a certain class as well as the data stored in them
''' </summary>
''' <param name="list"></param>
''' <returns></returns>
Public Shared Function GetFieldData(ByVal list As IList(Of Object)) As FieldInfo()
Dim fields() As FieldInfo = list.Single.GetType().GetFields()
Return fields
End Function
End Class
Here is code for creating newitems
''' <summary>
''' This function will create new Before and After objects
''' everything should be passed in as a IEnum
''' </summary>
''' <param name="Before"></param>
''' <param name="After"></param>
''' <returns></returns>
Function NewItem(Before As IEnumerable(Of Object), After As IEnumerable(Of Object))
If (Not ObjectsAreSameClass(Before, After)) Then 'If object classes are not the same, send an error message, else continue
'Pop error
Else
Dim BeforeFields() As FieldInfo = GetFieldData(Before)
Dim AfterFields() As FieldInfo = GetFieldData(After)
ObjectCounter += 1
'Now check and make sure the objects are not the same
If (BeforeFields.Equals(AfterFields)) Then
'Objects are the same so pop error?
End If
End If
Return Nothing
End Function

FieldInfo is information about the field, not including its value. To get the value you have to provide an instance of that object type. Here's an example that you can place on a form to see how it works:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim info() As FieldInfo = GetType(Form).GetFields(BindingFlags.NonPublic Or
BindingFlags.Instance)
For Each item As FieldInfo In info
Dim value As Object = item.GetValue(Me) ' Get the value from 'Me'
If Not IsNothing(value) Then
Debug.Print("{0} = {1}", item.Name, value.ToString())
Else
Debug.Print("{0} = Nothing", item.Name)
End If
Next
End Sub

Related

Adding a progress bar to show how much percentage of form had been loaded

I've a form having certain lines of code on the form_load event and thus it takes some time to be shown. I want to add a progress bar to show how much percentage of form has been loaded. How can I do so ..?
In short:- Like while browsing the web pages the progress as shown in web browser indicate the value of page has been loaded. I want to do so in vb.net.
How can I do so .?
You'll need to move your long running code into the form's .Shown event handler.
I would suggest adding a StatusStrip to your form and add a ToolStripProgressBar to that. I did this in a program I wrote and used the following bit of code work on it.
It's all pretty self explanatory :-)
''' <summary>
''' Sets minimum and maximum values for ToolStripProgressBar and shows it
''' </summary>
''' <param name="min">Minimum Value</param>
''' <param name="max">Maximum Value</param>
''' <param name="labeltext">Text to show in ToolStripLabel</param>
Private Sub SetAndShowProgressBar(min As Integer, max As Integer, labeltext As String)
ToolStripStatusLabel1.Text = labeltext
ToolStripProgressBar1.Minimum = min
ToolStripProgressBar1.Maximum = max + 5
ToolStripProgressBar1.Value = 1
ToolStripProgressBar1.Visible = True
StatusStrip1.Update()
End Sub
''' <summary>
''' Hides ToolStripProgressBar
''' </summary>
Private Sub HideProgressBar()
ToolStripStatusLabel1.Text = ""
ToolStripProgressBar1.Visible = False
StatusStrip1.Update()
End Sub
Private Delegate Sub incprogressbarDelegate()
Private Sub IncrementProgressBar()
If Me.InvokeRequired Then
Dim d As New incprogressbarDelegate(AddressOf IncrementProgressBar)
Me.Invoke(d, New Object())
Else
Me.ToolStripProgressBar1.Value += 1
StatusStrip1.Update()
End If
End Sub
Private Delegate Sub SetProgressBarDelegate(i As Integer)
Private Sub SetProgressBarValue(i As Integer)
If Me.InvokeRequired Then
Dim d As New SetProgressBarDelegate(AddressOf SetProgressBarValue)
Me.Invoke(d, New Object() {i})
Else
Me.ToolStripProgressBar1.Value = i
End If
End Sub

save variables to text file in vb.net

As the title says I would like to save variables in a .txt file or something similar. (not XML)I've tried My.Settings and I'd like a more portable solution.
Format being something like this:
[config]<br>
StartPath=C:\Users<br>
FormColour=Black
From your question it looks you are thinking about sort of configuration (or similar) file looking like .ini file in older Windows versions. Such a file has one or more [sections] with multiple name=value pairs in each. If this is your case, please take a look here:
Reading/writing an INI file
If you are going to store data which suit multi-level structure better than simple flat structure you show in the sample and you do not like XML for this purpose, you might consider for example JSON (available natively in .NET since version 3.5) or YAML. Their libraries are freely available for .NET.
For a portable solution you can use my INI File Manager which is really easy and secure to use.
#Region " INI File Manager " ' By Elektro
Public Class INIFileManager
#Region " Members "
#Region " Properties "
''' <summary>
''' Indicates the initialization file path.
''' </summary>
Public Shared Property FilePath As String =
IO.Path.Combine(Application.StartupPath, Process.GetCurrentProcess().ProcessName & ".ini")
#End Region
#Region " Variables "
''' <summary>
''' Stores the initialization file content.
''' </summary>
Private Shared Content As New List(Of String)
''' <summary>
''' Stores the INI section names.
''' </summary>
Private Shared SectionNames As String() = {String.Empty}
''' <summary>
''' Indicates the start element index of a section name.
''' </summary>
Private Shared SectionStartIndex As Integer = -1
''' <summary>
''' Indicates the end element index of a section name.
''' </summary>
Private Shared SectionEndIndex As Integer = -1
''' <summary>
''' Stores a single sorted section block with their keys and values.
''' </summary>
Private Shared SortedSection As New List(Of String)
''' <summary>
''' Stores all the sorted section blocks with their keys and values.
''' </summary>
Private Shared SortedSections As New List(Of String)
''' <summary>
''' Indicates the INI element index that contains the Key and value.
''' </summary>
Private Shared KeyIndex As Integer = -1
''' <summary>
''' Indicates the culture to compare the strings.
''' </summary>
Private Shared ReadOnly CompareMode As StringComparison = StringComparison.InvariantCultureIgnoreCase
#End Region
#Region " Exceptions "
''' <summary>
''' Exception is thrown when a section name parameter has invalid format.
''' </summary>
Private Class SectionNameInvalidFormatException
Inherits Exception
Public Sub New()
MyBase.New("Section name parameter has invalid format." &
Environment.NewLine &
"The rigth syntax is: [SectionName]")
End Sub
Public Sub New(message As String)
MyBase.New(message)
End Sub
Public Sub New(message As String, inner As Exception)
MyBase.New(message, inner)
End Sub
End Class
#End Region
#End Region
#Region " Methods "
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub Equals()
End Sub
Public Class [File]
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub Equals()
End Sub
''' <summary>
''' Checks whether the initialization file exist.
''' </summary>
''' <returns>True if initialization file exist, otherwise False.</returns>
Public Shared Function Exist() As Boolean
Return IO.File.Exists(FilePath)
End Function
''' <summary>
''' Creates the initialization file.
''' If the file already exist it would be replaced.
''' </summary>
''' <param name="Encoding">The Text encoding to write the initialization file.</param>
''' <returns>True if the operation success, otherwise False.</returns>
Public Shared Function Create(Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
Try
IO.File.WriteAllText(FilePath,
String.Empty,
If(Encoding Is Nothing, System.Text.Encoding.Default, Encoding))
Catch ex As Exception
Throw
Return False
End Try
Return True
End Function
''' <summary>
''' Deletes the initialization file.
''' </summary>
''' <returns>True if the operation success, otherwise False.</returns>
Public Shared Function Delete() As Boolean
If Not [File].Exist Then Return False
Try
IO.File.Delete(FilePath)
Catch ex As Exception
Throw
Return False
End Try
Content = Nothing
Return True
End Function
''' <summary>
''' Returns the initialization file content.
''' </summary>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
Public Shared Function [Get](Optional ByVal Encoding As System.Text.Encoding = Nothing) As List(Of String)
Content = IO.File.ReadAllLines(FilePath,
If(Encoding Is Nothing, System.Text.Encoding.Default, Encoding)).ToList()
Return Content
End Function
''' <summary>
''' Sort the initialization file content by the Key names.
''' If the initialization file contains sections then the sections are sorted by their names also.
''' </summary>
''' <param name="RemoveEmptyLines">Remove empty lines.</param>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
''' <returns>True if the operation success, otherwise False.</returns>
Public Shared Function Sort(Optional ByVal RemoveEmptyLines As Boolean = False,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then Return False
[File].[Get](Encoding)
Select Case Section.Has(Encoding)
Case True ' initialization file contains at least one Section.
SortedSection.Clear()
SortedSections.Clear()
Section.GetNames(Encoding) ' Get the (sorted) section names
For Each name As String In SectionNames
SortedSection = Section.[Get](name, Encoding) ' Get the single section lines.
If RemoveEmptyLines Then ' Remove empty lines.
SortedSection = SortedSection.Where(Function(line) _
Not String.IsNullOrEmpty(line) AndAlso
Not String.IsNullOrWhiteSpace(line)).ToList
End If
SortedSection.Sort() ' Sort the single section keys.
SortedSections.Add(name) ' Add the section name to the sorted sections list.
SortedSections.AddRange(SortedSection) ' Add the single section to the sorted sections list.
Next name
Content = SortedSections
Case False ' initialization file doesn't contains any Section.
Content.Sort()
If RemoveEmptyLines Then
Content = Content.Where(Function(line) _
Not String.IsNullOrEmpty(line) AndAlso
Not String.IsNullOrWhiteSpace(line)).ToList
End If
End Select ' Section.Has()
' Save changes.
Return [File].Write(Content, Encoding)
End Function
''' <summary>
''' Writes a new initialization file with the specified text content..
''' </summary>
''' <param name="Content">Indicates the text content to write in the initialization file.</param>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
''' <returns>True if the operation success, otherwise False.</returns>
Public Shared Function Write(ByVal Content As List(Of String),
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
Try
IO.File.WriteAllLines(FilePath,
Content,
If(Encoding Is Nothing, System.Text.Encoding.Default, Encoding))
Catch ex As Exception
Throw
Return False
End Try
Return True
End Function
End Class
Public Class [Key]
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub Equals()
End Sub
''' <summary>
''' Return a value indicating whether a key name exist or not.
''' </summary>
''' <param name="KeyName">Indicates the key name that contains the value to modify.</param>
''' <param name="SectionName">Indicates the Section name where to find the key name.</param>
''' <param name="Encoding">The Text encoding to write the initialization file.</param>
''' <returns>True if the key name exist, otherwise False.</returns>
Public Shared Function Exist(ByVal KeyName As String,
Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then Return False
[File].[Get](Encoding)
[Key].GetIndex(KeyName, SectionName)
Select Case SectionName Is Nothing
Case True
Return Convert.ToBoolean(Not KeyIndex)
Case Else
Return Convert.ToBoolean(Not (KeyIndex + SectionStartIndex))
End Select
End Function
''' <summary>
''' Set the value of an existing key name.
'''
''' If the initialization file doesn't exists, or else the Key doesn't exist,
''' or else the Section parameter is not specified and the key name doesn't exist;
''' then the 'key=value' is appended to the end of the initialization file.
'''
''' if the specified Section name exist but the Key name doesn't exist,
''' then the 'key=value' is appended to the end of the Section.
'''
''' </summary>
''' <param name="KeyName">Indicates the key name that contains the value to modify.</param>
''' <param name="Value">Indicates the new value.</param>
''' <param name="SectionName">Indicates the Section name where to find the key name.</param>
Public Shared Function [Set](ByVal KeyName As String,
ByVal Value As String,
Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then [File].Create()
[File].[Get](Encoding)
[Key].GetIndex(KeyName, SectionName)
' If KeyName is not found and indicated Section is found, then...
If KeyIndex = -1 AndAlso SectionEndIndex <> -1 Then
' If section EndIndex is the last line of file, then...
If SectionEndIndex = Content.Count Then
Content(Content.Count - 1) = Content(Content.Count - 1) &
Environment.NewLine &
String.Format("{0}={1}", KeyName, Value)
Else ' If not section EndIndex is the last line of file, then...
Content(SectionEndIndex) = String.Format("{0}={1}", KeyName, Value) &
Environment.NewLine &
Content(SectionEndIndex)
End If
' If KeyName is found then...
ElseIf KeyIndex <> -1 Then
Content(KeyIndex) = String.Format("{0}={1}", KeyName, Value)
' If KeyName is not found and Section parameter is passed. then...
ElseIf KeyIndex = -1 AndAlso SectionName IsNot Nothing Then
Content.Add(SectionName)
Content.Add(String.Format("{0}={1}", KeyName, Value))
' If KeyName is not found, then...
ElseIf KeyIndex = -1 Then
Content.Add(String.Format("{0}={1}", KeyName, Value))
End If
' Save changes.
Return [File].Write(Content, Encoding)
End Function
''' <summary>
''' Get the value of an existing key name.
''' If the initialization file or else the Key doesn't exist then a 'Nothing' object is returned.
''' </summary>
''' <param name="KeyName">Indicates the key name to retrieve their value.</param>
''' <param name="DefaultValue">Indicates a default value to return if the key name is not found.</param>
''' <param name="SectionName">Indicates the Section name where to find the key name.</param>
Public Shared Function [Get](ByVal KeyName As String,
Optional ByVal DefaultValue As Object = Nothing,
Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Object
If Not [File].Exist() Then Return DefaultValue
[File].[Get](Encoding)
[Key].GetIndex(KeyName, SectionName)
Select Case KeyIndex
Case Is <> -1 ' KeyName found.
Return Content(KeyIndex).Substring(Content(KeyIndex).IndexOf("=") + 1)
Case Else ' KeyName not found.
Return DefaultValue
End Select
End Function
''' <summary>
''' Returns the initialization file line index of the key name.
''' </summary>
''' <param name="KeyName">Indicates the Key name to retrieve their value.</param>
''' <param name="SectionName">Indicates the Section name where to find the key name.</param>
Private Shared Sub GetIndex(ByVal KeyName As String,
Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing)
If Content Is Nothing Then [File].Get(Encoding)
' Reset the INI index elements to negative values.
KeyIndex = -1
SectionStartIndex = -1
SectionEndIndex = -1
If SectionName IsNot Nothing AndAlso Not SectionName Like "[[]?*[]]" Then
Throw New SectionNameInvalidFormatException
Exit Sub
End If
' Locate the KeyName and set their element index.
' If the KeyName is not found then the value is set to "-1" to return an specified default value.
Select Case String.IsNullOrEmpty(SectionName)
Case True ' Any SectionName parameter is specified.
KeyIndex = Content.FindIndex(Function(line) line.StartsWith(String.Format("{0}=", KeyName),
StringComparison.InvariantCultureIgnoreCase))
Case False ' SectionName parameter is specified.
Select Case Section.Has(Encoding)
Case True ' INI contains at least one Section.
SectionStartIndex = Content.FindIndex(Function(line) line.Trim.Equals(SectionName.Trim, CompareMode))
If SectionStartIndex = -1 Then ' Section doesn't exist.
Exit Sub
End If
SectionEndIndex = Content.FindIndex(SectionStartIndex + 1, Function(line) line.Trim Like "[[]?*[]]")
If SectionEndIndex = -1 Then
' This fixes the value if the section is at the end of file.
SectionEndIndex = Content.Count
End If
KeyIndex = Content.FindIndex(SectionStartIndex, SectionEndIndex - SectionStartIndex,
Function(line) line.StartsWith(String.Format("{0}=", KeyName),
StringComparison.InvariantCultureIgnoreCase))
Case False ' INI doesn't contains Sections.
GetIndex(KeyName, , Encoding)
End Select ' Section.Has()
End Select ' String.IsNullOrEmpty(SectionName)
End Sub
''' <summary>
''' Remove an existing key name.
''' </summary>
''' <param name="KeyName">Indicates the key name to retrieve their value.</param>
''' <param name="SectionName">Indicates the Section name where to find the key name.</param>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
Public Shared Function Remove(ByVal KeyName As String,
Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then Return False
[File].[Get](Encoding)
[Key].GetIndex(KeyName, SectionName)
Select Case KeyIndex
Case Is <> -1 ' Key found.
' Remove the element containing the key name.
Content.RemoveAt(KeyIndex)
' Save changes.
Return [File].Write(Content, Encoding)
Case Else ' KeyName not found.
Return False
End Select
End Function
End Class
Public Class Section
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Private Shadows Sub Equals()
End Sub
''' <summary>
''' Adds a new section at bottom of the initialization file.
''' </summary>
''' <param name="SectionName">Indicates the Section name to add.</param>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
Public Shared Function Add(Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then [File].Create()
If Not SectionName Like "[[]?*[]]" Then
Throw New SectionNameInvalidFormatException
Exit Function
End If
[File].[Get](Encoding)
Select Case Section.GetNames(Encoding).Where(Function(line) line.Trim.Equals(SectionName.Trim, CompareMode)).Any
Case False ' Any of the existing Section names is equal to given section name.
' Add the new section name.
Content.Add(SectionName)
' Save changes.
Return [File].Write(Content, Encoding)
Case Else ' An existing Section name is equal to given section name.
Return False
End Select
End Function
''' <summary>
''' Returns all the keys and values of an existing Section Name.
''' </summary>
''' <param name="SectionName">Indicates the section name where to retrieve their keynames and values.</param>
Public Shared Function [Get](ByVal SectionName As String,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As List(Of String)
If Content Is Nothing Then [File].Get(Encoding)
SectionStartIndex = Content.FindIndex(Function(line) line.Trim.Equals(SectionName.Trim, CompareMode))
SectionEndIndex = Content.FindIndex(SectionStartIndex + 1, Function(line) line.Trim Like "[[]?*[]]")
If SectionEndIndex = -1 Then
SectionEndIndex = Content.Count ' This fixes the value if the section is at the end of file.
End If
Return Content.GetRange(SectionStartIndex, SectionEndIndex - SectionStartIndex).Skip(1).ToList
End Function
''' <summary>
''' Returns all the section names of the initialization file.
''' </summary>
Public Shared Function GetNames(Optional ByVal Encoding As System.Text.Encoding = Nothing) As String()
If Content Is Nothing Then [File].Get(Encoding)
' Get the Section names.
SectionNames = (From line In Content Where line.Trim Like "[[]?*[]]").ToArray
' Sort the Section names.
If SectionNames.Count <> 0 Then Array.Sort(SectionNames)
' Return the Section names.
Return SectionNames
End Function
''' <summary>
''' Gets a value indicating whether the initialization file contains at least one Section.
''' </summary>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
Public Shared Function Has(Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Content Is Nothing Then [File].Get(Encoding)
Return (From line In Content Where line.Trim Like "[[]?*[]]").Any()
End Function
''' <summary>
''' Removes an existing section with all of it's keys and values.
''' </summary>
''' <param name="SectionName">Indicates the Section name to remove with all of it's key/values.</param>
''' <param name="Encoding">The Text encoding to read the initialization file.</param>
Public Shared Function Remove(Optional ByVal SectionName As String = Nothing,
Optional ByVal Encoding As System.Text.Encoding = Nothing) As Boolean
If Not [File].Exist() Then Return False
If Not SectionName Like "[[]?*[]]" Then
Throw New SectionNameInvalidFormatException
Exit Function
End If
[File].[Get](Encoding)
Select Case [Section].GetNames(Encoding).Where(Function(line) line.Trim.Equals(SectionName.Trim, CompareMode)).Any
Case True ' An existing Section name is equal to given section name.
' Get the section StartIndex and EndIndex.
[Get](SectionName)
' Remove the section range index.
Content.RemoveRange(SectionStartIndex, SectionEndIndex - SectionStartIndex)
' Save changes.
Return [File].Write(Content, Encoding)
Case Else ' Any of the existing Section names is equal to given section name.
Return False
End Select
End Function
End Class
#End Region
End Class
#End Region
Here are some usage examples:
' Set the initialization file path.
'INIFileManager.FilePath = IO.Path.Combine(Application.StartupPath, "Config.ini")
' Create the initialization file.
'INIFileManager.File.Create()
' Check that the initialization file exist.
'MsgBox(INIFileManager.File.Exist)
' Writes a new entire initialization file with the specified text content.
'INIFileManager.File.Write(New List(Of String) From {"[Section Name 1]"})
' Set an existing value or append it at the enf of the initialization file.
'INIFileManager.Key.Set("KeyName1", "Value1")
' Set an existing value on a specific section or append them at the enf of the initialization file.
'INIFileManager.Key.Set("KeyName2", "Value2", "[Section Name 2]")
' Gets the value of the specified Key name,
'MsgBox(INIFileManager.Key.Get("KeyName1"))
' Gets the value of the specified Key name on the specified Section.
'MsgBox(INIFileManager.Key.Get("KeyName2", , "[Section Name 2]"))
' Gets the value of the specified Key name and returns a default value if the key name is not found.
'MsgBox(INIFileManager.Key.Get("KeyName0", "I'm a default value"))
' Gets the value of the specified Key name, and assign it to a control property.
'CheckBox1.Checked = CType(INIFileManager.Key.Get("KeyName1"), Boolean)
' Checks whether a Key exists.
'MsgBox(INIFileManager.Key.Exist("KeyName1"))
' Checks whether a Key exists on a specific section.
'MsgBox(INIFileManager.Key.Exist("KeyName2", "[First Section]"))
' Remove a key name.
'INIFileManager.Key.Remove("KeyName1")
' Remove a key name on the specified Section.
'INIFileManager.Key.Remove("KeyName2", "[Section Name 2]")
' Add a new section.
'INIFileManager.Section.Add("[Section Name 3]")
' Get the contents of a specific section.
'MsgBox(String.Join(Environment.NewLine, INIFileManager.Section.Get("[Section Name 1]")))
' Remove an existing section.
'INIFileManager.Section.Remove("[Section Name 2]")
'' Checks that the initialization file contains at least one section.
'MsgBox(INIFileManager.Section.Has())
'' Sort the initialization file (And remove empty lines).
'INIFileManager.File.Sort(True)
'' Gets the initialization file section names.
'MsgBox(String.Join(", ", INIFileManager.Section.GetNames()))
'' Gets the initialization file content.
'MsgBox(String.Join(Environment.NewLine, INIFileManager.File.Get()))
'' Delete the initialization file from disk.
'INIFileManager.File.Delete()
I suggest using an app.config instead. Using ConfigurationManager class, you can easily read/write multiple sections with little to no code. INI is going away as means of configuration, XML will probably stay for some time, I'd say stick to that. I recently wrote an article showing simple usage:
VB.NET - Reading and writing app.config # neolisk.com
Relevant parts are included here for convenience:
Imports System.Configuration
Imports System.Reflection
Public Class AppConfig
Private _config As Configuration
Private _settings As AppSettingsSection
Public Function GetProperty(propertyName As String) As String
Return _settings.Settings.Item(propertyName).Value
End Function
Public Sub SetProperty(propertyName As String, propertyValue As String)
_settings.Settings.Item(propertyName).Value = propertyValue
End Sub
Public Sub New()
_config = ConfigurationManager.OpenExeConfiguration(
Assembly.GetEntryAssembly().Location)
_settings = _config.AppSettings
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
_config.Save(ConfigurationSaveMode.Modified)
End Sub
End Class
Usage:
Dim _appConfig As New AppConfig
_appConfig.SetProperty(propertyName, propertyValue)
_appConfig.GetProperty(propertyName)
You will need to extend the above class to support multiple sections.

Download text file and set components?

How to make vb download a text file and then read all the lines to see what components are available and then set them on form? Text file would contain something like this:
[1] = number of component, pyc_file as the component name to display to user. and last is a link from where the file is downloaded.
The pyc_file should be displayed next on a checkbox.
[1];pyc_file;www.mediafire.com/abcd1234/_init_.pyc;
I don't know how to explain it very well. I hope you understand!
You can create your own type to parse the parts like this:
Public Class Form1
Dim LineToParse As String =
"[1];pyc_file;www.mediafire.com/abcd1234/_init_.pyc;"
Private Sub Test() Handles MyBase.Shown
Dim Comp As Component = Me.GetComponent(LineToParse)
Dim sb As New System.Text.StringBuilder
sb.AppendFormat("Line: {0}", LineToParse)
sb.AppendLine(Environment.NewLine)
sb.AppendFormat("Index: {0}", CStr(Comp.Index))
sb.AppendLine()
sb.AppendFormat("Name: {0}", Comp.Name)
sb.AppendLine()
sb.AppendFormat("Url: {0}", Comp.URL)
MessageBox.Show(sb.ToString, "Component information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
''' <summary>
''' Parses a comma-delimited component text-line and returns a Component Object.
''' </summary>
''' <param name="ComponentLine">Indicates the component line to parse.</param>
''' <returns>Component.</returns>
Friend Function GetComponent(ByVal ComponentLine As String) As Component
Dim ComponentParts As IEnumerable(Of String) =
ComponentLine.Split(";"c)
Dim Index As Integer =
Integer.Parse(ComponentParts(0).
Replace("[", String.Empty).Replace("]", String.Empty))
Dim Name As String =
ComponentParts(1)
Dim URL As Uri =
New Uri(If(Not ComponentParts(2).StartsWith("http://", StringComparison.OrdinalIgnoreCase),
String.Format("http://{0}", ComponentParts(2)),
ComponentParts(2)))
Return New Component With
{
.Index = Index,
.Name = Name,
.URL = URL
}
End Function
''' <summary>
''' A component.
''' </summary>
Friend NotInheritable Class Component
''' <summary>
''' Indicates the number of component.
''' </summary>
Public Property Index As Integer
''' <summary>
''' Indicates the component name to display to user.
''' </summary>
Public Property Name As String
''' <summary>
''' Indicates a link from where the file is downloaded.
''' </summary>
Public Property URL As Uri
End Class
End Class
I have solved it now :-)
Wasn't actually that hard.

Asynchronous Progress bar updating

This is a vb.net mvc 3 application.. I am new to asynchronous and threading combined so this is a bit over my head on 2 levels... I have a long running process that sends mass emails at set intervals to avoid terms of use violations.. Not only for this task but for other options I would like to add a progress bar that is updated through java..I have found a blog post about doing something like this... I have got the following code but there seems to be an issue where the progress bar is never showing...
Below is my extendedTaskRun Class:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading
Namespace ExtendedTaskHandler
''' <summary>
''' Long Running Test Class.
''' </summary>
Public Class extendedTaskRun
Private Shared syncRoot As New Object()
''' <summary>
''' Gets or sets the process status.
''' </summary>
''' <value>The process status.</value>
Private Shared Property ProcessStatus() As IDictionary(Of String, Integer)
Get
Return m_ProcessStatus
End Get
Set(value As IDictionary(Of String, Integer))
m_ProcessStatus = value
End Set
End Property
Private Shared m_ProcessStatus As IDictionary(Of String, Integer)
''' <summary>
''' Initializes a new instance of the <see cref="extendedTaskRun"/> class.
''' </summary>
Public Sub New()
If ProcessStatus Is Nothing Then
ProcessStatus = New Dictionary(Of String, Integer)()
End If
End Sub
Public Sub SetStatus(ByVal id As String, ByVal value As Integer)
SyncLock syncRoot
ProcessStatus(id) = value
End SyncLock
End Sub
''' <summary>
''' Processes the long running action.
''' </summary>
''' <param name="id">The id.</param>
Public Function ProcessLongRunningAction(id As String) As String
For i As Integer = 1 To 100
Thread.Sleep(100)
SyncLock syncRoot
ProcessStatus(id) = i
End SyncLock
Next
Return id
End Function
''' <summary>
''' Adds the specified id.
''' </summary>
''' <param name="id">The id.</param>
Public Sub Add(id As String)
SyncLock syncRoot
ProcessStatus.Add(id, 0)
End SyncLock
End Sub
''' <summary>
''' Removes the specified id.
''' </summary>
''' <param name="id">The id.</param>
Public Sub Remove(id As String)
SyncLock syncRoot
ProcessStatus.Remove(id)
End SyncLock
End Sub
''' <summary>
''' Gets the status.
''' </summary>
''' <param name="id">The id.</param>
Public Function GetStatus(id As String) As Integer
SyncLock syncRoot
If ProcessStatus.Keys.Where(Function(x) x = id).Count = 1 Then
Return ProcessStatus(id)
Else
Return 100
End If
End SyncLock
End Function
End Class
End Namespace
Then my controllers are as follows:
Public Function MassEmailStatus() As ActionResult
MassEmailAddressList = TempData("emailaddresses")
TempData.Clear()
TempData.Add("emailaddresses", MassEmailAddressList)
Return View()
End Function
Public Function MassEmailSendingStatus(ByVal id As String) As ActionResult
Dim d As List(Of String) = TempData("emList")
Dim EmailCount As Integer = d.Count
Dim triedCount As Integer = 0
Dim extendedTaskRun As New extendedTaskRun
extendedTaskRun.Add(id)
Dim percentDone As Integer = 0
While Not (triedCount = EmailCount)
For Each em In d
EmailSender(em, String.Empty)
triedCount += 1
percentDone = EmailCount / 100 + triedCount
extendedTaskRun.SetStatus(id, percentDone)
Next
End While
extendedTaskRun.Remove(id)
Return View()
End Function
Then the MassEmailStatus view is as follows:
#Code
ViewData("Title") = "MassEmailSendingStatus"
TempData.Add("emList", TempData("emailaddresses"))
end Code
<div>
Start Long Running Process
</div>
<br />
<div id="statusBorder">
<div id="statusFill">
</div>
</div>
<script type="text/javascript">
var uniqueId = '#Guid.NewGuid().ToString()';
var tdata = '#TempData("emailaddresses")';
$(document).ready(function (event) {
$('#startProcess').click(function () {
$.post("MassEmailSendingStatus", { id: uniqueId }, function () {
$('#statusBorder').show();
getStatus();
});
event.preventDefault;
});
});
function getStatus() {
var url = 'Admin/GetCurrentProgress/' + uniqueId;
$.get(url, function (data) {
if (data != "100") {
$('#status').html(data);
$('#statusFill').width(data);
window.setTimeout("getStatus()", 100);
}
else {
$('#status').html("Done");
$('#statusBorder').hide();
alert("The Long process has finished");
};
});
}
</script>
These are the additional functions that the blog mentioned and are in my code but from looking at the code I know they cannot be correctly wired up.
Private Delegate Function ProcessTask(id As String) As String
Private extendedRunClass As New extendedTaskRun
''' <summary>
''' Starts the long running process.
''' </summary>
''' <param name="id">The id.</param>
Public Sub StartLongRunningProcess(id As String)
extendedRunClass.Add(id)
Dim processTask As New ProcessTask(AddressOf extendedRunClass.ProcessLongRunningAction)
processTask.BeginInvoke(id, New AsyncCallback(AddressOf EndLongRunningProcess), processTask)
End Sub
''' <summary>
''' Ends the long running process.
''' </summary>
''' <param name="result">The result.</param>
Public Sub EndLongRunningProcess(result As IAsyncResult)
Dim processTask As ProcessTask = DirectCast(result.AsyncState, ProcessTask)
Dim id As String = processTask.EndInvoke(result)
extendedRunClass.Remove(id)
End Sub
''' <summary>
''' Gets the current progress.
''' </summary>
''' <param name="id">The id.</param>
Public Function GetCurrentProgress(id As String) As ContentResult
Me.ControllerContext.HttpContext.Response.AddHeader("cache-control", "no-cache")
Dim currentProgress = extendedRunClass.GetStatus(id).ToString()
Return Content(currentProgress)
End Function
I do know that Im not actually starting the process as im just calling EmailSender(em, String.Empty) which is where the work occurs, inside the for each loop of the MassEmailSendingStatus controller... What must I do to fix this correctly?
WOW my code was whacked...After taking the blog code and throwing it in a quick project of its own and starting it i was able to watch what was happening.. I have it working now... I will update this with the working solution in a bit... Working on returning more than just percentage to the progress bar now so messages are displayed as its working.. Not sure how im going to do that but im thinking I will do it inside the getCurrentProgress method as a string return from the extendedTaskRun class...

ConnectionString In Class Library

I'm merging two of my projects that can share a lot of the same classes into one solution with two web applications and a shared class library.
I've literally just dumped all the classes into the class library project and as expected I've got a ton of errors to fix. My main problem at the moment is the connection string. Currently I have this (which is obviously not going to work):
''' <summary>
''' Initialise the data access layer by loading the database connection string from the Web.Config file
''' </summary>
''' <remarks></remarks>
Shared Sub New()
_connectionString = WebConfigurationManager.ConnectionStrings("ClientFamilyManagementConnectionString").ConnectionString
End
What do I do with my connection strings now the classes are not in a web application?
I really feel like I'm missing something here so below I'm include an example of a class from the BLL and DAL. I can't pass in the connection string to the constructor of the DAL - it says it can't have any parameters.
BLL:
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Namespace CompanyName
<Serializable()> Public Class SubMarketSector
Private _id As Integer
Private _name As String
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New()
End Sub
Public Shared Function GetAllSubMarketSectors() As List(Of CompanyName.SubMarketSector)
Dim newSubMarketSectorDAO As New SubMarketSectorDAO
Return newSubMarketSectorDAO.GetAllSubMarketSectors
End Function
Public Shared Function GetSubMarketSectorByID(ByVal subMarketSectorID As Integer) As CompanyName.SubMarketSector
Dim newSubMarketSectorDAO As New SubMarketSectorDAO
Return newSubMarketSectorDAO.GetSubMarketSectorByID(subMarketSectorID)
End Function
End Class
End Namespace
DAL:
Namespace CompanyName
Public Class SubMarketSectorDAO
Private Const MainSelectByStatement As String = "SELECT ID, Name FROM Sub_Market_Sectors"
Private Const MainOrderByStatement As String = " ORDER BY Name"
Private Shared ReadOnly _connectionString As String = String.Empty
''' <summary>
''' Initialise the data access layer by loading the database connection string from the Web.Config file
''' </summary>
''' <remarks></remarks>
Shared Sub New()
_connectionString = WebConfigurationManager.ConnectionStrings("PursuitsConnectionString").ConnectionString
End Sub
''' <summary>
''' Returns a List of all Sub Market Sectors
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetAllSubMarketSectors() As List(Of CompanyName.SubMarketSector)
' Create the Connection
Dim currentConnection As SqlConnection = New SqlConnection(_connectionString)
' Create the Command Object, set the CommandText, add any required Parameters and set the Connection
Dim currentCommand As New SqlCommand
currentCommand.CommandText = MainSelectByStatement & MainOrderByStatement
currentCommand.Connection = currentConnection
Dim listOfSubMarketSectors As New List(Of CompanyName.SubMarketSector)
Using currentConnection
' Open the Connection
currentConnection.Open()
' Create the DataReader and Execute the Command
Dim currentDataReader As SqlDataReader = currentCommand.ExecuteReader()
' Populate the list with data
Do While currentDataReader.Read
Dim newSubMarketSector As CompanyName.SubMarketSector = PopulateSubMarketSector(currentDataReader)
listOfSubMarketSectors.Add(newSubMarketSector)
Loop
End Using
Return listOfSubMarketSectors
End Function
''' <summary>
''' Return a single Sub Market Sector
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetSubMarketSectorByID(ByVal subMarketSectorID As Integer) As CompanyName.SubMarketSector
' Create the Connection
Dim currentConnection As SqlConnection = New SqlConnection(_connectionString)
' Create the Command Object, set the CommandText, add any required Parameters and set the Connection
Dim currentCommand As New SqlCommand
currentCommand.CommandText = MainSelectByStatement & " WHERE ID = #subMarketSectorID" & MainOrderByStatement
currentCommand.Parameters.AddWithValue("#subMarketSectorID", subMarketSectorID)
currentCommand.Connection = currentConnection
Dim newSubMarketSector As New CompanyName.SubMarketSector
Using currentConnection
' Open the Connection
currentConnection.Open()
' Create the DataReader and Execute the Command
Dim currentDataReader As SqlDataReader = currentCommand.ExecuteReader()
' Populate the Market Sector
Do While currentDataReader.Read
newSubMarketSector = PopulateSubMarketSector(currentDataReader)
Loop
End Using
Return newSubMarketSector
End Function
Private Function PopulateSubMarketSector(ByVal currentDataReader As SqlDataReader) As CompanyName.SubMarketSector
Dim newSubMarketSector As New CompanyName.SubMarketSector
If Not (currentDataReader.IsDBNull(currentDataReader.GetOrdinal("ID"))) Then
newSubMarketSector.ID = currentDataReader("ID")
End If
If Not (currentDataReader.IsDBNull(currentDataReader.GetOrdinal("Name"))) Then
newSubMarketSector.Name = currentDataReader("Name")
End If
Return newSubMarketSector
End Function
End Class
End Namespace
How about storing the ConnectionStrings in each WebApplication and passing the specific connection string to the class library on instanciation. I don't know how you designed the class library (static methods, singleton, or whatever) but maybe you could pass them (each ConnectionString) to the constructor to set an instance variable which you later use (the same way like before).