Joe,
My only caution would be to disallow Nothing in the property set. Otherwise
if you tried to really set the property to Nothing, it would "fail" as the
Get would then start returning "MyStore"...
If you are already using Reflection, have you considered using an Attribute
on each of the fields/properties that would identify what the "default"
should be, then your base class could use this default attribute instead of
the other value. And you would not need "special" code in VB.NET or
potentially other languages that may use your product?
You could probably even use the System.ComponentModel.DefaultValueAttribute
instead of needing to define your own.
Something like (untested):
Public Class MyConfig
Inherits SomeConfiguration
<DefaultValue("")> _
Public MyAddress As String
End Class
public class MyConfig: SomeConfiguration
{
[DefaultValue("")]
public string MyAddress;
}
Your base constructor would need to check each field/property for the
DefaultValue attribute to know what default value to apply to that
field/property. The derived constructor would leave the fields
"uninitialized" as they were initialized by the base...
Hope this helps
Jay
Joe Fallon said:
Jay,
I hacked up this work around.
It seems to do what I want which is to initialize the value when the Base
class looks for it.
The PropertyGet checks to see if mStoreName Is Nothing, and since it
will
be
Nothing during the call to MyBase, it initializes it and returns the
"default" value which can then be used in MyBase.
Private mStoreName As String
Public Property StoreName() As String
Get
If IsNothing(mStoreName) Then
mStoreName = "MyStore"
End If
Return mStoreName
End Get
Set(ByVal Value As String)
mStoreName = Value
End Set
End Property
--
Joe Fallon
Jay,
Your terminology is (of course) correct.you are correct C# sets the fields
first, then calls the base, while VB.NET
calls the base first, then sets the fields.
What I called Public Properties are in fact Public Fields. (My mistake.)
Yes. The Base class is using reflection to set them.
"you are correct C# sets the fields first, then calls the base, while
VB.NET
calls the base first, then sets the fields."
OK. They ARE executed differently.
So now the question is: How do I make the VB code behave like the C# code?
In this case the sequence of events is important.
--
Joe Fallon
Joe,
Based on your sample, your description does not make sense?
MyAddress
&
MyName appear to be fields in the derived class, how is the base class
setting them? Is the base relying on Reflection or something to set
them?
Did you mean Fields instead of Properties?
As to the behavior itself: I remember there was one or two discussions
about
this during the first .NET beta or shortly after it came out. Basically
both
instance & static constructors behave a little differently between
C#
&
VB.NET. Which one is correct is really hard to say. You could try
groups.google.com, however I'm not sure what you would start looking
for.
The C# class evaluates the Public properties and then executes
MyBase.New.
Where are the public "properties" in your example, I only see fields,
and
So default values are set first and then MyBase.New reads in new
values
(if
they exist.)
This seems "backwards" to me, C# is partially constructing an object,
then
calls the base constructor, then finishes constructing the derived
object?
The VB code does it in reverse.
It excutes MyBase.New and then overwrites the new values when it sets
the
defaults.
This seems correct to me, VB.NET is ensuring that the base class is
completely constructed before it constructs the derived object.
Which one is truly "correct" is very must open to debate, which I
do
not
plan on participating in! :-|
As I stated, there was a discussion a year or two ago, however I don't
not
have any links handy.
Hope this helps
Jay
I have a C# class that works correctly.
I translated it to VB and now it runs differently.
The C# class evaluates the Public properties and then executes
MyBase.New.
So default values are set first and then MyBase.New reads in new
values
(if
they exist.)
The VB code does it in reverse.
It excutes MyBase.New and then overwrites the new values when it sets
the
defaults.
Can someone please explain what is going on?
The VB class looks like this:
Public Class MyConfig
Inherits SomeConfiguration
Public Sub New()
MyBase.New("SomeStuff")
End Sub
Public MyAddress As String = ""
Public MyName As String = ""
End Class
===========================================================
The C# version is like this:
public class MyConfig: SomeConfiguration
{
public MyConfig() : base("SomeStuff")
{
}
public string MyAddress = "";
public string StoreName = "";
}