How registrate two class with circular references in DWScript? - dwscript

I need to register TCollection and TCollectionItem classes, but because of the properties of
{TCollection}
property Items [Index: Integer]: TCollectionItem
{TCollectionItem}
property Collection: TCollection
сonstantly raises the exception of the impossibility to find TCollectionItem/TCollectionItem type.

If you register the class on you own within code, you could first register the TCollectionItem without the property Collection: TCollection, then register TCollection with property Items [Index: Integer]: TCollectionItem and afterwards adding the property Collection: TCollection to your TCollectionItem.

You can use:
type
TCollection = class;
TCollectionItem = class
published
property Items: TCollection;
end;
TCollection = class
published
property Items [Index: Integer]: TCollectionItem;
end;

Related

Implicitly defines - conflict (Auto generated private field)

Okay, so I stumbled upon an auto generated private member I wasn't aware of.
I know that if you have a property named e.g. P then the name get_P is reserved for the getter method and the name set_P is reserved for the setter method.
But what I didn't know was that the name _P is also reserved. It seems that this only applies to properties (not ReadOnly / WriteOnly) and fields defiend as WithEvents.
Public Class Test
Public Property p As Object
Public WriteOnly Property pW() As Object
Set(value As Object)
End Set
End Property
Public ReadOnly Property pR() As Object
Get
End Get
End Property
Public f As Object
Public WithEvents fWE As Object
Private _p As Object
Private _pW As Object
Private _pR As Object
Private _f As Object
Private _fWE As Object
End Class
The above class will produce the following errors:
1) property 'p' implicitly defines '_p', which conflicts with a member of the same name in class 'Test'.
2) WithEvents variable 'fWE' implicitly defines '_fWE', which conflicts with a member of the same name in class 'Test'.
If I remove all the fields named _{name} and return all members (including NonPublic fields) of type Test one can clearly see the auto generated members.
.cctor (Constructor)
.ctor (Constructor)
__ENCAddToList (Method)
__ENCList (Field)
_fWE (Field) <------------------------------- *2
_p (Field) <---------------------------------- *1
Equals (Method)
f (Field)
Finalize (Method)
fWE (Property)
get_fWE (Method)
get_p (Method)
get_pR (Method)
GetHashCode (Method)
GetType (Method)
MemberwiseClone (Method)
p (Property)
pR (Property)
pW (Property)
set_fWE (Method)
set_p (Method)
set_pW (Method)
ToString (Method)
So does anybody know why these fields are generated and/or their purpose?
It's because you're using auto generated properties. Those fields are automatically generated as the backing fields for the property. See section 9.7.4 of the VB 10 Lanugage Spec:
9.7.4 Automatically Implemented Properties
If a property omits declaration of any accessors, an implementation of the
property will be automatically supplied unless the property is declared in
an interface or is declared MustOverride. Only read/write properties with no
arguments can be automatically implemented; otherwise, a compile-time error
occurs.
An automatically implemented property x, even one overriding another
property, introduces a private local variable _x with the same type as the
property. If there is a collision between the local variable's name and
another declaration, a compile-time error will be reported.
The
automatically implemented property’s Get accessor returns the value of the
local and the property’s Set accessor that sets the value of the local.
For example, the declaration:
Public Property x() As Integer
is roughly equivalent to:
Private _x As Integer
Public Property x() As Integer
Get
Return _x
End Get
Set (value As Integer)
_x = value
End Set
End Property

How does "Overloads" work in a child class?

