VB.NET-101构造函数和方法 [英] VB.NET-101 Constructors and Methods

查看:87
本文介绍了VB.NET-101构造函数和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找出什么是必要的/可选的,我做了一个非常简单的

类和模块组合来添加两个数字。 (见下文)


看来需要一个空的构造函数才能正常工作,

虽然我完全看不出它是做什么的除了第二个构造函数之外。


此外,该示例在构造函数中没有消息调用时工作正常

(数字答案仍然存在且正确!)。

我执行它不再工作并返回构建或运行错误。

请解释,


感谢您的帮助,

John


***************

Public Class CSum


公共mSum为整数


公共子新()

''总和(0,0)

End Sub


Public Sub New(ByVal firstNumber As Integer,ByVal lastNumber As Integer)

''Sum(firstNumber,lastNumber)

结束子


公共子总和(ByVal firstNumber为整数,ByVal lastNumber为整数)

mSum = firstNumber + lastNumber
End Sub

结束班级


模块模块1


Sub Main()

将thisFirstNumber调整为整数= 10

将此LastLastNumber调整为整数= 100


将thisSum视为新CSum


thisSum.Sum(thisFirstNumber,thisLastNumber)

Console.WriteLine(" {0}和{1} = {2}",_
thisFirstNumber,thisLastNumber,thisSum.mSum)


End Sub


结束模块

Trying to find out what is essential / optional, I made an extremely simple
Class and Module combination to add two numbers. (see below)

It appears that an empty constructor is needed n order to work right,
although I quite don''t see what is does in addition to the 2nd constructor.

Also, the example works fine without message calls in either constructor
(the numerical answer is still there and correct!).
I exected it to no longer work and return build or run errors.
Please explain,

Thanks for your help,
John

***************
Public Class CSum

Public mSum As Integer

Public Sub New()
''Sum(0, 0)
End Sub

Public Sub New(ByVal firstNumber As Integer, ByVal lastNumber As Integer)
''Sum(firstNumber, lastNumber)
End Sub

Public Sub Sum(ByVal firstNumber As Integer, ByVal lastNumber As Integer)
mSum = firstNumber + lastNumber
End Sub

End Class

Module Module1

Sub Main()

Dim thisFirstNumber As Integer = 10
Dim thisLastNumber As Integer = 100

Dim thisSum As New CSum

thisSum.Sum(thisFirstNumber, thisLastNumber)

Console.WriteLine("The sum of {0} and {1} = {2}", _
thisFirstNumber, thisLastNumber, thisSum.mSum)

End Sub

End Module

推荐答案

如果你不想用2个参数调用那个,那么需要一个空的。

你宣布了另一个构造函数,但你是不使用。现在它在那里是
,如果你想使用一个空的构造函数,你必须添加它(因为你需要
)。


我不知道为什么你预料到错误。你调用了Sum方法。然后你取得了b
$ b检测结果 - 这对我来说很有意义。


" John" <乔** @ discussions.microsoft.com>在消息中写道

新闻:2B ********************************** @ microsof t.com ...
An empty one is needed if you don''t want to call the one with 2 paramters.
You declared the other constructor, but you are not using. Now that it''s
there, if you want to use an empty constructor, you havet o add it (as you
did).

I''m not sure why you expected errors. You called the Sum method. Then you
retrieved the result - makes sense that it shoudl all work to me.

"John" <Jo**@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
试图找出什么是必要的/可选的,我做了一个非常简单的类和模块组合来添加两个数字。 (见下文)

为了正常工作,似乎需要一个空的构造函数,
虽然除了第二个之外我还没看到它做什么
构造函数。

此外,该示例在构造函数中没有消息调用的情况下工作正常
(数字答案仍然存在并且正确!)。
我将其解释为不再工作返回构建或运行错误。
请解释,

感谢您的帮助,
John

*********** ****
Public Class CSum

Public mSum as Integer

Public Sub New()
''Sum(0,0)
Public Sub New(ByVal firstNumber As Integer,ByVal lastNumber As
Integer)
''Sum(firstNumber,lastNumber)
End Sub

Public Sub Sum(ByVal firstNumber As Integer,ByVal lastNumber As
Integer)
mSum = firstNumber + lastNumber
End Sub

结束类

模块模块1

Sub Main()

将thisFirstNumber调整为整数= 10
将此LastNastNumber调整为整数= 100

将此作为新CSum调暗

thisSum .Sum(thisFirstNumber,thisLastNumber)

Console.WriteLine(" {0}和{1} = {2}"的总和,_
thisFirstNumber,thisLastNumber,thisSum.mSum )

End Sub

结束模块
Trying to find out what is essential / optional, I made an extremely
simple
Class and Module combination to add two numbers. (see below)

It appears that an empty constructor is needed n order to work right,
although I quite don''t see what is does in addition to the 2nd
constructor.

