Global Variables - wpf

In my WPF applciation I would like to use global variable, the purpose is like to store the current user information etc, The problem is that, there are two methods for it and they are:
Application.Current.Properties vs My.Settings
I know that using My.Settings, the changes will be stored and when the app restarts or reopened, they would load the last saved ones, I dont want it this way. Could you please clarify is Application.Current.Property solve my problem or is there any other method for this.
Thank you.

Why not just create a static or singleton class to hold all your values, if your going to reload them every time the program is going to run anyway?
Public Class Globals
Public Shared Property One As String
Get
Return TryCast(Application.Current.Properties("One"), String)
End Get
Set(value As String)
Application.Current.Properties("One") = value
End Set
End Property
Public Shared Property Two As Integer
Get
Return Convert.ToInt32(Application.Current.Properties("Two"))
End Get
Set(value As Integer)
Application.Current.Properties("Two") = value
End Set
End Property
End Class

Related

How do we get immutable version of HashSet, List, and other stuffs

For example,
I have hashSet like this
Protected ReadOnly Property idsOfExistingOrder As HashSet(Of String)
It's ReadOnly
However, the ReadOnliness of that variable simple means that outsider cannot change the HashSet. It can still say add element to idsOfExistingOrder
So I should do something like this
Function getidsOfExistingOrder() As something
End Function
What is that something?
I look that HashSet support some interface
ICollection<T>, IEnumerable<T>, IEnumerable, ISerializable, IDeserializationCallback, ISet<T>, IReadOnlyCollection<T>
None of which is appropriate
IReadOnlyCollection<T> seems good but only support count. I want to test if the HashSet also contains some string.
So how should I do this?

How do I save a reordered list back to the SQL Server?

Using EF6 and SQL Server. This question is to ask if I am going in the right direction with my code.
Using a variety of SO posts I was able to define classes that contain lists and incorporate IComparable(Of T). Although the objects in the lists have properties that can be used for ordering such as dates, I need to maintain a user-defined order that is solely determined by how the user wants the objects arranged.
My first assumption is that I have to maintain a distinct property to maintain the user-defined order. Is that correct? This is a very simplified example of what I currently have defined:
Class Parent
:
Public Property SomeList as List(Of Something)
End Class
Class Something
Implements IComparable(of Something)
:
public property PositionInList as Integer
Public Function CompareTo(that As Something) As Integer ...
Return PositionInList.CompareTo(that.PositionInList)
End Function
End Class
And if I write the following:
dim aList as New List(Of Something)
aList = Parent.SomeList
aList.Sort()
...alist is sorted based on PositionInList as I expected.
If I want to insert a new record into the list, am I responsible for changing the values of PositionInList throughout all the objects in the list? The reason I ask is because the LINQ command of the form .insert(index,object) indicates it inserts an element into the List(Of T) at the specified index. And as I look at the table after the .insert(index,object) prior to SaveChanges(), the list does have the new object in the correct location, but it didn't set PositionInList to the value of the index specified in .insert(index,object). Is it supposed to and I haven't coded things correctly? Also, PositionInList is going to have to ripple the change throughout all the objects - am I responsible for that?
Are there predefined methods I need to learn that would manipulate PositionInList when I insert?
I will also want the user to be able to move items to new positions in the lists. Are there patterns to do this kind of thing, or once again is that pretty much up to me?
Added per request in comment below:
The application is divided into 3 layers: UI, domain, storage. The storage layer includes a storage service class which has the CRUD methods that interface to the SQL Server.
This is one of the current Create methods. At present it only appends a record at the end of the list. Per your comment, I will modify it to update PositionInList when the call to create requests an insertion somewhere inside the list. That will require additional parameters not currently coded in the method.
Public Overrides Sub ElementCreate(DomainElement As DomainElement)
Dim aSqlElement As New SqlElement
aSqlElement.DomainToSqlElement(DomainElement)
Dim theParent = SqlContext.SqlContents.Include("ContentElements").Where(Function(x) x.ID = aSqlElement.ContentID).SingleOrDefault()
theParent.ContentElements.Add(aSqlElement)
SqlContext.SaveChanges()
End Sub
You can use a SortedSet instead so that you don't have to re-sort.
Class Parent
:
Public SortedSet<Something> SomeList = new SortedSet<Something>(
new FuncComparer<Something>( (a, b) => a.PositionInList.CompareTo(b.PositionInList ) ));
End Class

VBA Passing Class Arrays to Functions

