What's the elegant way to make sure that values of dictionary are called or set in a specific location? - vb.net

Private Shared Property _dictOfCoinNameNameBasedOnMarketandSymbol As Dictionary(Of String, String)
Get
End Get
Set(value As Dictionary(Of String, String))
End Set
End Property
The usual way, if it's not a dictionary, is to turn the variable into property. Then we can put breakpoint in set and get.
The thing is setting a dictionary as property will make the compiler think I need a set method to set the dictionary. I want a set method to set an item in the property.
I want to be able to replace spread occurrences of
_dictOfCoinNameNameBasedOnMarketandSymbol("key") = "value"
into a code I can set breakpoint too. How should I?
I suppose I can just create a normal sub and replace all occurrences of _dictOfCoinNameNameBasedOnMarketandSymbol("key") = "value" into AssignCoinNameNameBasedOnMarketandSymbolWithKeyValue("key","value")
I wrote
Function GetCoinNameNameBasedOnMarketandSymbol(key As String) As String
Return _dictOfCoinNameNameBasedOnMarketandSymbol(key)
End Function
Sub AssignCoinNameNameBasedOnMarketandSymbol(key As String, coinname As String)
_dictOfCoinNameNameBasedOnMarketandSymbol(key) = coinname
End Sub
And then I will just manually all other occurrences of _dictOfCoinNameNameBasedOnMarketandSymbol. Then I can put breakpoints in AssignCoinNameNameBasedOnMarketandSymbol if I see _dictOfCoinNameNameBasedOnMarketandSymbol get assigned wrong value.
The problem with this technique is there isn't really anyway to enforce it automatically. I just have to go through the code and replace that one by one.
Are there more elegant ways?
If _dictOfCoinNameNameBasedOnMarketandSymbol is not a dictionary then changing it to property with proper get and set value would do just fine. I don't even have to change anything.

I think that I may have initially misinterpreted the question. I will leave my original answer below for completeness but I will answer here based on my new understanding.
It seems to me that what you actually need is an indexed property that will wrap the Dictionary, e.g.
Public Class SomeClass
Private Shared ReadOnly valuesByKey As New Dictionary(Of String, String)
Public Shared Property ValueByKey(key As String) As String
Get
Return valuesByKey(key)
End Get
Set
valuesByKey(key) = Value
End Set
End Property
End Class
You can now just get and set the ValueByKey property and never have direct access - read-only or otherwise - to the Dictionary object inside the class. You can also add whatever code you like before the Return statement in the getter and before or after the existing line of code in the setter.
ORIGINAL ANSWER:
What you want is a ReadOnly property. That's exactly how collections are exposed throughout the .NET Framework, e.g. Control.Controls, ComboBox.Items, DataSet.Tables, DataTable.Columns, DataTable.Rows, etc, etc. In your case, that would look like this:
Public Shared ReadOnly Property CoinNamesByMarketAndSymbol As New Dictionary(Of String, String)
I've taken the liberty of giving that property a sensible name. Note that this is an auto-property, i.e. one where you don't explicitly specify a getter or setter. Without using an auto-property, the equivalent code would this:
Private Shared _coinNamesByMarketAndSymbol As New Dictionary(Of String, String)
Public Shared ReadOnly Property CoinNamesByMarketAndSymbol As Dictionary(Of String, String)
Get
Return _coinNamesByMarketAndSymbol
End Get
End Property
Note that I have declared this property Public. I note that you have declared it Private but that seems to make little sense. What use could such a property be? If it should only be accessed internally then why would you not just use a field?
With the code above, you can now get the Dictionary from the property to get, add or remove items but you cannot replace the entire Dictionary, i.e. you can do this:
Dim coinName = SomeClass.CoinNamesByMarketAndSymbol(marketAndSymbol)
SomeClass.CoinNamesByMarketAndSymbol.Add(marketAndSymbol, coinName)
SomeClass.CoinNamesByMarketAndSymbol.Remove(marketAndSymbol)
But you cannot do this:
SomeClass.CoinNamesByMarketAndSymbol = New Dictionary(Of String, String)

Related

Why does the Default Property access the setter when removing an item

