473,486 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB code runs different than C# code - Why?

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 = "";

}
--
Joe Fallon

Nov 20 '05 #1
16 1406
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
you are correct C# sets the fields first, then calls the base, while VB.NET
calls the base first, then sets the fields.
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl... 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 = "";

}
--
Joe Fallon

Nov 20 '05 #2
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Ox****************@TK2MSFTNGP12.phx.gbl... 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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
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 = "";

}
--
Joe Fallon


Nov 20 '05 #3
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Ox****************@TK2MSFTNGP12.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
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 = "";

}
--
Joe Fallon



Nov 20 '05 #4
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Ox****************@TK2MSFTNGP12.phx.gbl...
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

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ux**************@TK2MSFTNGP11.phx.gbl...
> 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 = "";
>
> }
> --
> Joe Fallon
>
>
>



Nov 20 '05 #5
Jay,
You're a genius!

I just re-visited the problem and got stuck using a Boolean field.
My IsNothing hack failed in that case.

I checked here and you came up with a better all-around solution which is to
use
<DefaultValue("")> _
Public MyAddress As String

This works MUCH better.
A lot less code and the default is not used later to overwrite the value I
set during the reflection process
which was the problem when I used Public MyAddress As String = "Some
String"

Thank you so much!
--
Joe Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Ox****************@TK2MSFTNGP12.phx.gbl...
> 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
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:ux**************@TK2MSFTNGP11.phx.gbl...
> > 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 = "";
> >
> > }
> > --
> > Joe Fallon
> >
> >
> >
>
>



Nov 20 '05 #6
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null Reference
exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Ox****************@TK2MSFTNGP12.phx.gbl...
> 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
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:ux**************@TK2MSFTNGP11.phx.gbl...
> > 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 = "";
> >
> > }
> > --
> > Joe Fallon
> >
> >
> >
>
>



Nov 20 '05 #7
Why not use your C# class in your VB.Net application? Is there some reason
you don't want to?

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null Reference exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
> 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
>
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message
> news:Ox****************@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> > "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> > news:ux**************@TK2MSFTNGP11.phx.gbl...
> > > 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 = "";
> > >
> > > }
> > > --
> > > Joe Fallon
> > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #8
Just educational at this point!
--
Joe Fallon
"Brian" <no****@prairie.lakes.com> wrote in message
news:10*************@corp.supernews.com...
Why not use your C# class in your VB.Net application? Is there some reason
you don't want to?

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null Reference
exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
> 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
>
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:OU**************@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> >
> >
> > "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message
> > news:Ox****************@TK2MSFTNGP12.phx.gbl...
> > > 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
> > >
> > > "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> > > news:ux**************@TK2MSFTNGP11.phx.gbl...
> > > > 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 = "";
> > > >
> > > > }
> > > > --
> > > > Joe Fallon
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #9
Joe,
attribute using reflection. Right now it is coming up with a Null Reference exception because it does not know what that value is. What is coming up with a Null Reference exception?
- your base constructor
- your derived constructor
- your derived classes code

Simply adding the DefaultValue to your class will not do anything.

You will need to add code to your base constructor to look for the attribute
and act accordingly.

You can use the MemberInfo.GetCustomAttributes method to get the list of
custom attributes for a member (field or property).

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl... Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null Reference exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
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" <jf******@nospamtwcny.rr.com> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OU**************@TK2MSFTNGP12.phx.gbl...
> 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
>
>
>
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in

message
> news:Ox****************@TK2MSFTNGP12.phx.gbl...
> > 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
> >
> > "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> > news:ux**************@TK2MSFTNGP11.phx.gbl...
> > > 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 = "";
> > >
> > > }
> > > --
> > > Joe Fallon
> > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #10
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote...
You will need to add code to your base constructor to look for the attribute and act accordingly.


I thought I'd tag this on to this thread but any thread you respond to would
do... Jay is there a place I can vote for your getting an MVP++ somewhere?
You always answer clearly and as far as I can see you continue to follow up
until the person gets it.