Apologies if I am making some amateur mistakes, but I am new to VBA.
I'm trying to populate an array declared inside of a class as a property, but something's going wrong. After much searching I have two questions that I can't seem to find anywhere else:
1 - Is there a simpler way to accomplish saving data to an array-like structure that I can pass to functions from a sub? The array-like structure needs to be resizable because I will not know how many components I will add until each iteration of a loop checks those conditions.
2 - How do I correctly accomplish the passing of the array property in my class correctly to another function? It's frustrated me enough that I would like to know how it can be done if only to understand what I was doing wrong or, perhaps, what misunderstanding I had of the way VBA works.
Code structure follows:
I have declared a Segments property inside of a CTask class like this:
Private pSegments() As CSegment
Public Property Get Segments() As CSegment()
Segments = pSegments()
End Property
Public Property Get segment(index As Integer) As CSegment
segment = pSegments(index)
End Property
Public Property Let Segments(Value() As CSegment)
pSegments() = Value()
End Property
I pass the CTask from a Sub, where it is defined to populateTasks using this code:
Dim tasks() As CTask
ReDim tasks(1 To 10)
Call populateTasks(tasks)
When I do this, the populateTasks code receives it using the following code:
Function populateTasks(ByRef tasks() As CTask)
I then try to call another function from populateTasks called populateSegments like this:
Call populateSegments(tasks(icount).Segments)
I receive segments array inside populateSegments like this:
Function populateSegments(ByRef Segments() As CSegment)
The last two code snippets are where the problem resides. The segments array is populating correctly inside the populateSegments function, but when I check the array to see if it is empty just below the call to populateSegments there isn't anything in the tasks(icount).segments array. Thanks in advance for any help, and please let me know if more information is required.
Couldn't you make populatesegments a method of cTask? That avoids the problem and is a better design

How to fill a listbox with an array filled in module1 VBA

I'm new to creating user forms in VBA. I've been using VBA macros for a while now though so I understand some about them. Right now I'm creating a spreadsheet for a user where I'm using a user form with a list box. I have a lot of code in a regular module and need to refernce the module to fill the listbox. What I have right now is this:
Private Sub StartButton_Click()
Dim Number
Call GetTellerNames
For Number = 0 To 40 Step 1
If GetTellerNames(Number) <> "" Then
ListBox1.AddItem (GetTellerNames(Number))
End If
Next
End Sub
When I run this I get an error saying
Sub of Function not defined
How do I fix this so that I can use the array in my module to fill the list box? I've already got the code to fill the array working.
Here is the code for the GetTellerNames sub in the module:
Private Function GetTellerNames()
GetTellerNames = FindOthers(BranchNumber, TellerCode, 2)
End Function
It is using global variables that are set in other parts of the code. I can post all of the code if necessary.
Since the GetTellerNames() code lies within a standard module you need to change the access modifier to public in order to be able to access that method/sub from the UserForm1 object module.

Aggregation relationships and instantiating objects from database storage (vb)

I have a small application which allows users to create accounts and then award 'points' to each other. UserAccount is an object, and so is Point, and there is an aggregation relationship between them: each UserAccount has a member variable (PointHistory) which is a collection of Points.
The Point object simply contains properties for which UserAccount object sent/received the Point.
This was fine when I simply serialized the classes to persist my object model. However, I am now implementing a database to persist the object data, and only one/some objects will be loaded into memory at any given time. To load the objects, I have a constructor method which creates a new UserAccount from a row in the database, and a similar constructor to create a Point from a database row. This is where the trouble starts - how can the constructor for one object call the constructor for another object when the second constructor must refer to what the first constructor has not yet constructed? In order to complete construction, the object must be already constructed.
The easy solution is simply to replace the collection of Point objects (PointHistory) with a collection of strings drawn from a database query. This suffices for my purposes. However, I'm wondering if there is another way to do that which doesn't abandon the Point object/object-model, and whether this aggregation/persistence problem is common?
If I understand correctly, the UserAccount constructor needs to construct the Points that belong to this UserAccount, and these Points need a way to refer to the UserAccount currently under construction?
If that is the case, you can pass Me into the Point constructor, which will in effect pass the UserAccount that's being constructed. It will pass the UserAccount object even it the construction hasn't yet completed.
Class UserAccount
Private _pointHistory As New System.Collections.Generic.List(Of MyProject.Point)
Public Sub New()
...
...
' Instantiate a new point, and pass this UserAccount object into it
' so that it can grab a reference
Dim newPoint as New MyProject.Point(Me)
'Add the Point to this UserAccount's collection
_pointHistory.Add(newPoint)
End Sub
End Class
Class Point
Private _parentAccount as MyProject.UserAccount
Public Sub New(parentAccount as MyProject.UserAccount)
'Store the reference to the User Account
_parentAccount = parentAccount
End Sub
End Class

Resources