Also, the example works fine without message calls in either constructor
(the numerical answer is still there and correct!).
I exected it to no longer work and return build or run errors.
Please explain,

Thanks for your help,
John

***************
Public Class CSum

Public mSum As Integer

Public Sub New()
''Sum(0, 0)
End Sub

Public Sub New(ByVal firstNumber As Integer, ByVal lastNumber As
Integer)
''Sum(firstNumber, lastNumber)
End Sub

Public Sub Sum(ByVal firstNumber As Integer, ByVal lastNumber As
Integer)
mSum = firstNumber + lastNumber
End Sub

End Class

Module Module1

Sub Main()

Dim thisFirstNumber As Integer = 10
Dim thisLastNumber As Integer = 100

Dim thisSum As New CSum

thisSum.Sum(thisFirstNumber, thisLastNumber)

Console.WriteLine("The sum of {0} and {1} = {2}", _
thisFirstNumber, thisLastNumber, thisSum.mSum)

End Sub

End Module



我注释掉函数调用两个构造函数。在我看过目前为止的书中的所有示例中,使用了X参数的方法,在每个构造函数中放置了带有X参数的方法(除非可选)


我现在明白这些消息来电并不总是需要吗?

什么时候需要,什么时候不需要?


谢谢为了你的帮助。

John


" Marina"写道:
I "commented out" the functions calls in both constructors. In all examples
in my book seen so far, where a method with X parameters was used, method
calls with X arguments (unless optional) were placed in each constructor.
Do I now understand that such message calls are not always needed?
When are they needed and when not?

Thanks for your help.
John

"Marina" wrote:
如果你不想用2个参数调用那个,那么需要一个空的。
你宣布了另一个构造函数,但你没有使用。现在它就在那里,如果你想使用一个空构造函数,你必须添加它(就像你所做的那样)。

我不确定为什么你预料到错误。你调用了Sum方法。然后你检索了结果 - 让我觉得它对我起作用了。

John <乔** @ discussions.microsoft.com>在消息中写道
新闻:2B ********************************** @ microsof t.com。 ..
An empty one is needed if you don''t want to call the one with 2 paramters.
You declared the other constructor, but you are not using. Now that it''s
there, if you want to use an empty constructor, you havet o add it (as you
did).

I''m not sure why you expected errors. You called the Sum method. Then you
retrieved the result - makes sense that it shoudl all work to me.

"John" <Jo**@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
试图找出什么是必要的/可选的,我做了一个非常简单的类和模块组合来添加两个数字。 (见下文)

为了正常工作,似乎需要一个空的构造函数,
虽然除了第二个之外我还没看到它做什么
构造函数。

此外,该示例在构造函数中没有消息调用的情况下工作正常
(数字答案仍然存在并且正确!)。
我将其解释为不再工作返回构建或运行错误。
请解释,

感谢您的帮助,
John

*********** ****
Public Class CSum

Public mSum as Integer

Public Sub New()
''Sum(0,0)
Public Sub New(ByVal firstNumber As Integer,ByVal lastNumber As
Integer)
''Sum(firstNumber,lastNumber)
End Sub

Public Sub Sum(ByVal firstNumber As Integer,ByVal lastNumber As
Integer)
mSum = firstNumber + lastNumber
End Sub

结束类

模块模块1

Sub Main()

将thisFirstNumber调整为整数= 10
将此LastNastNumber调整为整数= 100

将此作为新CSum调暗

thisSum .Sum(thisFirstNumber,thisLastNumber)

Console.WriteLine(" {0}和{1} = {2}"的总和,_
thisFirstNumber,thisLastNumber,thisSum.mSum )

End Sub

结束模块
Trying to find out what is essential / optional, I made an extremely
simple
Class and Module combination to add two numbers. (see below)

It appears that an empty constructor is needed n order to work right,
although I quite don''t see what is does in addition to the 2nd
constructor.

Also, the example works fine without message calls in either constructor
(the numerical answer is still there and correct!).
I exected it to no longer work and return build or run errors.
Please explain,

Thanks for your help,
John

***************
Public Class CSum

Public mSum As Integer

Public Sub New()
''Sum(0, 0)
End Sub

Public Sub New(ByVal firstNumber As Integer, ByVal lastNumber As
Integer)
''Sum(firstNumber, lastNumber)
End Sub

Public Sub Sum(ByVal firstNumber As Integer, ByVal lastNumber As
Integer)
mSum = firstNumber + lastNumber
End Sub

End Class

Module Module1

Sub Main()

Dim thisFirstNumber As Integer = 10
Dim thisLastNumber As Integer = 100

Dim thisSum As New CSum

thisSum.Sum(thisFirstNumber, thisLastNumber)

Console.WriteLine("The sum of {0} and {1} = {2}", _
thisFirstNumber, thisLastNumber, thisSum.mSum)

End Sub

End Module




对不起,我完全没有关注你的

方法中的参数与你的构造函数有什么关系。它们是完全独立的,并且与b $ b无关。


