Default properties

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;

MyClass c = new MyClass();

c = "my text";

In the line above, the string is assigned to a field in the class instance.

If I'm understanding correctly, the way to declare a default property in C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a crippled
indexer.
 
Chuck,
Please refer to
http://msdn.microsoft.com/library/d.../vbconpropertiesmethodsfieldsincomponents.asp
MyClass c = new MyClass();
c = "my text";
Actually it is wrong to try to expect assigning strings to object c, as c
has a type MyClass.

Even assigning of a string to c[0] is not exactly very intuitive
c[0] = "my new text";

The reason why you would want to use indexer is to select ONE ITEM out of a
collection.
Like one Widget out of Widgets class, as it is done in MSDN example I
recommended.
The only benefit is: Instead of having to specify (say)
Widgets.Item[3].Status, you can use Widgets[3].Status.

The VB6 style parameterless default prperty is not really encouraged in
..NET.

Fakher Halim
Software Architect
TPG



Chuck Bowling said:
Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;


In the line above, the string is assigned to a field in the class instance.

If I'm understanding correctly, the way to declare a default property in C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a crippled
indexer.
 
A more specific example at:
http://msdn.microsoft.com/library/e...efaultPropertyOrMethodForClass.asp?frame=true

Fakher Halim said:
Chuck,
Please refer to
http://msdn.microsoft.com/library/d.../vbconpropertiesmethodsfieldsincomponents.asp
MyClass c = new MyClass();
c = "my text";
Actually it is wrong to try to expect assigning strings to object c, as c
has a type MyClass.

Even assigning of a string to c[0] is not exactly very intuitive
c[0] = "my new text";

The reason why you would want to use indexer is to select ONE ITEM out of a
collection.
Like one Widget out of Widgets class, as it is done in MSDN example I
recommended.
The only benefit is: Instead of having to specify (say)
Widgets.Item[3].Status, you can use Widgets[3].Status.

The VB6 style parameterless default prperty is not really encouraged in
.NET.

Fakher Halim
Software Architect
TPG



Chuck Bowling said:
Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;


In the line above, the string is assigned to a field in the class instance.

If I'm understanding correctly, the way to declare a default property in C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a crippled
indexer.
 
Thank you for the response. I understand how to use indexers and their
purpose and i think they are a great idea. However, my gripe is specifically
what you mentioned below. My idea of a default property is the style of VB6.
The current C# syntax is simply an indexer. MS help describes this as a
method for setting default properties. A lack of distinction that can be
confusing for the layman...


Fakher Halim said:
Chuck,
Please refer to
http://msdn.microsoft.com/library/d.../vbconpropertiesmethodsfieldsincomponents.asp
MyClass c = new MyClass();
c = "my text";
Actually it is wrong to try to expect assigning strings to object c, as c
has a type MyClass.

Even assigning of a string to c[0] is not exactly very intuitive
c[0] = "my new text";

The reason why you would want to use indexer is to select ONE ITEM out of a
collection.
Like one Widget out of Widgets class, as it is done in MSDN example I
recommended.
The only benefit is: Instead of having to specify (say)
Widgets.Item[3].Status, you can use Widgets[3].Status.

The VB6 style parameterless default prperty is not really encouraged in
.NET.

Fakher Halim
Software Architect
TPG



Chuck Bowling said:
Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;


In the line above, the string is assigned to a field in the class instance.

If I'm understanding correctly, the way to declare a default property in C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a crippled
indexer.
 
