469,890 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

How to pass arguments to a property in C#?

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

Nov 17 '05 #1
15 8112
Prachi Dimble <pr****@eximiuus.com> wrote:
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..


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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Prachi Dimble <pr****@eximiuus.com> wrote:
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..


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


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
Nov 17 '05 #3
"Michael S" <a@b.c> a écrit dans le message de news:
ua*************@TK2MSFTNGP12.phx.gbl...
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....


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
Nov 17 '05 #4
okay!

Thanks,
Prachi

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Prachi Dimble <pr****@eximiuus.com> wrote:
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..


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

Nov 17 '05 #5
"Prachi Dimble" <pr****@eximiuus.com> schrieb im Newsbeitrag
news:eX**************@TK2MSFTNGP10.phx.gbl...
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


Hi Parichi,

to call parameterized properties from VB.NET in C# you have to call the
accessormethods:
field = VBObject.get_Field(...);
VBObject.setField(......,field);
Nov 17 '05 #6
I think it's a simplicity

obj.Field( "string1", "string2", 3)

Is that a method or a property being called?

In C# you know it;s a method. What about with the code provided by the OP ?

IMO if you need to pass parameters you better go with a method.


Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
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

Nov 17 '05 #7
we can use [] notation like the indexers currently do.

I think it'd be a nice feature to have. for example, say I'm implementing a
shopping cart that exposes line items, and I want to access it like

orderEntry = myCart.LineItems[productID];

currently, I'd have to have a property LineItems that exposes the underlying
line item collection with the indexer. and if this line item collection has
methods that mutates the collection that i don't want to expose outside of
the API, I'd have to mark them as internal, or return some kind of readonly
wrapper.

or alternatively, if parameterized properties are supported, I don't have to
expose the internal collection at all. Just make a property of shopping cart
called LineItems that take product ID as a parameter.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
I think it's a simplicity

obj.Field( "string1", "string2", 3)

Is that a method or a property being called?

In C# you know it;s a method. What about with the code provided by the OP ?

IMO if you need to pass parameters you better go with a method.


Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
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


Nov 17 '05 #8
Indeed.

And C# indexer approach is just a limitation that demands a workaround to
achieve what VB/Delphi sport with grace. But I am curious why Hejlsberg et
al decided to take this approach. For me it makes no sense.

- Michael S

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
we can use [] notation like the indexers currently do.

I think it'd be a nice feature to have. for example, say I'm implementing
a
shopping cart that exposes line items, and I want to access it like

orderEntry = myCart.LineItems[productID];

currently, I'd have to have a property LineItems that exposes the
underlying
line item collection with the indexer. and if this line item collection
has
methods that mutates the collection that i don't want to expose outside of
the API, I'd have to mark them as internal, or return some kind of
readonly
wrapper.

or alternatively, if parameterized properties are supported, I don't have
to
expose the internal collection at all. Just make a property of shopping
cart
called LineItems that take product ID as a parameter.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
I think it's a simplicity

obj.Field( "string1", "string2", 3)

Is that a method or a property being called?

In C# you know it;s a method. What about with the code provided by the OP
?

IMO if you need to pass parameters you better go with a method.


Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
> 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
>
>


Nov 17 '05 #9
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:u3**************@TK2MSFTNGP15.phx.gbl...
obj.Field( "string1", "string2", 3)

Is that a method or a property being called?

In C# you know it;s a method. What about with the code provided by the OP

?

Well, since nothing is assigned to it, nor assigned from it, it would
have to be a method in any language, right? ;-)

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #10

"James Curran" <ja*********@mvps.org> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:u3**************@TK2MSFTNGP15.phx.gbl...
obj.Field( "string1", "string2", 3)

Well, since nothing is assigned to it, nor assigned from it, it would
have to be a method in any language, right? ;-)


Not really.

In Delphi it is pretty straight forward as there are language constructs
that shows that a property is mapped to a field.

fCount: Integer;
property Count: integer read fCount;

Hence the compiler simply map the property Count to the virtual address of
fCount.

