Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default property with parameter problem.

Status
Not open for further replies.

morleyhill

Programmer
Aug 13, 2002
4
0
0
GB
Hi Everyone

I'm an experienced VB.Net programmer but just starting to pick up C#. I'm trying to convert some VB.Net clases into equivalent C# and I've hit a problem with converting a parametized property. Can someone please help me convert the following VB.Net into C#-

Default Public Property Item(ByVal index As Integer) As String
Get
Return List.Item(index).ToString
End Get
Set(ByVal Value As String)
List.Item(index) = Value
End Set
End Property

I've two problems-
1. What is the correct syntax for flagging the property as Default?

2. How do I implement the parameter 'index'?

I used an online VB.Net to C# conversion tool which produced the following-

public string Item
{
get
{
return List.Item(index).ToString;
}
set
{
List.Item(index) = value;
}
}

but the 'default' setting is lost and the compiler complains that 'index' isn't defined.

This is such a common and simple pattern I'm struggling to understand where I'm going wrong!!!


 
There aren't default properties in C#, but what it sounds like what you want is an "indexer". Switch to class view, right-click on the class name, then select Add indexer, and you'll get a wizard that will walk you thru the process.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Code:
public class Class1
{
public class1(){}

[DefaultMember(true)]
public string this [int Index]
{    get
    {
        return List.Item(Index).ToString;
    }
        set
    {
        List.Item(Index) = value;
    }
}
}


Usage:

Class1 c = new Class1();
c[0].ToString();

Scott
Programmer Analyst
<{{><
 
I stand corrected.

Stnkyminky - is that part of the CLS spec?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The code you post cannot implement a property because you need to pass a parameter (index).
Try to implement that property as an indexer. But an indexer cannot be declared as default member (using DefaultMemberAttribute).
The DefaultMemberAttribute can be applied on class level and refers to properties only,so it does not work on indexer, (stnkyminky solution).
Here are more about the properties and indexers:
A property is a member that provides access to a characteristic of an object or a class.
Examples of properties include the length of a string, the name of a customer, and so on.
Properties are a natural extension of fields-both are named members with associated types, and the syntax for accessing fields and properties is the same.
However, unlike fields, properties do not denote storage locations.
Instead, properties have accessors that specify the statements to be executed when their values are read or written.
Properties thus provide a mechanism for associating actions with the reading and writing of an object's attributes and also they permit such attributes to be computed.
Property declarations are subject to the same rules as method declarations and here is the general format:
Code:
attribute   property-modifier   type   member-name   {   accessor-declarations   }
The "type" of a property declaration specifies the type of the property introduced by the declaration, and the "member-name" specifies the name of the property.
Unless the property is an explicit interface member implementation, the "member-name" is simply an identifier.
For an explicit interface member implementation, the "member-name" consists of an interface-type followed by a "." and an identifier.
The "type" of a property must be at least as accessible as the property itself.
The "accessor-declarations", which must be enclosed in "{" and "}" tokens, declare the accessors of the property.
The accessors specify the executable statements associated with reading and writing the property.
Even though the syntax for accessing a property is the same as that for a field, a property is not classified as a variable.
Thus, it is not possible to pass a property as a ref or out argument.

Example:
Code:
abstract class A
{
	int y;
	public virtual int X {
		get { return 0; }
	}
	public virtual int Y {
		get { return y; } 
		set { if(y > value) y=value; }
	}
	public abstract int Z { get; set; }
}

Each accessor declaration consists of the token "get" or "set" followed by an "accessor-body".
For abstract and extern properties, the accessor-body for each accessor specified is simply a semicolonb (see Z property).
For other properties, the accessor-body for each accessor specified is a block which contains the statements to be executed when the corresponding accessor is invoked.

An indexer is a member that enables an object to be indexed in the same way as an array.
Indexers are declared using indexer-declarations:
Code:
attribute   indexer-modifiers   indexer-declarator   {   accessor-declarations   }

where indexer-declarator has one of the following formats:
Code:
type   this   [   parameter-list   ]
type   interface-type   .   this   [   parameter-list   ]

Indexers and properties are very similar in concept, but differ in the following ways:
A property is identified by its name, whereas an indexer is identified by its signature.
A property is accessed through a simple-name or a member-access , whereas an indexer element is accessed through an element-access.
A property can be a static member, whereas an indexer is always an instance member.
A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.
A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.
It is a compile-time error for an indexer accessor to declare a local variable with the same name as an indexer parameter.

Example of an indexer with two paramters:
Code:
class Matrix
{
	int[,] cells = new int[26,10];
	public int this[char c, int column]
	{
		get {

			return cells[c - 'A', column];
		}
		set {

			cells[c - 'A', colm] = value;
		}
	}
}

-obislavu-
 
Thanks stnkyminky, however-

1. The compiler doesn't like [DefaultMember(true)], it complains that-
Attribute 'DefaultMember' is not valid on this declaration type. It is valid on 'class, struct, interface' declarations only.

2. Is the declaration-
public string this [int Index]
a special property type? It looks like this has lost the property name 'Item' is that correct?
 
Sorry, while working on that last post you good people have given me all the answers. Thanks to everyone for their help.
There I was thinking the only difference between VB.Net and C# was semicolons and curly brackets!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top