In the code below, when trying to remove an item from the Cases list the code breaks in the Setter with an index out of bounds. When running the debugger in VisualStudio 2017 it successfully goes through the Remove() function and deletes the last item but after returning to Main() it will break on the Setter and the call stack says it is coming from the Remove call. Example code below:
Sub Main()
Dim Cases As Collection = New Collection()
Dim caseIndex As Integer = 2
Cases.Remove(Cases(caseIndex))
End Sub
Public Class Collection
Public WithEvents Cases As List(Of CaseClass)
Public Sub New()
Cases = New List(Of CaseClass)()
Cases.Add(New CaseClass)
Cases.Add(New CaseClass)
Cases.Add(New CaseClass)
End Sub
Default Public Property BeltCase(ByVal Index As Integer) As CaseClass
Get
Return Cases(Index)
End Get
Set(ByVal Value As CaseClass)
Cases(Index) = Value
End Set
End Property
Public Sub Remove(ByRef BeltCase As CaseClass)
Cases.Remove(BeltCase)
End Sub
End Class
Public Class CaseClass
Public test As Int16
End Class
Call Stack:
TestingVBBug.exe!TestingVBBug.Module1.Collection.set_BeltCase(Integer Index,TestingVBBug.Module1.CaseClass Value) Line 25 Basic
TestingVBBug.exe!TestingVBBug.Module1.Main() Line 6 Basic
So why would we be going through the Setter at all. And why does that happen after we exit the remove function?
The problem is caused by your Remove() method, that is, you have a ByRef parameter (for some reason). When you use ByRef, any changes made to the parameter inside the method must be reflected to the variable that was passed to the method. That happens by reassigning the value to the original variable.
In your case, it works like this:
The Remove() method is called and a variable (Cases(caseIndex)) is passed to it.
Some work is done inside the Remove() method which might, or might not include changing the value of the parameter BeltCase.
The value of the parameter BeltCase gets reassigned to the variable that was originally passed to the method (which is Cases(caseIndex)).
As a result of the above step, the setter of the BeltCase property gets called with Index = 2 which raises the out of range exception because Cases(2) doesn't exist (was removed).
To confirm, you can see this problem go away when you replace this line:
Cases.Remove(Cases(caseIndex))
..with:
Dim myCase As CaseClass = Cases(caseIndex)
Cases.Remove(myCase)
That way, you create a new variable which refers to the same CaseClass object and most importantly avoid calling the setter of your Collection.BeltClase property.
However, a better solution would be to not use ByRef in the first place since you don't seem to need it in this situation. So, simply use Public Sub Remove(ByVal BeltCase As CaseClass) instead.
Check this question for more about ByVal and ByRef with objects.
One last thing, please don't call your class Collection because it can be very confusing to anyone looking at your project.

Changing a Type to a Class causes ByRef parameters to act ByVal