In managed langauges, as Java and CIL; there is no such thing as a property.
C# have properties as syntactic sugar for getters and setters, but as you
said; CIL only sports methods.

But the Just In Time Compiler (JIT) will optimize and scan all get and set
methods. If the methods is just read and assignment to fields it will be
inlined.

Hence..

private int count;
public int Count
{
get
{
return count;
}
}

.... would never yield a method call to get_Count, but map straight to the
virtual address of count.

This is true for both Java and CIL.

JITters are quite smart. Just look how they manages strings:

int digit = 2;
string strsimple = "1" + digit.ToString() + "3";

C# implies that two calls are made to an overridden + operator method on the
class System.String in conjunction with the ToString() method. Luckely for
us, and our users, this is not what we get, as it would give us the
performance of a snail =)

The JITter could care no less about language constructs in C# and CIL, and
simply calculate the size of the strings and allocate a a new string and
then copy the buffers. No methods called whatsoever. Really!

But what about this?

string str = "Hello, " + name + "!" + " It is " + day + " and the weather is
" + weather + ". Have a nice day!";

In this case, the JITter would actually create a StringBuilder (StringBuffer
in Java).
And you might think it would use be something like
tempstr.Append(...).Append(..).Append(...).ToStrin g();

But No! There is no such thing as an Append method on a StringBuilder when
it comes actual runtime. That method will be replaced by the JITter with
proper cpu-instructions for moving unicode, depending on what cpu you got.
And future computers will have a unicode-chip that does encodings in
hardware. Strings will be faster...

This is what we love about Managed Code. We get all the sugar from C#
syntax, and the JITter can become smarter and smarter, move to better and
better computers, without breaking any code.

What you write in C# is not WYSIWYG but WYSGDSTYT

WYSIWYG - What You See Is What You Get
WYSGDSTYT- What You See Gets Done Smarter Than You Think

Regards
- Michael S
Nov 17 '05 #11
Michael S <a@b.c> wrote:
But what about this?

string str = "Hello, " + name + "!" + " It is " + day + " and the weather is
" + weather + ". Have a nice day!";

In this case, the JITter would actually create a StringBuilder (StringBuffer
in Java).
In Java this is accurate, although the calls are created by the Java
compiler, not the JIT compiler. (Hopefully in Java 1.5 it uses the new
Java StringBuilder instead, which doesn't synchronize - I haven't
checked though.)

In C#, String.Concat is used instead of a StringBuilder.
But No! There is no such thing as an Append method on a StringBuilder when
it comes actual runtime.
Um, I rather think there is.
That method will be replaced by the JITter with
proper cpu-instructions for moving unicode, depending on what cpu you got.


Certainly within the method there will be CPU instructions, obviously,
and the method *may* be inlined by the JIT, but it won't always be.
(Consider a reflection call.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
Michael S <a@b.c> wrote:
But what about this?

string str = "Hello, " + name + "!" + " It is " + day + " and the weather is
" + weather + ". Have a nice day!";

In this case, the JITter would actually create a StringBuilder (StringBuffer
in Java).
In Java this is accurate, although the calls are created by the Java
compiler, not the JIT compiler. (Hopefully in Java 1.5 it uses the new
Java StringBuilder instead, which doesn't synchronize - I haven't
checked though.)

In C#, String.Concat is used instead of a StringBuilder.
But No! There is no such thing as an Append method on a StringBuilder when
it comes actual runtime.
Um, I rather think there is.
That method will be replaced by the JITter with
proper cpu-instructions for moving unicode, depending on what cpu you got.


Certainly within the method there will be CPU instructions, obviously,
and the method *may* be inlined by the JIT, but it won't always be.
(Consider a reflection call.)

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

You can check out www.eassignment.net for help with your assignments.
They are really helpful.

Simon Tanner

Nov 17 '05 #14
"Michael S" <a@b.c> wrote in message
news:el*************@TK2MSFTNGP12.phx.gbl...
"James Curran" <ja*********@mvps.org> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
obj.Field( "string1", "string2", 3) Well, since nothing is assigned to it, nor assigned from it, it would
have to be a method in any language, right? ;-)


