How do I pass a List through the PreviousPage property? - vb.net

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.

Related

how to handle properties with this FxCopAnalyzer warning: "CA2227: Set "Images" as read-only by removing the setter for the property"

What is this programme about:
This program was developed with the VB.Net language, the .NET Framework 4.8 and Visual Studio 2019 CE. The idea of this program is to run a rudimentary database. The list is similar to a classic Internet forum—there are threads, there are different numbers of postings in the threads and in each post there are different numbers of pictures and long texts. If the thread is selected using the ComboBox, all posts with their images and texts are displayed one below the other. When you click on a specific post, only its images are displayed.
The user can create threads and posts. For reasons of legibility, only extended Latin letters are allowed when writing the text (e.g. René, Chloë).
When you close the program, you will be asked whether you want to save the data. These data are read in when the program is loaded. If images are not found, their paths will be displayed in a window.
The user also has the option of searching through all threads and viewing the results with various sorting options. In this case, only the posts found are listed in the ListBox, and here, too, the user can select the posts individually.
The program also reads in user data when it starts. A user can log in and, depending on his role, has certain power to make decisions. A “normal” user can create threads and posts, but only an administrator or moderator can edit and delete texts. If you are not logged in, you can only read threads and posts.
What I would like to know from you
I use the FxCopAnalyzer, which is currently giving me 8 warnings. I am aware that it sometimes exaggerates a little or criticizes certain things that were done on purpose. But I'd like to let you guys have a look to make it better. What exactly is this supposed to mean: "CA2227: Set "Images" (Bilder) as read-only by removing the setter for the property". Because when I remove the setter, I get an error message.
I feel the same way with other properties in other classes. So I need to know, how to fix this, and I need a general solution. I appreciate.
Class structure
There is the class Forum, the class Class_Thread and the class Class_Post. Class_Thread inherits from forum. In the Class_thread there is a list(of Class_Post) which contains instances of Class_Post. ("The thread knows what posts it has"). In FormMain, a List(of Class_Thread) is created, in which the instances of Class_Thread are.
In Class_Post's constructor, the following parameters are transferred: 1) the heading of the posting, 2) the text, 3) the number, 4) images as List(Of System.Drawing.Bitmap), 5) the date, 6) the image paths As List(Of String).
Class_Post.vb
#Disable Warning CA1707 ' Bezeichner dürfen keine Unterstriche enthalten
Public Class Class_Post : Inherits Class_Forum
Private _ueberschrift As String = ""
Private _text_dazu As String = ""
Private _nummer As UInt16
Private _bilder As List(Of System.Drawing.Bitmap)
Private _erstelldatum_dieses_Posts As Date
Private _pfade_der_Bilder As List(Of String)
Public Property Ueberschrift As String
Get
Return _ueberschrift
End Get
Set(value As String)
_ueberschrift = value
End Set
End Property
Public Property Text_dazu As String
Get
Return _text_dazu
End Get
Set(value As String)
_text_dazu = value
End Set
End Property
Public Property Nummer As UShort
Get
Return _nummer
End Get
Set(value As UShort)
_nummer = value
End Set
End Property
Public Property Bilder As List(Of Bitmap)
Get
Return _bilder
End Get
Set(value As List(Of Bitmap))
_bilder = value
End Set
End Property
Public Property Erstelldatum_dieses_Posts As Date
Get
Return _erstelldatum_dieses_Posts
End Get
Set(value As Date)
_erstelldatum_dieses_Posts = value
End Set
End Property
Public Property Pfade_der_Bilder As List(Of String)
Get
Return _pfade_der_Bilder
End Get
Set(value As List(Of String))
_pfade_der_Bilder = value
End Set
End Property
Public Sub New(ByVal Ueberschrift As String, ByVal Text As String, ByVal nr As UInt16, ByVal uebergebene_Bilder As List(Of System.Drawing.Bitmap), ByVal _date As Date, ByVal Pfade As List(Of String))
Me.Ueberschrift = Ueberschrift
Me.Text_dazu = Text
Me.Nummer = nr
Me.Bilder = uebergebene_Bilder
Me.Erstelldatum_dieses_Posts = _date
Me.Pfade_der_Bilder = Pfade
End Sub
End Class
#Enable Warning CA1707 ' Bezeichner dürfen keine Unterstriche enthalten
This is in FormMain, where a new Instance of Class_Post is created This Sub is using a second Form from which the data is taken.
Private Sub Button_neueAntwort_Click(sender As Object, e As EventArgs) Handles Button_neueAntwort.Click
' doppelt abgesichert, falls die Variable SI aus irgendeinem Grunde nicht (-1) ist, obwohl sie das sollte.
If SI <> (-1) OrElse ComboBox1.SelectedItem IsNot Nothing Then
Using FA As New Form_Antwort
Dim DR As DialogResult = FA.ShowDialog(Me)
If DR = DialogResult.Yes Then
Dim neuerPost As New Class_Post(FA.Titel_des_Posts, FA.Beschreibung, CUShort(Liste_mit_allen_Threads(SI).Posts_in_diesem_Thread.Count + 1), FA.NeueListeBitmaps, Date.Now, FA.Liste_mit_Pfaden)
Liste_mit_allen_Threads(SI).Posts_in_diesem_Thread.Add(neuerPost)
alle_Posts_in_diesem_Thread_anzeigen()
End If
End Using
Else
MessageBox.Show("Bitte zuerst einen Thread auswählen.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Thanks to Jimi and Craig, I can come up with a solution here. The thing is, unfortunately I let the FxCopAnalyzer influence me a bit. It had suggested using getters and setters instead of the normal fields, and that's why the warning I reported about came up. But the solution looks very simple, just use properties:
Public Class Class_Post : Inherits Class_Forum
Public ReadOnly Property Ueberschrift As String = ""
Public ReadOnly Property Text_dazu As String = ""
Public ReadOnly Property Nummer As UInt16
Public ReadOnly Property Bilder As List(Of System.Drawing.Bitmap)
Public ReadOnly Property Erstelldatum_dieses_Posts As Date
Public ReadOnly Property Pfade_der_Bilder As List(Of String)
Public Sub New(ByVal Ueberschrift As String, ByVal Text As String, ByVal nr As UInt16, ByVal uebergebene_Bilder As List(Of System.Drawing.Bitmap), ByVal _date As Date, ByVal Pfade As List(Of String))
Me.Ueberschrift = Ueberschrift
Me.Text_dazu = Text
Me.Nummer = nr 'kann maximal 65535 werden
Me.Bilder = uebergebene_Bilder
Me.Erstelldatum_dieses_Posts = _date
Me.Pfade_der_Bilder = Pfade
End Sub
End Class
Edit:
Of course, this is only true until you decide to create a button where a moderator wants to delete an image. Then, ReadOnly has to be removed again and the FxCopAnalyzer complains again.

Add rows to class bound datagridview

Hi I'm trying to bind a list of objects to a datagridview
Binding a existing list(Of is working but I'm trying to add or remove a object from, my dgv isn't updating.
Public Class Form1
Dim lt As New List(Of Test)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lt.Add(New Test("Mac", 2200))
lt.Add(New Test("PC", 1100))
dgv.DataSource = lt
lt.Add(New Test("Android", 3300)) 'This line won't appear in the dgv
End Sub
End Class
Public Class Test
Public Sub New(ByVal name As String, ByVal cost As String)
_name = name
_cost = cost
End Sub
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _cost As String
Public Property Cost() As String
Get
Return _cost
End Get
Set(ByVal value As String)
_cost = value
End Set
End Property
End Class
How can I add or remove or change a value from the dgv to the list and inverse?
NoiseBe
Change this line:
Dim lt As New List(Of Test)
To:
Imports System.ComponentModel
...
Private lt As New BindingList(Of Test)
When the collection contents will change, you should use a BindingList(Of T). This collection has events associated with it which make it aware of changes to the list contents.
If in addition to the list contents, the list items will change (like Test.Name), you should also implement INotifyPropertyChanged on the class itself.
Another way to do it:
dgv.DataSource = Nothing
lt.Add(New Test("Android", 3300))
dgv.DataSource = lt
This "resets" the DataSource so that the new contents will show up. However, it means that several other things get reset like selected items; if you are binding to a List/Combo control, you will also have to reset the ValueMember and DisplayMember properties as well.

Sharing a List of(String) between Private Sub

In my code I basically read a textbox and put each line into a list (Of String) Dim "testblock" in code below
From there I create another list (of string) and split each line whenever a space is found. Dim "block" in code below
Now I want to be able to access that list from anywhere in the project.
How do I go about sharing a List of(String) between Private Sub such as form buttons?
Private Sub PhaseCodeBTN_Click(sender As Object, e As EventArgs) Handles PhaseCodeBTN.Click
Dim testblock As New List(Of String)
Dim lines As String() = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim block As New List(Of String)
For Each l As String In lines
testblock.Add(l)
Dim words As String() = BlockCodeBox.Text.Split(" ")
For Each splitword As String In words
block.Add(splitword)
Next
Next
BlockCodeBox.Text = testblock(BlockNumBox.Text)
WordCmdBox.Text = block(WordNumBox.Text)
End Sub
Private Sub PhaseBlackBTN_Click(sender As Object, e As EventArgs) Handles PhaseBlackBTN.Click
WordCmdBox.Text = block(WordNumBox.Text)
End Sub
Create a Public Shared Class with your List(Of String) there to use it anywhere in the project:
Public Shared Class DataHolder
Private _block As New List(Of String)
Public Property Block As List(Of String)
Get
Return _block
End Get
Set
_block = value
End Set
End Property
End Class
Example:
Either simply use DataHolder.Block.Add(splitword) OR following steps:
Declare a class variable block to use it in entire class:
Private block As List(Of String)
Initialize it in some suitable function / event-handler like Form_Load:
block = DataHolder.Block
Do the operation:
block.Add(splitword)

Duplicate Settings In Reference DLL

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.

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