I've heard advice to change from User Defined Type (UDT) to a regular Class in order to overcome the limitations of UDT, such as not being able to use For Each with a UDT.
I've also heard advice to change from a regular Class to UDT to overcome the Class limitation where you can't pass things BYREF, like...
'Function:
Public Function RemoveArticle (ByRef strMovieTitle As String)
'Expected input is like "Terminator, The"
strMovieTitle = Left(... 'removes the article.
End Function
That works fine for this call:
Dim strMovieTitle As String
strMovieTitle = "Terminator, The"
RemoveArticle strMovieTitle
But not this call:
Dim objMovie As MovieClass
objMovie.strMovieTitle = "Terminator, The"
objMovie.strMovieGenre = "Sci-Fi"
InvertArticle objMovie.strMovieTitle
Even though MovieClass defines
strMovieTitle As String
I can't go changing RemoveArticle (and every simple little function like it) to take a MovieClass parameter instead of a String parameter because there are other UDTs or Classes and String Variables that also need to use RemoveArticle.
What do I do if I need to use For Each and I also need to pass ByRef?
Is there a way a Class can work around the parameter problem?
(Using Excel 2010.)
Now I have understood your concern.
You simply can't take that approach to meet your goal. As Tim Williams has commented in your question, your best bet would be something like this:
Dim objMovie As MovieClass
Dim strMovieTitle As String
strMovieTitle = "Terminator, The"
objMovie.strMovieTitle = InvertArticle(strMovieTitle)
However, I see that this still does not satisfy your need.
My suggestion is as follows:
make your object internal, target properties Private and expose them with Property Let and Property Get. This way you can do the modifications you want to the properties either on set or on get (from within the class... rather than fixing things from outside the class).
Aside note, in regards to create a helper class (as someone has recommended to you): you could join into one class all those functions you use widely, such as RemoveArticle or InvertArticle. However, it requires to create an instance object every time you want to use them and, therefore, does not combine well with the recommendation I am giving to you (if you want just to simplify code). So having them in a Module as you do now is fine. Just to clarify: those recommendations they gave to you are unrelated to your question here.
Example A: on set
In you class MovieClass, rename first all the instances of strMovieTitle to pStrMovieTitle and add this to your code:
Private pStrMovieTitle As String
Public Property Let strMovieTitle (strIn As String)
pStrMovieTitle = InvertArticle(strIn)
End Property
Public Property Get strMovieTitle As String
strMovieTitle = pStrMovieTitle
End Property
The usage would be something like this:
Dim objMovie As MovieClass
objMovie.strMovieTitle = "Terminator, The" ' the article position gets rectified on assignation
objMovie.strMovieGenre = "Sci-Fi"
'InvertArticle objMovie.strMovieTitle ' => you don't need to do this call
Example B: on get
To keep your original string as it comes, and do apply your helpers when you get the property value. That way you always preserve the original string. However, this approach will need more rework and it's only worthy in cases where you have lots of ways to use that String in different parts of your code.
Private pStrMovieTitleSource As String
Public Property Let strMovieTitle (strIn As String)
pStrMovieTitleSource = Trim(strIn)
End Property
Public Property Get strMovieTitleSource () As String
strMovieTitleSource = pStrMovieTitleSource
End Property
Public Property Get strMovieTitleRoot () As String
strMovieTitleRoot = RemoveArticle(pStrMovieTitleSource)
End Property
Public Property Get strMovieTitle () As String
strMovieTitle = InvertArticle(pStrMovieTitleSource)
End Property
Hope it helps

VBA Complicated Getter, Setter syntax

Hi I'm rather new to VBA I need to create an object with relativelly complicated Getter and Setter. To do this I am constantly checking with MSDN but clearly I am not understanding something because VBE keep highlighting lines starting and closing: Property (it appaently needs Get or Let??), Get(it apparently needs identifier), Let(it apparently needs identifier as well).
But I am trying to follow more concise notation where Get and Let methods are within a Property Statement which is used by Microsoft in its examples(see link above).
Can someone tell me where is my syntax wrong(or Microsoft's Documentation for that matter)???
Thank you
Private Matrix() As Vector
Property Transition()
Public Get(Old_S As String, New_S As String, Period As Integer) As Double
' Some Code
Return Matrix(Column, Row).Value(Period)
End Get
Public Let(Old_S As String, New_S As String, Vector_String As String)
' Some Code
Matrix(Row, Column).Value = Vector_String
End Let
End Property
You are reading the documentation for VB.NET. That's why you are confused. The syntax for properties in VBA is different. In VBA, the Get and Let for a property are not grouped together. They need to be listed separately, essentially like two separate methods:
Private mMyProperty As String
Public Property Get MyProperty() As String
MyProperty = mMyProperty
End Property
Public Property Let Transition(Value As String)
mMyProperty = Value
End Property
For VBA reference material, try starting here.
Your issue is that you are reading .Net help files! :)
Assuming you have a valid Vector class, your properties need to bde defined like this:
Private Matrix() As Vector
Public Property Get Transition(Old_S As String, New_S As String, Period As Integer) As String
' Some Code
Transition = Matrix(Column, Row).Value(Period)
End Property
Public Property Let Transition(Old_S As String, New_S As String, Period As Integer, Vector_String As String)
' Some Code
Matrix(Row, Column).Value = Vector_String
End Property
Note that the argument list for both procedures must match except that the Letter has an additional argument of the same type that the Getter returns.
It is also slightly unusual to have a Getter property that accepts arguments in VBA - that would more usually be implemented as a method.
Looks like you're using VB.Net syntax in VBA. That's not going to work, as they're entirely different languages. Here is a link to the proper documentation for the VBA Property Keyword.
Here is how you would write it in VBA.
Private Matrix() As Vector
Public Property Get MatrixValue(Old_S As String, New_S As String, Period As Integer) As Double
' Some Code
MatrixValue = Matrix(Column, Row).Value(Period)
End Property
Public Property Let MatrixValue(Old_S As String, New_S As String, Vector_String As String)
' Some Code
Matrix(Row, Column).Value = Vector_String
End Property
But you're going to have trouble with the Get property. You can pass parameters into Get, but it's not exactly intuitive. I think what you're really looking for is a function.
Public Function GetMatrixValue(Old_S As String, New_S As String, Period As Integer) As Double
' Some Code
MatrixValue = Matrix(Column, Row).Value(Period)
End Function

How to instantiate Class object with varying number of property values

Been working a lot with custom classes lately and I love the power you can have with them but I have come across something that I'm not able to solve and/or find anything helpful online.
I have a list of a class with properties I'm looking to only store information pulled from a database into.
Public Class CustomClass
Public _Values As String
Public _Variables As String
Public ReadOnly Property Values() As String
Get
Return _Values
End Get
End Property
Public ReadOnly Property Variables() As String
Get
Return _Variables
End Get
End Property
Sub New(ByVal values As String, ByVal variables As String)
_Values = values
_Variables = variables
End Sub
End Class
I will be iterating through some database entries, and I'm looking to store them into the appropriate property when I hit them (since I won't have them all available immediately, which is part of my problem). I want to just be able to add either the value or the variable at a time and not both of them, but since I have the sub procedure 'New' passing two arguments, it will always require passing them both. I've found the only way around this is by making them optional fields which I don't feel is the right way to solve this. Is what I'm looking to do possible with a class or would it be simpler by using a structure?
You can overload the constructor:
Friend Class Foo
' using auto-implement props:
Public Property Name As String ' creates a _Name backing field
Public Property Value as Integer
Public Sub New(newN as String, newV as Integer)
' access "hidden" backing fields if you want:
_Name = newN
_Value = newV
End Sub
Public Sub New() ' simple ctor
End Sub
Public Sub New(justName As String)
' via the prop
Name = justName
End Sub
End Class
You now have 3 ways to create the object: with full initialization, partial (name only) or as a blank object. You will often need a "simple constructor" - one with no params - for other purposes: serializers, Collection editors and the like will have no idea how to use the parameterized constructors and will require a simple one.
If rules in the App were that there was no reason for a MyFoo to ever exist unless both Name and Value being defined, implementing only the New(String, Integer) ctor enforces that rule. That is, it is first about the app rules, then about coding convenience.
Dim myFoo As New Foo ' empty one
myFoo.Name = "ziggy" ' we only know part of it
Since the default of string is nothing, you could pass nothing for the value you don't have. IE
Collection.Add(New CustomClass("My Value",Nothing))
Every type has a default, so this works with more than just strings.