删除您发布的那个类中的所有构造函数作为示例。

只剩下Sum方法和变量声明。一切都应该工作

罚款。


可能是时候买一本新书了。


" John" <乔** @ discussions.microsoft.com>在留言中写道

news:84 ********************************** @ microsof t.com ...
Sorry, I am not following at all as far as what the parameters in your
methods have to do with your constructors. They are completely separate and
unrelated.

Delete all the constructors in that class you posted as an example. Have
just the Sum method left and variable declaration. Everything should work
fine.

Might be time to get a new book.

"John" <Jo**@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
我注释掉了函数调用两个构造函数。在我到目前为止看到的所有示例中,使用带有X参数的方法,在每个构造函数中放置了带有X参数(除非可选)的方法调用。
我现在明白了吗?是不是总是需要这样的留言?
什么时候需要?什么时候不需要?

感谢您的帮助。
John

Marina ;写道:
I "commented out" the functions calls in both constructors. In all examples
in my book seen so far, where a method with X parameters was used, method
calls with X arguments (unless optional) were placed in each constructor.
Do I now understand that such message calls are not always needed?
When are they needed and when not?

Thanks for your help.
John

"Marina" wrote:
如果你不想用2
参数调用那个,那么需要一个空的。
你宣布了另一个构造函数,但是你没有使用。现在它就在那里,如果你想使用一个空构造函数,你必须添加它(就像
你所做的那样)。

我'我不确定为什么你会预料到错误。你调用了Sum方法。然后你检索了结果 - 让我觉得它对我起作用了。

John <乔** @ discussions.microsoft.com>在消息中写道
新闻:2B ********************************** @ microsof t.com。 ..
An empty one is needed if you don''t want to call the one with 2
paramters.
You declared the other constructor, but you are not using. Now that it''s
there, if you want to use an empty constructor, you havet o add it (as
you
did).

I''m not sure why you expected errors. You called the Sum method. Then you
retrieved the result - makes sense that it shoudl all work to me.

"John" <Jo**@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
>试图找出什么是必要的/可选的,我做了一个非常的
>简单
>类和模块组合添加两个数字。 (见下文)
>
>为了正常工作,似乎需要一个空的构造函数,
>虽然除了第二个之外我还没有看到什么做的
>构造函数。
>
>此外,该示例在没有消息调用的情况下工作正常
>构造函数
> (数字答案仍然存在且正确!)。
>我将其用于不再工作并返回构建或运行错误。
>请解释,
>
>感谢您的帮助,
>约翰
>
> ***************
> Public Class CSum
>
>公共mSum作为整数
>
> Public Sub New()
> ''总和(0,0)
>结束子
>
> Public Sub New(ByVal firstNumber As Integer,ByVal lastNumber As
> Integer)
> ''Sum(firstNumber,lastNumber)
>结束子
>
> Public Sub Sum(ByVal firstNumber As Integer,ByVal lastNumber As
> Integer)
> mSum = firstNumber + lastNumber
>结束子
>
>结束班
>
>模块模块1
>
> Sub Main()
>
>将thisFirstNumber调整为整数= 10
> Dim thisLastNumber As Integer = 100
>
>将这个作为新CSum黯然失色
>
> thisSum.Sum(thisFirstNumber,thisLastNumber)
>
> Console.WriteLine(" {0}和{1} = {2}",_
> thisFirstNumber,thisLastNumber,thisSum.mSum)
>
>结束子
>
>结束模块
> Trying to find out what is essential / optional, I made an extremely
> simple
> Class and Module combination to add two numbers. (see below)
>
> It appears that an empty constructor is needed n order to work right,
> although I quite don''t see what is does in addition to the 2nd
> constructor.
>
> Also, the example works fine without message calls in either
> constructor
> (the numerical answer is still there and correct!).
> I exected it to no longer work and return build or run errors.
> Please explain,
>
> Thanks for your help,
> John
>
> ***************
> Public Class CSum
>
> Public mSum As Integer
>
> Public Sub New()
> ''Sum(0, 0)
> End Sub
>
> Public Sub New(ByVal firstNumber As Integer, ByVal lastNumber As
> Integer)
> ''Sum(firstNumber, lastNumber)
> End Sub
>
> Public Sub Sum(ByVal firstNumber As Integer, ByVal lastNumber As
> Integer)
> mSum = firstNumber + lastNumber
> End Sub
>
> End Class
>
> Module Module1
>
> Sub Main()
>
> Dim thisFirstNumber As Integer = 10
> Dim thisLastNumber As Integer = 100
>
> Dim thisSum As New CSum
>
> thisSum.Sum(thisFirstNumber, thisLastNumber)
>
> Console.WriteLine("The sum of {0} and {1} = {2}", _
> thisFirstNumber, thisLastNumber, thisSum.mSum)
>
> End Sub
>
> End Module




这篇关于VB.NET-101构造函数和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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