如何将参数传递给C#中的属性? [英] How to pass arguments to a property in C#?

查看:91
本文介绍了如何将参数传递给C#中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在vb.Net中,可以将参数传递给属性。如何在c#中实现它?下面给出了用于将参数传递给属性getter和setter的vb.net代码..


谢谢,

Prachi


公共属性字段(ByVal strFieldName As String,可选ByVal tableName As String ="",可选ByVal rowNum As Integer = 0)

获取

如果tableName <> ""然后返回objDataSet.Tables(tableName).Rows(rowNum)(strField Name)

返回objDataSet.Tables(objDataSet.Tables.Count - 1).Rows(rowNum)(strFieldName)

结束获取


套装(ByVal价值)

如果tableName<> ""然后

objDataSet.Tables(tableName).Rows(rowNum)(strField Name)= Value

Else

objDataSet.Tables(objDataSet.Tables) .Count - 1).Rows(rowNum)(strFieldName)= Value

结束如果

结束集

结束财产

Hi,

In vb.Net one can pass arguments to properties. How does one achieve it in c#? Given below is the vb.net code for passing arguments to property getters and setters..

Thanks,
Prachi

Public Property Field(ByVal strFieldName As String, Optional ByVal tableName As String = "", Optional ByVal rowNum As Integer = 0)
Get
If tableName <> "" Then Return objDataSet.Tables(tableName).Rows(rowNum)(strField Name)
Return objDataSet.Tables(objDataSet.Tables.Count - 1).Rows(rowNum)(strFieldName)
End Get

Set(ByVal Value)
If tableName <> "" Then
objDataSet.Tables(tableName).Rows(rowNum)(strField Name) = Value
Else
objDataSet.Tables(objDataSet.Tables.Count - 1).Rows(rowNum)(strFieldName) = Value
End If
End Set
End Property

推荐答案

Prachi Dimble< pr **** @ eximiuus.com>写道:
Prachi Dimble <pr****@eximiuus.com> wrote:
在vb.Net中,可以将参数传递给属性。如何在c#中实现它?下面给出了将参数传递给
属性getter和setter的vb.net代码..
In vb.Net one can pass arguments to properties. How does one achieve
it in c#? Given below is the vb.net code for passing arguments to
property getters and setters..




在C#中,你只能将参数传递给被调用的特殊属性

索引器。你需要让你的房产成为你班级的索引。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



In C#, you can only pass parameters to special properties called
indexers. You''ll need to make your property an indexer for your class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




" Jon Skeet [C#MVP ]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Prachi Dimble< pr **** @ eximiuus.com>写道:
Prachi Dimble <pr****@eximiuus.com> wrote:
在vb.Net中,可以将参数传递给属性。如何在c#中实现它?下面给出了用于将参数传递给
属性getter和setter的vb.net代码..
In vb.Net one can pass arguments to properties. How does one achieve
it in c#? Given below is the vb.net code for passing arguments to
property getters and setters..



在C#中,只能将参数传递给名为
的特殊属性索引。您需要让您的酒店成为您班级的索引器。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



In C#, you can only pass parameters to special properties called
indexers. You''ll need to make your property an indexer for your class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




我认为C#对于这个索引器来说非常麻烦。我想知道为什么

Hejlsberg等人决定采用这种复杂性,当Delphi以VB.NET的方式运行参数化属性。他们坚持

已经知道更好....


- Michael S



I think C# is quite cumbersome with this indexer-thingy. I wonder why
Hejlsberg et al decided do go for such complexity, when Delphi sported
parameterized properties in just the way VB.NET does stuff. They shoulod
have known better....

- Michael S


"迈克尔S" < a@b.c> écritdansle message de news:

ua ************* @ TK2MSFTNGP12.phx.gbl ...
"Michael S" <a@b.c> a écrit dans le message de news:
ua*************@TK2MSFTNGP12.phx.gbl...
我觉得C#对于这个索引器来说非常麻烦。我想知道为什么Hejlsberg等人决定采用这种复杂性,当Delphi以VB.NET的方式运行参数化属性时。他们已经知道了更好....
I think C# is quite cumbersome with this indexer-thingy. I wonder why
Hejlsberg et al decided do go for such complexity, when Delphi sported
parameterized properties in just the way VB.NET does stuff. They shoulod
have known better....




这就是为什么Delphi 2005提供的Delphi for .NET(以及C#)是

这么好的主意。 Delphi在最佳OO框架.NET中的所有遗产。


Joanna


-

Joanna Carter

顾问软件工程师



Which is why Delphi for .NET as provided (along with C#) by Delphi 2005 is
such a good idea. All the heritage of Delphi in the best OO framework .NET.

Joanna

--
Joanna Carter
Consultant Software Engineer


这篇关于如何将参数传递给C#中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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