You are right, Chuck. It is not at all default property.
For instance, In VB6, you could just skip the Caption Property of a label
control (a class instance in C#), and just assign a string to it
Both of the following were allowed:
lblEnterName.Caption = "Enter full name"
lblEnterName = "Enter full name"

Since .NET languages are stongly typed, only comptible data types could be
assigned to a class instance (what used to be a VB6 control).
Doing same in .NET would complain "Cannot implicity convert string to
System.Windows.Forms.Label."
Frankely I can't imagine why it shoud not.
Fakher Halim

Chuck Bowling said:
Thank you for the response. I understand how to use indexers and their
purpose and i think they are a great idea. However, my gripe is specifically
what you mentioned below. My idea of a default property is the style of VB6.
The current C# syntax is simply an indexer. MS help describes this as a
method for setting default properties. A lack of distinction that can be
confusing for the layman...


Fakher Halim said:
Chuck,
Please refer to
http://msdn.microsoft.com/library/d.../vbconpropertiesmethodsfieldsincomponents.asp
MyClass c = new MyClass();
c = "my text";
Actually it is wrong to try to expect assigning strings to object c, as c
has a type MyClass.

Even assigning of a string to c[0] is not exactly very intuitive
c[0] = "my new text";

The reason why you would want to use indexer is to select ONE ITEM out
of
a
collection.
Like one Widget out of Widgets class, as it is done in MSDN example I
recommended.
The only benefit is: Instead of having to specify (say)
Widgets.Item[3].Status, you can use Widgets[3].Status.

The VB6 style parameterless default prperty is not really encouraged in
.NET.

Fakher Halim
Software Architect
TPG



Chuck Bowling said:
Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;


In the line above, the string is assigned to a field in the class instance.

If I'm understanding correctly, the way to declare a default property
in
C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a crippled
indexer.
 
Chuck,
I have been trying to figure out the best way of simulating VB6 style default property in C#
as:
MyClass c="Fakher Halim";//asing a constant string directly to any class instance
Console.WriteLine(c); //print it to verify

It is implemented in the following class using implicit type casting operator
class MyClass{//Has Text Property, as default
//Ability to assign strings directly to .Text Property of MyClass -- Like VB6
public static implicit operator MyClass(string s) {
MyClass c=new MyClass();
c.Text=s;
return c;
}
private string _Text;
public string Text{
get{return _Text; }
set { _Text = value; }
}
public override string ToString() { return Text;}
}

Fakher Halim
Software Architect
TPG

Fakher Halim said:
You are right, Chuck. It is not at all default property.
For instance, In VB6, you could just skip the Caption Property of a label
control (a class instance in C#), and just assign a string to it
Both of the following were allowed:
lblEnterName.Caption = "Enter full name"
lblEnterName = "Enter full name"

Since .NET languages are stongly typed, only comptible data types could be
assigned to a class instance (what used to be a VB6 control).
Doing same in .NET would complain "Cannot implicity convert string to
System.Windows.Forms.Label."
Frankely I can't imagine why it shoud not.
Fakher Halim

Chuck Bowling said:
Thank you for the response. I understand how to use indexers and their
purpose and i think they are a great idea. However, my gripe is specifically
what you mentioned below. My idea of a default property is the style of VB6.
The current C# syntax is simply an indexer. MS help describes this as a
method for setting default properties. A lack of distinction that can be
confusing for the layman...


Fakher Halim said:
Chuck,
Please refer to
http://msdn.microsoft.com/library/d.../vbconpropertiesmethodsfieldsincomponents.asp
MyClass c = new MyClass();
c = "my text";
Actually it is wrong to try to expect assigning strings to object c, as c
has a type MyClass.

Even assigning of a string to c[0] is not exactly very intuitive
c[0] = "my new text";

The reason why you would want to use indexer is to select ONE ITEM out
of
a
collection.
Like one Widget out of Widgets class, as it is done in MSDN example I
recommended.
The only benefit is: Instead of having to specify (say)
Widgets.Item[3].Status, you can use Widgets[3].Status.

The VB6 style parameterless default prperty is not really encouraged in
.NET.

Fakher Halim
Software Architect
TPG



Maybe I'm doing something wrong or just don't understand the concept, but
i'm having a problem with default properties.

My impression of how a default property should act is this;


In the line above, the string is assigned to a field in the class
instance.

If I'm understanding correctly, the way to declare a default property in
C#
is to use an indexer like so:

public string this[int i]
{
get{return myString; }
set { myString = value; }
}

I tried this and the only way I could make it work was like so;

c[0] = "my new text";

How is this a default property? It looks to me like no more than a
crippled
indexer.
 
Back
Top