You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection - vb.net

I have been looking online for quite some time about this error. I cannot seem to be able to figure this one out.
I have a web service created with vb.net in vs 2010. Here is a look at my property
Public Class MyClass
Inherits ConfigurationSection
Protected _score As Integer
<ConfigurationProperty("score", DefaultValue:="12", IsRequired:=False), _
IntegerValidator(ExcludeRange:=False, MinValue:=6, MaxValue:=24)>
Property gt_score() As Integer
Get
Return CType(Me("score"), Integer)
End Get
Set(ByVal value As Integer)
Me("score") = value
End Set
End Property
End Class
When I try to add this as a service to a web app, also done in vs2010 with vb.net, I get the error in the title. Please help with this. I am not sure what is needed in order to implement a default accessor.

I have been struggling with a similar problem myself. I have a class that inherits from ConfigurationSection but I get the same error message when I try to use it in conjunction with XMLSerializer.
I believe this issue cannot be addressed within your class. The problem lies with the properties of the ancestor (ConfigurationSection). It has 4 public properties of type ConfigurationLockCollection (namely LockAllAttributesExcept, LockAllElementsExcept, LockAttributes and LockElements). During serialisation, the serializer finds that these properties implement ICollection but do not have the required default accessors, hence the exception.
For my part I have tried shadowing these 4 properties and decorating them with the XMLIgnore() but still get the same problem. AFAIK it will not be possible to serialize any class that inherits from ConfigurationSection until MS adds the default accessor to ConfigurationLockCollection.
For now I will be implementing my own methods that serialize just the properties I need from my customised configuration section.

I haven't worked with VB.net, but in general, ICollection is an interface class.
An interface describes the methods and properties that classes inheriting from the interface need to define. In your case, your derrived class MyClass needs to define a default accessor.
What if you try:
get
{
return this;
}

I encountered the same problem. I resolved it by creating an XML wrapper object that just contains the properties I want to serialize and create two methods that map from the the unserializable object (a custom configuration element derived from ConfigurationElement) to the Attribute object and back.
I can then serialize/deserialize a collection of the simple objects.
See My Answer To This Question

Related

Is there a solution to "Cannot access '<init>': it is private in XYZ?

I included a library I'd like to use, but in accessing to one of its classes I get the error message,
"Cannot access '<init>': it is private in [class name]
Is there something I can do to rectify this on my side, or am I just stuck to not use the package?
The error means the constructor is private. Given your comment, I'm assuming you're using a library. If this is the case, you'll have to find a different way to initialize it. Some libraries have factories or builders for classes, so look up any applicable documentation (if it is a library or framework). Others also use the singleton pattern, or other forms of initialization where you, the developer, don't use the constructor directly.
If, however, it is your code, remove private from the constructor(s). If it's internal and you're trying to access it outside the module, remove internal. Remember, the default accessibility is public. Alternatively, you can use the builder pattern, factory pattern, or anything similar yourself if you want to keep the constructor private or internal.
I came across this issue when trying to extend a sealed class in another file. Without seeing the library code it is hard to know if that is also what you are attempting to do.
The sealed classes have the following unique features:
A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself.
A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.
Sealed classes are not allowed to have non-private constructors (their constructors are private by default).
Classes that extend subclasses of a sealed class (indirect inheritors) can be placed anywhere, not necessarily in the same file.
For more info, have a read at https://www.ericdecanini.com/2019/10/14/kotlins-sealed-class-enums-on-steroids/
Hopefully, this will help others new to Kotlin who are also encountering this issue.
Class constructors are package-private by default. Just add the public keyword before declaring the constructor.
By default constructor is public so need to remove internal keyword.

Adding [DataContract] to class with [Serializable] causes problems

