VB代码运行不同于C#代码 - 为什么? [英] VB code runs different than C# code - Why?

查看:66
本文介绍了VB代码运行不同于C#代码 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正常工作的C#类。

我把它翻译成VB,现在运行方式不同。


C#类评估公共属性然后执行MyBase.New。

因此首先设置默认值,然后MyBase.New读取新值(如果

它们存在。)


VB代码反向执行。

它会在设置

默认值时覆盖MyBase.New然后覆盖新值。


有人可以解释一下发生了什么吗?

VB类看起来像这样:

Public Class MyConfig


继承SomeConfiguration


Public Sub New()


MyBase.New(" SomeStuff")


End Sub


Public MyAddress As String =""


Public MyName As String =""


结束班


================== ================================ =========

C#版本是这样的:

公共类MyConfig:SomeConfiguration


{


public MyConfig( ):base(SomeStuff)


{


}

public string MyAddress ="" ;;


公共字符串StoreName ="" ;;


}

-

Joe Fallon

解决方案

Joe,

根据您的样本,您的描述不会合理? MyAddress&

MyName似乎是派生类中的字段,基类

如何设置它们?是否依赖于Reflection或其他东西设置它们?

你的意思是Fields而不是属性吗?


关于行为本身:我记得有关于

的一两次讨论在第一次.NET测试期间或者在测试结束后不久。基本上两个

实例&静态构造函数在C#&

VB.NET之间的行为略有不同。哪一个是正确的,真的很难说。您可以尝试

groups.google.com,但是我不确定您会开始寻找什么。

C#类评估公共属性然后执行MyBase.New。
公共财产在哪里?在你的例子中,我只看到字段,并且

你是正确的C#首先设置字段,然后调用基数,而VB.NET

先调用基数,然后设置字段。

因此首先设置默认值,然后MyBase.New读入新值
(如果它们存在。)
这似乎是向后。对我来说,C#部分构造一个对象,然后

调用基础构造函数,然后完成构造派生对象?

VB代码反向执行。
它会激活MyBase.New,然后在设置
默认值时覆盖新值。
这对我来说似乎是正确的,VB.NET确保在构造派生对象之前完全构造基类是



哪个一个是真正正确的是非常必须开放的辩论,我不参加
计划参加! : - |


正如我所说的那样,一两年前就进行过讨论,但是我不是没有b $ b有任何链接方便。


希望这有帮助

Jay


" Joe Fallon" < JF ****** @ nospamtwcny.rr.com>在消息中写道

news:ux ************** @ TK2MSFTNGP11.phx.gbl ...我有一个正常工作的C#类。
我把它翻译成VB,现在运行方式不同。

C#类评估Public属性,然后执行MyBase.New。
因此首先设置默认值,然后MyBase.New读入新值
(如果存在的话。)

VB代码反向执行。
它会激活MyBase.New,然后在设置
默认值。

有人可以解释一下发生了什么吗?

VB类看起来像这样:
Public Class MyConfig

继承SomeConfiguration

Public Sub New()

MyBase.New(SomeStuff)

End Sub

Public MyAddress As String =""

Public MyName As String =""

End Class

====== ============================================ ====== ===
C#版本是这样的:
公共MyConfig():base(SomeStuff)

{

}

public string MyAddress ="" ;;

public string StoreName ="" ;;

}
-
Joe Fallon



Jay,

你的术语是(当然) correct.you是正确的C#首先设置字段

,然后调用基数,而VB.NET

先调用基数,然后设置字段。


我所谓的公共属性实际上是Public Fields。 (我的错误。)


是的。 Base类使用反射来设置它们。


你是正确的C#首先设置字段,然后调用基数,而VB.NET

首先调用基数,然后设置字段。


好​​的。它们的执行方式不同。


现在问题是:如何使VB代码像C#代码一样?

在这种情况下,顺序活动很重要。


-

Joe Fallon


" Jay B. Harlow [MVP - Outlook ]" < JA ************ @ msn.com>在消息中写道

新闻:Ox **************** @ TK2MSFTNGP12.phx.gbl ... Joe,
根据您的样本,你的描述没有意义? MyAddress&
MyName似乎是派生类中的字段,基类如何设置它们?依赖于反射的基础还是设置它们的东西?
你的意思是菲尔兹而不是属性?

关于行为本身:我记得有一两个讨论
在第一次.NET测试期间或者在测试结束后不久。基本上
两个实例&静态构造函数在C#&
VB.NET之间的行为略有不同。哪一个是正确的,真的很难说。您可以尝试
groups.google.com,但是我不确定您会开始寻找什么。

C#类评估公共属性然后执行
MyBase.New。

公共属性在哪里?在你的例子中,我只看到字段,并且


因此首先设置默认值,然后MyBase.New读取新值
(如果

它们存在。)


这似乎是向后的。对我来说,C#部分构造一个对象,然后调用基础构造函数,然后完成构造派生对象?

VB代码反向执行。
它会占用MyBase.New,然后在设置



默认值时覆盖新值。


这对我来说似乎是正确的,VB。 NET确保在构造派生对象之前完全构造基类。

