Duplicate Settings In Reference DLL - vb.net

I have a DLL with several properties and a function that generates runs a SSRS report in the background and saves it to a PDF file.
I have a DataTable with all the reports that need to be generated and where they need to be saved.
I want to make each instance of the DLL run in a separate thread. I took a stab at it found the 2nd row in the DataTable overrode the first row.
Here is the Class/DLL Code
Public Class SSRSFunctions
Private Shared _Formated_Parameters As String
Private Shared _Report_Parameters As Dictionary(Of String, String)
Public Property FORMATED_PARAMETERS() As String
Get
Return _Formated_Parameters
End Get
Set(ByVal value As String)
_Formated_Parameters = value
End Set
End Property
Public Sub New()
_Report_Parameters = New Dictionary(Of String, String)
End Sub
Public Function RenderReportToFile() As String
'RenderReportHere
End Function
Public Sub AddParameter(ByVal Name As String, ByVal Value As String)
If _Report_Parameters.ContainsKey(Name) Then
_Report_Parameters.Remove(Name)
_Report_Parameters.Add(Name, Value)
Else
_Report_Parameters.Add(Name, Value)
End If
End Sub
End Class
Here is the calling Code
Private Sub CheckForNewRequests()
'Filter DataTable for Reports to Run
For Each dr As DataRow in DateTable.Rows
Dim rpt As New SSRSFunctions
Dim t1 As New Threading.Thread(AddressOf StartNewThread)
rpt.FORMATED_PARAMETERS = (dr("REPORT_PARAMS"))
t1.Start(rpt)
Next
End Sub
Private Function StartNewThread(ByVal report As SSRSFunctions) As String
Return report.RenderReportToFile()
End Function
I am trying to figure out why the "Dim rpt As New SSRSFunctions" is not creating a new instance of the DLL and so the second row of the dataTable has a new instance to store it's parameters.
The second row is overriding the first.
Help?
Thanks
jlimited

Dont make the private properties shared, remove the Shared keyword from the declarations.
change
Private Shared _Formated_Parameters As String
Private Shared _Report_Parameters As Dictionary(Of String, String)
to
Private _Formated_Parameters As String
Private _Report_Parameters As Dictionary(Of String, String)
By sharing them you are saying that no matter how many instances of the class is created always use (share) the same instance of the shared internal variable.

Related

Casting substrings with linq into a list of object and than sorting it base on property in vb.net

