How to pass arguments to a property in C#?

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

Prachi Dimble said:
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.
 
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)(strFieldName)
Return objDataSet.Tables(objDataSet.Tables.Count - 1).Rows(rowNum)(strFieldName)
End Get

Set(ByVal Value)
If tableName <> "" Then
objDataSet.Tables(tableName).Rows(rowNum)(strFieldName) = Value
Else
objDataSet.Tables(objDataSet.Tables.Count - 1).Rows(rowNum)(strFieldName) = Value
End If
End Set
End Property
 
Jon Skeet said:
In C#, you can only pass parameters to special properties called
indexers. You'll need to make your property an indexer for your class.

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
 
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
 
okay!

Thanks,
Prachi

Jon Skeet said:
In C#, you can only pass parameters to special properties called
indexers. You'll need to make your property an indexer for your class.
 
Prachi Dimble said:
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)(strFieldName)
Return objDataSet.Tables(objDataSet.Tables.Count -
1).Rows(rowNum)(strFieldName)
End Get
Set(ByVal Value)
If tableName <> "" Then
objDataSet.Tables(tableName).Rows(rowNum)(strFieldName) = 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);
 
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,
 
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.
 
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 said:
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 ) said:
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 ( .NET/ C# MVP ) said:
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
 
James Curran said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:u3Hg#[email protected]...
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(...).ToString();

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
 
Michael S said:
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.)
 
Michael S said:
James Curran said:
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
 
James Curran said:
Michael S said:
James Curran said:
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
 
Back
Top