VB.net 3.5 assignmet operator assigning pointer instead of copying?

My program has decided to assign pointers instead of make copies of an object, and I'm not sure why. I have something like this:
Public Class Foo
Private myFooData As New List(Of FooData)
Public Sub New(ByVal newFooData As List(Of FooData))
myFooData = newFooData
End Sub
Public Property FooValues() As List(Of FooData)
Get
Return myFooData
End Get
Set(ByVal value As List(Of FooData))
myFooData = value
End Set
End Property
End Class
And it's used like this:
Public Sub Dosomething()
Dim mainFoo as new Foo
For x = 1 to 10
mainFoo.FooValues(x) = New FooData
Next
Dim originalFoo as new Foo
originalFoo.FooValues = mainFoo.FooValues.Take(3).ToList
Dim newFoo as new Foo
newFoo.FooValues = originalFoo.FooValues
newFoo.FooValues(1) += 1
End Sub
Very simplified, but basically what I'm doing. So for some reason today when I change item in newFoo.FooValues, originalFoo.FooValues also changes, and mainFoo does not. I've tried assigning the entire objects as well and I get the same results. Any ideas why this may be happening and how to fix it?
This is how assignment in .Net is supposed to work.
When you called .ToList() in the middle of your second snippet, your code iterates over the set and makes copies into a whole new list. This is why your mainFoo object is "protected" — you created a new instance. If the FooData items being copied are themselves references to objects (hint: they probably are), then only the references are copied. The only exceptions are for strings and value types (primitives and structures), or if you code it by hand.
It's usually a good idea for List properties to make the property readonly:
Public Property FooValues() As List(Of FooData)
Get
Return myFooData
End Get
End Property
This will still let you manipulate the list to your heart's content, but prevents you from completely switching a list instance out from under the class. The same is true for other complex types exposed from a class as a property.
The one thing you don't want to do is change this to be a Structure instead of a Class. This may seem to do what you want at first, but it will cause other problems for you later.
When using Foo.FooValues, you assign references to the List(Of FooData) internally used in your Foos - and no values!
Let's consider the steps
Dim mainFoo as new Foo
mainFoo now has its own backing list.
originalFoo.FooValues = mainFoo.FooValues.Take(3).ToList
originalFoo gets assigned a new backing list storing some of the values of mainFoo.
newFoo.FooValues = originalFoo.FooValues
newFoo used to have a backing list of its own, but now, it uses the one originalFoo uses. The exactly same one (by pointer) and no copy.
Thus changing newFoo will change originalFoo but not mainFoo which has its own list.
Since I don't know what you're trying to achieve after all, I cannot tell how to fix the code, but as you see, it's never a good idea to make some backing list accessible i.e. assignable.
Thus I'd advice to keep the list immutable and private, just giving indexing access.