I just wanted to say thanks for all your help in this newsgroup.
Tom
Nov 20 '05 #11
Joe,
Have you tried something like:

Public Class JoeFallon

Public Shared Sub Main()
Dim config As New MyConfig
Debug.WriteLine(config.MyName, "MyName")
Debug.WriteLine(config.MyAddress, "MyAddress")
End Sub

End Class

Public MustInherit Class SomeConfiguration

Protected Sub New(ByVal arg As String)
Dim type As Type = Me.GetType()
For Each field As FieldInfo In type.GetFields()
For Each attribute As DefaultValueAttribute In
field.GetCustomAttributes(GetType(DefaultValueAttr ibute), False)
field.SetValue(Me, attribute.Value)
Next
Next
End Sub

End Class

Public Class MyConfig
Inherits SomeConfiguration

Public Sub New()
MyBase.New("Something")
End Sub

<DefaultValue("Name")> _
Public MyName As String

<DefaultValue("Address")> _
Public MyAddress As String

End Class

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null Reference exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon

<<snip>>
Nov 20 '05 #12
Hi Tom,
I agree 100%.

Whenever I post a question here I always hope Jay gets to it first!

What I can't figure out is why is he an Outlook MVP instead of VB.Net??
--
Joe Fallon
Access MVP

"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote...
You will need to add code to your base constructor to look for the attribute
and act accordingly.


I thought I'd tag this on to this thread but any thread you respond to

would do... Jay is there a place I can vote for your getting an MVP++ somewhere?
You always answer clearly and as far as I can see you continue to follow up until the person gets it.

I just wanted to say thanks for all your help in this newsgroup.
Tom

Nov 20 '05 #13
Jay,
I will take a look at this suggestion later.
I tried to get it to work for many hours yesterday and failed too many times
to count.

Every time I took a step forward, I ended up taking 2 backwards.

GetCustomAttributes worked when I used a Property but not when it was a
Field.
(Or maybe the way I used it was what failed.)

This was very frustrating since it worked perfectly in C# and I just wanted
to translate the code toVB.
(I didn't write the C# code - if I did, I probably wouldn't be struggling
with some of these concepts!)
--
Joe Fallon


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uq****************@TK2MSFTNGP12.phx.gbl...
Joe,
Have you tried something like:

Public Class JoeFallon

Public Shared Sub Main()
Dim config As New MyConfig
Debug.WriteLine(config.MyName, "MyName")
Debug.WriteLine(config.MyAddress, "MyAddress")
End Sub

End Class

Public MustInherit Class SomeConfiguration

Protected Sub New(ByVal arg As String)
Dim type As Type = Me.GetType()
For Each field As FieldInfo In type.GetFields()
For Each attribute As DefaultValueAttribute In
field.GetCustomAttributes(GetType(DefaultValueAttr ibute), False)
field.SetValue(Me, attribute.Value)
Next
Next
End Sub

End Class

Public Class MyConfig
Inherits SomeConfiguration

Public Sub New()
MyBase.New("Something")
End Sub

<DefaultValue("Name")> _
Public MyName As String

<DefaultValue("Address")> _
Public MyAddress As String

End Class

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null

Reference
exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon

<<snip>>

Nov 20 '05 #14
Joe,
It may help if you could share what you have, at least enough to show the
problem. Then I or someone else may be able to identify quicker the change
you need to make...

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Jay,
I will take a look at this suggestion later.
I tried to get it to work for many hours yesterday and failed too many times to count.

Every time I took a step forward, I ended up taking 2 backwards.

GetCustomAttributes worked when I used a Property but not when it was a
Field.
(Or maybe the way I used it was what failed.)

This was very frustrating since it worked perfectly in C# and I just wanted to translate the code toVB.
(I didn't write the C# code - if I did, I probably wouldn't be struggling
with some of these concepts!)
--
Joe Fallon


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uq****************@TK2MSFTNGP12.phx.gbl...
Joe,
Have you tried something like:

