Property

  • Thread starter Thread starter Roberto Sartori
  • Start date Start date
R

Roberto Sartori

Hi.

I'd want to know if it is possible in C # to declare one property analogous
to following (written in VB):

Property PropertyName (ByVal Index As Integer) As Object
Get
Return List.Item(Index)
End Get
Set (ByVal Value As Object)
List.Item(Index) = Value
End Set
End Property

(where List is an object of class CollectionBase).

The problem resides in the fact that in C # I don't succeed to declare a
property that receives a parameter (in this case an integer, but in a
generalized manner whichever type of object).

I've seen that an analogous thing can be made through indexer, but in the
case I want to pass more than one parameter or a parameter that isn't an
Integer?

More faster, someone can tell me why this translantion in C# of that VB code
fragment doesn't work?

public Object PropertyName (int Index) {
get {
return List.Item(Index);
}
set {
List.Item(Index)=value;
}
}

Thanks everyone.
Hi.
Roberto Sartori.
 
Look at indexers in the C# help.

public class MyCollection : CollectionBase
{
public MyItem this[int index, other parameters....]
{
get
{
return (MyItem)List[index];
}
set
{
List[index] = value;
}
}
}

or in your case MyItem would be Object although CollectionBase is generally
used for strongly typed collections so you cast the Object to the
collection's Item type rather then returning type Object.

SP
 
Your VB property example won't compile either. Just use separate get/set
functions or an indexer (C#).

kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top