I am using the DataContractSerializer to persist objects. Most classes have the [DataContract] attribute but one (not sure why) has [Serializable]. The properties of this class have the [DataMember] attribute. So far, it has worked ok.
Recently, I tried to add a new property but get an error when deserialising. The error message is something like: 'property Notes was expected but found property ModuleNr'.
I tried adding the [DataContract] attribute but got a different error like: 'Deserialised object with id 15 not found'. It seems that the class is not being deserialised at all.
Can anyone explain what I am doing wrong and what I can do to deserialise the existing objects.
Thanks.
Is it the case that the object that was persisted was using the older class structure, and the property you added is a required property, causing the deserialization to fail?
Also, if the Serializable property is there and you are not sure why its there, it might be a good idea to verify if some other code is not using this class to serialize - if it is, the new property you are adding might break it :)
Hope this helps!

vb.net get/set Properties without logic

In many of the articles I have read on the web say that when creating properties in vb.net, they should use the get/set methods and a private/protected member variable in the class.
Like so:
Public Class Person
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
end class
If there is no logic in the get/set of the property, why wouldn't one write that same property like this:
Public class Person
Public Property Name as string
end class
Is this because properties were intended to just be accessors into the class from outside and you would store the variable in the class?
The reason is that these guidelines and tutorials were published before VB.NET 4.0 came out. There’s no other reason not to use automatically implemented properties.
While Konrad has it spot on, I'll add that being a tutorial, educating the student on how properties work is more important than shortcut implementation. A more modern tutorial should show the expanded code, then the shortcut.
Ultimately, this depends on the tutorial, whether it's about programming fundamentals and methodology, or about a specific feature.
Legacy Tutorials before this was a feature. The auto-implemented properties notation gives you a terse way of accomplishing consistency of access to your class

No parameterless constructer defined for this object when putting EnabledDelete=true on LinqDataSource

If I have a LinqDataSource without EnabledDelete, EnabledUpdate, EnabledInsert, it works fine, but as soon as I add those properties to the data source, I get the error:
No parameterless constructor defined for this object.
Here is an answer that helped me solve the issue from the MSDN forums:
LinqDataSource requires a default constructor on the DataContext. If you are working in a web application or website project, the Linq to SQL designer should have created a default constructor and connection string for you when you dragged tables from the database onto the design surface.
Did you create your DataContext and drag tables onto the design surface from a webapp or website project? Open the Lib.NorthwindDataContext class that was generated and see if it has the default constructor.
If you really want, you could also use LinqDataSource without the default constructor by handling the ContextCreating event and providing your own context instance.
As the error indicates, you need to provide a parameterless constructor for the class.
public class MyClass
{
public MyClass()
{
// This is the parameterless constructor
}
// rest of the class members goes here.
}
The system requires a parameterless constructor when it is required to create instances of a class automatically. It cannot determine the meaning of the parameters of your other constructors so it depends on this constructor.
Even if your constructor does nothing it will still work, though you may want it to provide useful defaults for your class properties.

is it possible to return an array of object Interface's rather than an object?

I have an interface that describes an specialized list in my application...
Public Interface IAlphabeticListItem
Property StartingLetter() As String
Property DetailsList() As Generic.List(Of DetailsData)
End Interface
Multiple objects implement this interface, but I want to return them in a webservice not as the underlying object, but as the interface.
<WebMethod()> _
Public Function GetCategoryList(...) As Generic.List(Of IAlphabeticListItem)
...
End Function
Sadly, this causes an exception
Cannot serialize interface IAlphabeticListItem.
Exception Details: System.NotSupportedException: Cannot serialize interface IAlphabeticListItem.
Is there a way to make an interface serializable, or am I going to have to convert each of my objects into a concrete class implementing the interface, and then return that class?
Yes and no. You cannot directly expose the generic class for XML serialization because it's not supported. You can however expose a non-generic interface on the same collection and serialize that. This blog goes into great detail on how to make this work
http://srtsolutions.com/blogs/billwagner/archive/2006/11/20/xml-serialization-and-generic-interfaces.aspx
I don't have time to test it right now, but in C# you can extend interfaces, so I suppose you can do that in VB.NET.
Just make IAlphabeticListItem extend ISerializable