Public Class JoeFallon

Public Shared Sub Main()
Dim config As New MyConfig
Debug.WriteLine(config.MyName, "MyName")
Debug.WriteLine(config.MyAddress, "MyAddress")
End Sub

End Class

Public MustInherit Class SomeConfiguration

Protected Sub New(ByVal arg As String)
Dim type As Type = Me.GetType()
For Each field As FieldInfo In type.GetFields()
For Each attribute As DefaultValueAttribute In
field.GetCustomAttributes(GetType(DefaultValueAttr ibute), False)
field.SetValue(Me, attribute.Value)
Next
Next
End Sub

End Class

Public Class MyConfig
Inherits SomeConfiguration

Public Sub New()
MyBase.New("Something")
End Sub

<DefaultValue("Name")> _
Public MyName As String

<DefaultValue("Address")> _
Public MyAddress As String

End Class

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
Jay,
It is not working quite right yet.

<DefaultValue("")> _

As you stated my base class needs to evaluate the <DefaultValue("")> _
attribute using reflection. Right now it is coming up with a Null

Reference
exception because it does not know what that value is.

Do you have some sample VB code for how to do that?
Thanks.
--
Joe Fallon

<<snip>>


Nov 20 '05 #15
Just a quick note for any lurkers.
Jay was kind enough to review my code, re-write a chunk of it so it worked
<g>, and send it back to me.
Of course, it works perfectly!

Jay - thanks again for all your help!
--
Joe Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:en****************@TK2MSFTNGP09.phx.gbl...
Joe,
It may help if you could share what you have, at least enough to show the
problem. Then I or someone else may be able to identify quicker the change
you need to make...

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Jay,
I will take a look at this suggestion later.
I tried to get it to work for many hours yesterday and failed too many

times
to count.

Every time I took a step forward, I ended up taking 2 backwards.

GetCustomAttributes worked when I used a Property but not when it was a
Field.
(Or maybe the way I used it was what failed.)

This was very frustrating since it worked perfectly in C# and I just

wanted
to translate the code toVB.
(I didn't write the C# code - if I did, I probably wouldn't be struggling with some of these concepts!)
--
Joe Fallon


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:uq****************@TK2MSFTNGP12.phx.gbl...
Joe,
Have you tried something like:

Public Class JoeFallon

Public Shared Sub Main()
Dim config As New MyConfig
Debug.WriteLine(config.MyName, "MyName")
Debug.WriteLine(config.MyAddress, "MyAddress")
End Sub

End Class

Public MustInherit Class SomeConfiguration

Protected Sub New(ByVal arg As String)
Dim type As Type = Me.GetType()
For Each field As FieldInfo In type.GetFields()
For Each attribute As DefaultValueAttribute In
field.GetCustomAttributes(GetType(DefaultValueAttr ibute), False)
field.SetValue(Me, attribute.Value)
Next
Next
End Sub

End Class

Public Class MyConfig
Inherits SomeConfiguration

Public Sub New()
MyBase.New("Something")
End Sub

<DefaultValue("Name")> _
Public MyName As String

<DefaultValue("Address")> _
Public MyAddress As String

End Class

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
> Jay,
> It is not working quite right yet.
>
> <DefaultValue("")> _
>
> As you stated my base class needs to evaluate the <DefaultValue("")> _ > attribute using reflection. Right now it is coming up with a Null
Reference
> exception because it does not know what that value is.
>
> Do you have some sample VB code for how to do that?
> Thanks.
> --
> Joe Fallon
>
>
<<snip>>



Nov 20 '05 #16
Lurkers,
Important note: I only look at code sent privately that I requested to see!

Thanks for understanding!

Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
Just a quick note for any lurkers.
Jay was kind enough to review my code, re-write a chunk of it so it worked
<g>, and send it back to me.
Of course, it works perfectly!