Not really.

In Delphi it is pretty straight forward as there are language constructs
that shows that a property is mapped to a field.
[snip]


You seemed to have competely missed my point.

The original statement was :

obj.Field( "string1", "string2", 3)
Is that a method or a property being called?

I was trying to point out, that had it been written as:
int x = obj.Field( "string1", "string2", 3)
then we truly couldn't tell if it were a method or a property. But, as it
was written as
obj.Field( "string1", "string2", 3)
it would have to be either:
a) a method, called for it's side effects.
b) a property, with side-effects (ie, a method pretending to be a
property), or
c) a property without side-effect (ie, in this case, a NOP)

Hence, we can safely conclude, if it's not an error, it's a method.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

fCount: Integer;
property Count: integer read fCount;

Hence the compiler simply map the property Count to the virtual address of
fCount.

In managed langauges, as Java and CIL; there is no such thing as a property. C# have properties as syntactic sugar for getters and setters, but as you
said; CIL only sports methods.

But the Just In Time Compiler (JIT) will optimize and scan all get and set
methods. If the methods is just read and assignment to fields it will be
inlined.

Hence..

private int count;
public int Count
{
get
{
return count;
}
}

... would never yield a method call to get_Count, but map straight to the
virtual address of count.

This is true for both Java and CIL.

JITters are quite smart. Just look how they manages strings:

int digit = 2;
string strsimple = "1" + digit.ToString() + "3";

C# implies that two calls are made to an overridden + operator method on the class System.String in conjunction with the ToString() method. Luckely for
us, and our users, this is not what we get, as it would give us the
performance of a snail =)

The JITter could care no less about language constructs in C# and CIL, and
simply calculate the size of the strings and allocate a a new string and
then copy the buffers. No methods called whatsoever. Really!

But what about this?

string str = "Hello, " + name + "!" + " It is " + day + " and the weather is " + weather + ". Have a nice day!";

In this case, the JITter would actually create a StringBuilder (StringBuffer in Java).
And you might think it would use be something like
tempstr.Append(...).Append(..).Append(...).ToStrin g();

But No! There is no such thing as an Append method on a StringBuilder when
it comes actual runtime. That method will be replaced by the JITter with
proper cpu-instructions for moving unicode, depending on what cpu you got.
And future computers will have a unicode-chip that does encodings in
hardware. Strings will be faster...

This is what we love about Managed Code. We get all the sugar from C#
syntax, and the JITter can become smarter and smarter, move to better and
better computers, without breaking any code.

What you write in C# is not WYSIWYG but WYSGDSTYT

WYSIWYG - What You See Is What You Get
WYSGDSTYT- What You See Gets Done Smarter Than You Think

Regards
- Michael S

Nov 17 '05 #15

"James Curran" <ja*********@mvps.org> wrote in message
news:eb**************@TK2MSFTNGP15.phx.gbl...
"Michael S" <a@b.c> wrote in message
news:el*************@TK2MSFTNGP12.phx.gbl...
"James Curran" <ja*********@mvps.org> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl...
>> obj.Field( "string1", "string2", 3)
> Well, since nothing is assigned to it, nor assigned from it, it
> would
> have to be a method in any language, right? ;-)


Not really.

In Delphi it is pretty straight forward as there are language constructs
that shows that a property is mapped to a field.
[snip]


You seemed to have competely missed my point.


Not really.

The original obj.Field( "string1", "string2", 3) is actual a property taking
arguments.
However, the terrible VB syntax makes it look like a method.

Hence, You miss-read the OP and I miss-read you.. What a mess. =)

- Michael S
Nov 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

41 posts views Thread by Berk Birand | last post: by
41 posts views Thread by Telmo Costa | last post: by
9 posts views Thread by Csaba Gabor | last post: by
36 posts views Thread by Pacific Fox | last post: by
3 posts views Thread by Davy | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.