I have a base class and a child class and they both have the same property and I don't understand why VB wants me to use "Overloads" for the property in the child class. The difference is the child class version of the property is Shared while the parent class is basically there for structure. The properties look like this:
Public MustInherit Class Parent
Public ReadOnly Property Species As String
Get
Return "Should get species from a child."
End Get
End Property
End Class
Public Class Child
Inherits Parent
Public Shared ReadOnly Property Species As String
Get
Return "Species1"
End Get
End Property
End Class
Species is flagged in the line Public Shared ReadOnly Property Species As String in the child class with the warning message
property 'Species' shadows an overloadable member declared in the base
class 'Parent'. If you want to overload the base method, this method
must be declared 'Overloads'.
What I want to know is why does it want this to be overloaded? Overloading is typically used when different parameters are being passed into functions with the same name which is well documented, but I've found nothing explaining why overloads is suddenly suggested in a situation like this.
Note: that the code properly reports "Species1" regardless of if have the "Overloads" or not adding to my confusion of what it actually does...
If you want to overload the base method, this method must be declared 'Overloads'.
The error message is too generic. Note how it talks about a method even though the warning is about a property. You cannot overload a property.
If I were the King of France, I would have written the error message like:
Property 'Species' hides the 'Species' property inherited from the 'Parent' base class. Use the Shadows keyword to suppress this warning if hiding was intended. Change the name of the property if hiding was not intended.
This warning should almost never be ignored because is almost always identifies a code smell. Using the Shared keyword for Child.Species is very strange and almost certainly not what you intended. Any code that uses your Child object through a reference of type Parent will always get the wrong species name since it will use the base property. The more sane thing to do here is to declare the Parent.Species property Overridable and use the Overrides keyword in Child.Species property declaration, without Shared.
If you shadow a member - the base class will still use it's version of the member when called. For example, if a function in your base class called Species it would still get the value "Should get species from a child."
In this case, overloading will cause functions in the base class to use the child's value.
To make this a bit clearer... the following code's message box says "Original Value", not "New Value"
Public Class Form1
Dim X As New Child
Dim Y = MsgBox(X.ShowMe)
End Class
Public Class Parent
Public Function ShowMe() As String
Return member
End Function
Public Property member As String = "Original value"
End Class
Public Class Child
Inherits Parent
Public Property member As String = "New value"
End Class

Bind Details view with class object

How can I bind the details view and return a class object?
Dim _obj As New NewRequestDetailsBL
_obj = NewRequestDetailsBL.SelectRequestDetails(Session("requestid"),
Session("contactid"))
dvAdmin1.DataSource = _obj ' this line is giving the error
dvAdmin1.DataBind()
Error:
System.InvalidOperationException: Data source is an invalid type.
It must be either an IListSource, IEnumerable, or IDataSource.
The error contains every information you need!
It must be either an IListSource, IEnumerable, or IDataSource.
Your class has to implement at least one of these interfaces and then you'll be able to set DataSource to an instance of NewRequestDetailsBL class.

.NET DefaultValue Property Attribute instantiate new custom object type

I'm trying to use the DefaultValue attribute to decorate a property in order to assert the value is defaulted as a new list of a typed object in the program. The failing code is as follows:
<DataContract()>
Partial Public Class MessageBaseResponse
#Region "Properties"
<DataMember()>
Public Property Header As Header
<DataMember()>
<DefaultValue(GetType(List(Of [Error])))>
Public Property Errors As List(Of [Error])
<DataMember()>
<DefaultValue(GetType(List(Of Warning)))>
Public Property Warnings As List(Of Warning)
#End Region
End Class
How to instantiate new lists as default property value using the DefaultValue attribute approach?
The DefaultValue attribute has more to do with serializing the data, not setting the actual default value of the property. The linked page notes:
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
Try instantiating the lists with the "New" keyword:
Public Property Errors As New List(Of [Error])

WCF Read DataMember Name attribute

I have a very simple class called person.
public class Person{
[DataMember(Name="MyName")]
public string Name { get;set;}
}
If I try to serialize or de-serialize, everything works great. In the XML I can see a tag called "MyName" and in the object I see with the VS Intellisense a property called Name.
What I need now is to access, from the object, the serialized name of the property.
For example, I can do object.GetType().GetProperty("Name"); but if I try to do object.GetType().GetProperty("MyName"), the reflection says that the property does not exist. How I can read the serialized name of the property? Is there a way?
It seems that the only way is to access, using reflection, the attributes of the property in this way:
var att = myProperty.GetType().GetAttributes();
var attribute = property.GetCustomAttributes(false)[0] as DataMemberAttribute;
Console.WriteLine(attribute.Name);
This works on both, client and server, without the need of serialize and deserialize the object.