This have to be in vb.net linq, i'm pretty sure I could do it in c#, but I cant find any good enough translator to help me ... even the answers I find here in SO seems to only be written in linq, hence the question which might be a duplicate of a c# one.
That being said, considering these 2 classes :
Public class User
Public Property Name() As String
Public Property Teams As TeamList
Public sub New(d as string, results as TeamList)
me.name = d
me.Teams = results
end sub
end class
Public Class TeamList
Public Property TeamName() As String
Public Property fullscore() As list(of object)
Public sub New(name as string, value as list(of string))
me.TeamName = name
me.fullscore = value
me.fullscore = getFullScore(value) (return a list of object)
end sub
End Class
I'm struggling in the final steps of my linq -to - object : (you can copy /paste this in linqpad)
Sub Main
dim Definition as new Dictionary(of String, object)
definition.add("user1_redTeam-02", new object)
definition.add("user1_redTeam-01", new object)
definition.add("user1_blueTeam-03", new object)
definition.add("user2_redTeam-01", new object)
definition.add("user1_redTeam-03", new object)
definition.add("user1_blueTeam-01",new object)
definition.add("user2_blueTeam-01", new object)
definition.add("user1_blueTeam-02", new object)
definition.add("user2_redTeam-02", new object)
Dim q3 = (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
Select New With _
{.UserName = userlists.Key, _
.animationList = (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
Select New With {.Team = scList.Key, _
.Score = scList.ToList()})})
q3.dump()
End Sub
this is the result :
now, all I want is to sort the .score attribute (just a simple .sort(), and instead of returning an anonymous q3 object, which I,m cluless to transform, I'd like the q3 to be a list(of User)
it think it should looks like this ... but I cant make it works, i always gets some linq conversion errors :
Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Linq.IGrouping2[System.String,System.String],UserQuery+User]' to type 'System.Collections.Generic.List`1[UserQuery+User]'.
Dim q3 as List(of User)= (From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_")(0)) _
Select New User(userlists.Key, (From scList In userlists.GroupBy(Of String)(Function(s) s.Split("-")(0)) _
Select New TeamList(scList.Key, scList.ToList()))))
Your code examples seem to be incorrect - for example, it seems like User.Teams should be a list of some type, not a TeamList object, which isn't really a list. Anyway, with a little modification, this is what I came up with - maybe it's close to what you were looking for (a list of users with the scores sorted). You can paste into LINQPad to run it.
Sub Main
Dim Definition As New Dictionary(of String, Object)
definition.add("user1_redTeam-02", New Object)
definition.add("user1_redTeam-01", New Object)
definition.add("user1_blueTeam-03", New Object)
definition.add("user2_redTeam-01", New Object)
definition.add("user1_redTeam-03", New Object)
definition.add("user1_blueTeam-01",New Object)
definition.add("user2_blueTeam-01", New Object)
definition.add("user1_blueTeam-02", New Object)
definition.add("user2_redTeam-02", New Object)
Dim q3 = (
From userlists In Definition.Keys.GroupBy(Function(s) s.Split("_"c)(0))
Select New User(
userlists.Key,
(From scList In userlists.GroupBy(Function(s) s.Split("-"c)(0))
Select New Team(scList.Key.Split("_"c)(1), scList.OrderBy(Function(s) s).ToList())).ToList()
)
).ToList()
q3.dump()
End Sub
' Define other methods and classes here
Public class User
Public Property Name() As String
Public Property Teams() As List(Of Team)
Public Sub New(d As String, results As List(Of Team))
Me.Name = d
Me.Teams = results
End Sub
End Class
Public Class Team
Public Property TeamName() As String
Public Property FullScore() As List(Of String)
Public Sub New(name As String, value As List(Of String))
Me.TeamName = name
Me.FullScore = value
End Sub
End Class

How to resolve errors when using 'Dictionary'

I'm creating a Class to handle registry entries for my application, but I'm getting some problems early on.
In the below, it should take all of the keys/values for a SubKey and add them to a Dictonary. The message box that is commented out shows the keys and values correctly, but every time the function is run the line below generates an error A first chance exception of type 'System.NullReferenceException'.
As the registry keys themselves seem to be fine, I think it's to do with the way I'm using the RegKeys Dictonary. If someone could take a look and advise I'd be grateful. Thanks.
This is how I'm initiating the Class (I haven't even tried to do anything else just yet) -
Private Sub getMyRegSettings()
Dim ServerPing As RegistryKey = Registry.CurrentUser.OpenSubKey("ServerPing")
Dim Servers As RegistryKey = ServerPing.OpenSubKey("Servers")
Dim MyRegistry As New MyRegistry(Servers)
Dim RegKeys As Dictionary(Of String, String) = MyRegistry.RegKeys
End Sub
And here is the Class that is causing me some trouble -
Public Class MyRegistry
Public RegKeys As Dictionary(Of String, String)
Public Sub New(SubKey As RegistryKey)
get_registry_keys(SubKey)
End Sub
Private Sub get_registry_keys(SubKey As RegistryKey)
' Print the information from the Test9999 subkey.
For Each valueName As String In SubKey.GetValueNames() ' Error occurs here
'MsgBox("Key: " & valueName & vbCrLf & "Value: " & SubKey.GetValue(valueName))
RegKeys(valueName) = SubKey.GetValue(valueName).ToString()
Next valueName
End Sub
End Class
You are not initialising your RegKeys Object
Try changing this line:
Public RegKeys As Dictionary(Of String, String)
to this:
Public RegKeys As New Dictionary(Of String, String)
This will ensure the dictionary is initialised when the class is created
This line
Public RegKeys As Dictionary(Of String, String)
declares a variable of Dictionary type (a reference type) but this variable is not an instance of a Dictionary. To become a valid instance you need to instantiate with new
Public RegKeys = new Dictionary(Of String, String)

How do I pass a List through the PreviousPage property?

I'm trying to pass some values to another page and I have the values in List (Of String). However, I'm getting an error (at design-time in the IDE) saying: There is no member "X" of System.Web.UI.Page. I was able to get the Property through Intellisense. So I'm not sure why I'm getting the error.
Here's the code:
'Source Page (Default.aspx)-
Private locationDetails As List(Of String)
Public ReadOnly Property LocationsForDetail() As List(Of String)
Get
Return locationDetails
End Get
End Property
Private loadDetails As List(Of String)
Public ReadOnly Property LoadsForDetail() As List(Of String)
Get
Return loadDetails
End Get
End Property
Private optionDetails As List(Of String)
Public ReadOnly Property OptionsForDetail() As List(Of String)
Get
Return optionDetails
End Get
End Property
Protected Sub RadButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadButton2.Click
For Each facility As ListItem In CheckBoxList1.Items
If facility.Selected Then
locationDetails.Add(facility.Text)
End If
Next
For Each load As ListItem In CheckBoxList2.Items
If load.Selected Then
loadDetails.Add(load.Text)
End If
Next
optionDetails.Add(DropDownList2.SelectedValue)
optionDetails.Add(DropDownList3.SelectedValue)
optionDetails.Add(DropDownList4.SelectedValue)
optionDetails.Add(RadDatePicker1.SelectedDate.Value.ToShortDateString)
optionDetails.Add(RadDatePicker2.SelectedDate.Value.ToShortDateString)
Server.Transfer("Breakdown.aspx")
End Sub
'Target Page (Breakdown.aspx) -
Protected Sub populateChart()
Dim locations As List(Of String) = PreviousPage.LocationsForDetail 'get error here
Dim capacities As List(Of String) = PreviousPage.LoadsForDetail 'get error here
Dim rescOptions As List(Of String) = PreviousPage.OptionsForDetail 'get error here
bindData.populateChart(RadChart1, locations, capacities, rescOptions)
End Sub
Server.Transfer wouldn't do a postback to the page. I believe (however it's been a long time since I have used PreviousPage) that it only works when you the the PostBackURL property on a control.
It seems that even though the IDE marked it as an error, the code compiled and ran fine. I'm not sure if I'm lucky, the IDE is buggy, or it's some sort of undefined behavior.

Do local variables in shared method work like static variable in C?

Will the list in this shared method keep its state throughout the life of the method? Or will a new list be created every time this method is called?
Protected Shared Function newResxNodes(ByVal newName As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode)
Dim newResxNodesList As List(Of ResXDataNode) = New List(Of ResXDataNode)
Dim newResxNode As ResXDataNode = New ResXDataNode(newName, newValue)
If newComment <> String.Empty Then
newResxNode.Comment = newComment
End If
newResxNodesList.Add(newResxNode)
Return newResxNodesList
End Function
No, It does not work like static variables in C. It will be a new list for every call. If you want to retain the list and list items, create a shared class field.
I've done a test and it returns 3 lines.
Module Module1
Class b
Public Sub New()
Console.WriteLine("New")
End Sub
End Class
Class a
Public Shared Sub Test()
Dim c As b = New b
End Sub
End Class
Sub Main()
a.Test()
a.Test()
a.Test()
Console.ReadLine()
End Sub
End Module

Vb.net Custom Class Property to lower case

I am trying to create a settings class.
The Property Test() is a list of strings.
When I add a string such as: t.test.Add("asasasAAAAA")
I want it to autmatically turn lowercase.
For some reason it is not. Any Ideas?
p.s.
using t.test.Add(("asasasAAAAA").ToLower) will not work for what I need.
Thank you.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As New Settings
t.test.Add("asasasAAAAA")
t.test.Add("aBBBBBAAAAA")
t.test.Add("CCCCCsasAAAAA")
End Sub
End Class
Public Class Settings
Private strtest As New List(Of String)
Public Property test() As List(Of String)
Get
Return strtest
End Get
Set(ByVal value As List(Of String))
For i As Integer = 0 To value.Count - 1
value(i) = value(i).ToLower
Next
strtest = value
End Set
End Property
End Class
ashakjs
That's the reason: set accessor of your property is actually never called.
when you use t.test.Add("asasasAAAAA") you are actually calling a get accessor, which returns a list, after that specified string is added to this list, so .ToLower function is never called.
Simple way to fix this:
Dim list as New List(Of String)
list.Add("asasasAAAAA")
list.Add("aBBBBBAAAAA")
list.Add("CCCCCsasAAAAA")
t.test = list
Alternatively, you can implement your own string list (easiest way - inherit from Collection (Of String)), which will automatically convert all added string to lower case.
What you are trying to do and what you are doing don't match. To do what you want, you need to create your own collection class extending the generic collection - or provide a custom method on your settings class which manually adjusts the provided string before adding it to the local (private) string collection.
For an example of the second option, remove the public property of the settings class which exposes the list of string and use a method like the following:
Public Sub Add(ByVal newProp As String)
strtest.Add(newProp.toLower())
End Sub