Jay - thanks again for all your help!
--
Joe Fallon

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:en****************@TK2MSFTNGP09.phx.gbl...
Joe,
It may help if you could share what you have, at least enough to show the
problem. Then I or someone else may be able to identify quicker the change you need to make...

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Jay,
I will take a look at this suggestion later.
I tried to get it to work for many hours yesterday and failed too many times
to count.

Every time I took a step forward, I ended up taking 2 backwards.

GetCustomAttributes worked when I used a Property but not when it was a Field.
(Or maybe the way I used it was what failed.)

This was very frustrating since it worked perfectly in C# and I just

wanted
to translate the code toVB.
(I didn't write the C# code - if I did, I probably wouldn't be

struggling with some of these concepts!)
--
Joe Fallon


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:uq****************@TK2MSFTNGP12.phx.gbl...
> Joe,
> Have you tried something like:
>
> Public Class JoeFallon
>
> Public Shared Sub Main()
> Dim config As New MyConfig
> Debug.WriteLine(config.MyName, "MyName")
> Debug.WriteLine(config.MyAddress, "MyAddress")
> End Sub
>
> End Class
>
> Public MustInherit Class SomeConfiguration
>
> Protected Sub New(ByVal arg As String)
> Dim type As Type = Me.GetType()
> For Each field As FieldInfo In type.GetFields()
> For Each attribute As DefaultValueAttribute In
> field.GetCustomAttributes(GetType(DefaultValueAttr ibute), False)
> field.SetValue(Me, attribute.Value)
> Next
> Next
> End Sub
>
> End Class
>
> Public Class MyConfig
> Inherits SomeConfiguration
>
> Public Sub New()
> MyBase.New("Something")
> End Sub
>
> <DefaultValue("Name")> _
> Public MyName As String
>
> <DefaultValue("Address")> _
> Public MyAddress As String
>
> End Class
>
> Hope this helps
> Jay
>
> "Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
> news:ef**************@TK2MSFTNGP10.phx.gbl...
> > Jay,
> > It is not working quite right yet.
> >
> > <DefaultValue("")> _
> >
> > As you stated my base class needs to evaluate the
<DefaultValue("")> _ > > attribute using reflection. Right now it is coming up with a Null
> Reference
> > exception because it does not know what that value is.
> >
> > Do you have some sample VB code for how to do that?
> > Thanks.
> > --
> > Joe Fallon
> >
> >
> <<snip>>
>
>



Nov 20 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
13111
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
2456
by: Neil Sargent | last post by:
I have a generic code library, developed as an MDA and distributed as an MDE. It is developed using my system workgroup file. I have a number of applications which use the code library. One of...
20
1711
by: vb | last post by:
hi all, I have to compare an address of structure with an absolute address(this address signifies the start of a memory component). Now my question is the follwong code leagal:- #include...
19
2366
by: Dave | last post by:
I'm building a research application that needs to be a super speed demon. I decided that one way to do this is to use goto loops instead of while() loops when I need them. that way, instead of...
13
1549
by: Chip | last post by:
I have a dual-threaded app that runs just as it's supposed to under Casini. However, when I run the exact same code undier IIS (ASP.NET 2.0) on the same machine, it is totally unpredictable and...
1
1737
by: jeeji | last post by:
HI I am experiencing something funny here where I have two equivalent queries: If I run them through query analyzer, the first runs slower than the second, but if run through C# application, the...
0
1382
by: Nogusta123 | last post by:
My web service has a single method called "CheckEligibility". This method can be used to check a person's eligibility for multiple products. Each product has different data requiements so each...
2
1251
by: Bart | last post by:
Hi, I ran the same code with two different providers (oledb abd sqlclient), and i got two different behaviours. The code with OLEDB runs perfect without error. The same code with SQLClient...
8
2399
by: Henri.Chinasque | last post by:
Hi all, I am wondering if there are any quick/efficient ways to look at a piece of c++ code and determine if it would run faster if it was compiled to native code. I ask this because all of my...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4863
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.