473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 8375
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Hi - I have a dynamically created table, which has a number of forms - each named consecutively as 'adduserX' where X is a number generated from ASP. Within each form, I need to have a button...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
7
by: TJ | last post by:
In C# how do you achieve pass-by-reference property declarations in the Type Library? I am writing a COM Class Library that must mimick an existing library for which the only information is the...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.