哪一个是真正正确的。是非常必须开放的辩论,我不打算参与! : - |

正如我所说的那样,一两年前就进行了一次讨论,但我并不是很方便。

希望这有助于杰伊

乔法伦 < JF ****** @ nospamtwcny.rr.com>在消息中写道
新闻:ux ************** @ TK2MSFTNGP11.phx.gbl ...

我有一个正常工作的C#类。
我把它翻译成VB,现在运行方式不同。

C#类评估Public属性,然后执行
MyBase.New。因此首先设置默认值,然后MyBase.New读取新值
(如果

它们存在。)

VB代码执行它相反。
它可以使用MyBase.New然后在设置
默认值时覆盖新值。

有人可以解释一下发生了什么吗?
< VB类看起来像这样:
Public Class MyConfig

继承SomeConfiguration

公共子新()

MyBase。新的(SomeStuff)

End Sub

Public MyAddress As String =""

Public MyName As String ="" ;

结束课

=============================== =================== =========
C#版本是这样的:
公共类MyConfig:SomeConfiguration

公共MyConfig():base(SomeStuff)



$
b $ b公共字符串MyAddress ="" ;;

公开string StoreName ="";

}
- Joe Fallon




周杰伦,

我把这项工作搞砸了。

似乎做我想要的是在初始化时的值基础

级查找它。


PropertyGet检查mStoreName是否为Nothing,因为它将是

Nothing在调用MyBase期间,它初始化它并返回

default然后可以在MyBase中使用的值。

私有mStoreName为字符串


公共属性StoreName()为字符串


获取


如果IsNothing(mStoreName)那么


mStoreName =" MyStore"


结束如果


返回mStoreName


结束获取


设置(ByVal值为字符串)


mStoreName =价值


结束套件


结束物业

-

Joe Fallon

" Joe Fallon" < JF ****** @ nospamtwcny.rr.com>写在消息

新闻:OU ************** @ TK2MSFTNGP12.phx.gbl ...

Jay,你的术语(当然)是正确的。你是正确的C#首先设置字段,然后调用基数,而VB.NET

先调用基数,然后设置字段。
我所谓的Public Properties实际上是Public Fields。 (我的错误。)

是的。 Base类使用反射来设置它们。

你是正确的C#首先设置字段,然后调用基础,而



VB.NET首先调用基数,然后设置字段。

好的。它们的执行方式不同。

现在问题是:如何使VB代码像C#代码一样?
在这种情况下,事件序列很重要。

-
Joe Fallon

Jay B. Harlow [MVP - Outlook]" < JA ************ @ msn.com>在消息中写道
新闻:Ox **************** @ TK2MSFTNGP12.phx.gbl ...

Joe,
基于在您的样本上,您的描述没有意义? MyAddress&
MyName似乎是派生类中的字段,基类如何设置它们?依赖于反射的基础还是设置
的东西?

你的意思是字段而不是属性吗?

行为本身:我记得在第一次.NET测试期间或者在测试结束后不久,有一两次关于

的讨论


。基本上


instance&静态构造函数在C#&
VB.NET之间的行为略有不同。哪一个是正确的,真的很难说。您可以尝试
groups.google.com,但是我不确定您将开始查找
for。

C#类评估公共属性然后执行MyBase .New。

公共属性在哪里?在你的例子中,我只看到字段,




因此首先设置默认值,然后MyBase.New读取在新的


值中(如果

它们存在。)


这似乎是向后的。对我来说,C#部分构造一个对象,
然后调用基础构造函数,然后完成构造派生的
对象?

VB代码反向执行。
它会占用MyBase.New,然后在设置默认值时覆盖新值。


这对我来说似乎是正确的,VB.NET确保基类完全构建在它之前构造派生对象。

哪一个是真正的正确的是非常必须开放的辩论,我不打算参与! : - |

正如我所说,一两年前有一次讨论,但是我没有b $ b没有任何链接方便。

希望这有助于杰伊

乔法伦 < JF ****** @ nospamtwcny.rr.com>在消息中写道
新闻:ux ************** @ TK2MSFTNGP11.phx.gbl ...

我有一个正常工作的C#类。
我将它翻译成VB,现在运行方式不同。

C#类评估Public属性,然后执行MyBase.New。因此首先设置默认值,然后MyBase.New读入新的


值(如果

它们存在。)

VB代码执行它反之。
它可以解决MyBase.New,然后在设置


默认值时覆盖新值。

有人可以解释一下是什么继续?

VB类看起来像这样:
Public Class MyConfig

继承SomeConfiguration

Public Sub New()

MyBase.New(SomeStuff)

End Sub

Public MyAddress As String =""

Public MyName As String =""

End Class

======================= =========================== =========
C#版本是这样的:
公共课MyConfig:SomeConfiguration

公共MyConfig():base(SomeStuff)



/>}

公共字符串MyAddress ="& ;;

公共字符串StoreName ="" ;;

}
-
Joe Fallon





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

解决方案

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



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




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





这篇关于VB代码运行不同于C#代码 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