property with enum as type - vb.net

In a solution I have noticed a property that has a type enum:
Public Enum ContentType
HTML = 1
JSON = 2
XML = 3
End Enum
Public Property ContentID() As ContentType
Get
Return _contentID
End Get
Set(ByVal value As ContentType)
_contentID= value
End Set
End Property
Strangely these enums reflect a primary key in a table, I had an issue as a client had different primary keys and this was causing a select statement to not be entered.
Everything else seems to be working and it just got me thinking. My question is will this property throw an error if I try to set the value to be something that isn't contained in the enum? Because as I say this will definitely be happening and I have seen no errors thrown or am I missing something.

will this property throw an error if I try to set the value to be something that isn't contained in the enum?
It will not. Enumerations are backed by an integral type (Integer, Long etc...) and a variable will accept any valid value for its underlying type.
You can use the System.Enum.IsDefined method to check the value before trying to use it:
Returns an indication whether a constant with a specified value exists in a specified enumeration.

Related

Type 'Collection' is not defined vb BC30002

I am attempting to migrate a legacy vb.net application to .net standard and turn it into a nuget package. A good amount of it has been straight forward. I am currently hung up on this error caused by functions like this.
Public Property ErrorMessages As Collection
Get
ErrorMessages = _errorMessages
End Get
Set(value As Collection)
_errorMessages = value
End Set
End Property
If i import System.Collections.ObjectModelCollection(Of T) it is asking me for a type and i am unsure how to proceed. It turns my code into
Collection(Of,) and expects a second argument. Has anyone faced this before? Do i use a different import statement or how is this dealt with in vb now?
You should almost certainly replace Collection with Dictionary(Of TKey, TValue), using the dictionary type from the System.Collections.Generic namespace.
Once again, this requires you to fill in the genetic type arguments TKey and TValue with the actual types. You need to figure out from context which type fits the collection. The value of TKey is probably String since that’s the only key type VB6’ collections properly support. And given the name (ErrorMessages), TValue is probably String as well.

Json.net not reading back primary key

I'm using Newsoft JSON lib in a vb .net project and I found this error I can't solve.
I use CLSA objects of type Formulario and add some to a list. When I serialize that list the Primary Keys value are fine. In this example the first object of the list has a value of 3 in the ID property but after deserialize the object that value returns 0.
This happend with every ID. I check CSLA class and try to tweak some values like Int32 to Integer or change the readonly property to a read/write property but no avail.
Am I missing something? Every other property has the correct value but the IDs.
Thanks!

Multiple Set handlers in a property

I remember coming across some way to declare multiple Set handlers in a property but now I can't figure out how it's done. It's useful in that one can assign different data types and the Set handler does the conversion, but I get the error
'Set' is already declared
thoughts anyone?
It's not possible
It would be nice to be able to write both
sQuantity = "1234"
and
sQuantity = 1234
with two setter functions, but trying to write even one setter function with the wrong parameter type seems doomed to failure:-
error BC31064: 'Set' parameter must have the same type as the containing property.
If Visual Basic doesn't allow conversion between setter parameter type and property type then there is no way it would be possible to have two setter functions. If setter functions are forced to have the same type as the property, then it could not know which to run if there were more than one!
So I'd argue 'not only does it not seem possible, but it is actually not possible!'
There is a workaround
What you can do however, is have two properties of different types changing the same underlying variable, so that you can write
sQuantityFromString = "1234"
and
sQuantityFromInt = 1234
using
Public Shared WriteOnly Property sQuantityFromInt () As Integer
with a setter function that takes an integer as a parameter and with both properties setter functions modifying the same underlying string member variable.
Private Shared m_sQuantity As String = Nothing
As far as I know, you cannot have multiple Set statements for a class property. A property cannot be overridden.
You can use a setter functions (this is mostly a paradigm in Java) and overload that if you need to. Then I would also suggest making the property readonly.
One other option is to have the property be defined as an Object and in the set check the TypeOf of the value being used to set the property and do whatever business logic you want. The only problem with this approach is that then your property doesn't have type checking.

Entity framework mapping issues

I am entirely new to .Net having been working in it for a week at most so please go easy and be detailed as possible :)
I have the following PONO:
Public Class WorkOrderEntity
Private intTrackingNumber As Integer
Private intDateReceived As Integer
Private strManufacturer As String
Public Property TrackingNumber() As Integer
Get
Return intTrackingNumber
End Get
Set(value As Integer)
intTrackingNumber = value
End Set
End Property
Public Property DateReceived() As String
Get
' TODO: Convert timestamp to formatted date
Return intDateReceived
End Get
Set(value As String)
' TODO: Convert formatted date to timestamp
intDateReceived = value
End Set
End Property
End Class
The issue I am faced with is how to store date/time as a timestamp (integer) but provide public properties which format/convert accordingly.
I suppose I could provide a an additional getter()/setter() but ideally I wonder if EF has a way of circumventing this "convention"?
Additionally - I am also curious as to whether it's possible to map properties to columns which are not labelled correctly?
Basically if I were working in a existing database (EF automatically builds my PONO with properties named after table fields) I wish to name the fields something more meaningful; some fields for example might be awkward abbreviations but in the object model I want something more English friendly?
I seem to recall being able to do this with Hibernate in Java (actually it's PHP port) but never the less does EF support such a feature?
Any ideas?
A timestamp SQL Server at least is now a RowVersion. In either case it is not a datetime, but rather a byte array for the version which is auto updated when ever the row changes. You don't convert it to a date.
Regarding mapping an element via configuration rather than convention, you can use the attributes or fluent syntax to specify the mapping. See http://msdn.microsoft.com/en-US/data/jj200620 for info on how to use these features.

What kind of array is Foo() as Foo()?

We generated a class from an XML file a while back. I think we used xsd.exe.
One of the main node collections in the XML file was rendered as:
<System.Xml.Serialization.XmlElementAttribute("PRODUCT")> _
Public Property PRODUCT() As PRODUCT()
Get
Return Me.pRODUCTField
End Get
Set
Me.pRODUCTField = value
End Set
End Property
And sure, there's PRODUCT class defined later on, and it worked fine. Serialized and deserialized fine. Didn't need to worry about it or manipulate it.
Only now we have to revisit and manipulate the data.
But what kind of collection (array?) is Public Property PRODUCT() As PRODUCT(), and how do we loop over it? And add to it?
Basic question, I know. Probably got too comfortable with generics and now xsd has thrown something at me which isn't List(of T) I'm running scared.
Don't be confused by the two sets of parens there. The first set, is simply the parens after the name of the property, whereas the second identifies the return type as an array of Product objects.
Similar to: Public Property IDs() As Integer()
That property returns only an array of integers, and the parens near IDs() only exist because you're declaring the property.
Since it appears to be a standard array of Product objects, you can loop over it with any number of normal loops:
For Each p As PRODUCT In obj.PRODUCTS()
...
Next
or
For i As Integer = 0 To obj.PRODUCTS.Length-1
...
Next i
Your code
Public Property PRODUCT() as PRODUCT()
Returns an array of Objects Of Type PRODUCT. Now whether that Type is a Collection, Structure, or Array I do not know with the code you have provided. The simplest way to loop over it would be as such.
For each prod as PRODUCT in rtnPRODUCTS
'Do Something
Next