Connecting Tech Pros Worldwide Forums | Help | Site Map

Default properties

Chuck Bowling
Guest
 
Posts: n/a
#1: Nov 16 '05
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.



Fakher Halim
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Default properties


Chuck,
Please refer to
http://msdn.microsoft.com/library/de...components.asp[color=blue]
> MyClass c = new MyClass();
> c = "my text";[/color]
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[color=blue]
> c[0] = "my new text";[/color]

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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
news:O9rugmbJEHA.1940@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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[/color]
instance.[color=blue]
>
> If I'm understanding correctly, the way to declare a default property in[/color]
C#[color=blue]
> 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[/color]
crippled[color=blue]
> indexer.
>
>[/color]


Fakher Halim
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Default properties


A more specific example at:
http://msdn.microsoft.com/library/en...asp?frame=true

"Fakher Halim" <fakher@msn.com> wrote in message
news:%23lcVDgcJEHA.232@TK2MSFTNGP12.phx.gbl...[color=blue]
> Chuck,
> Please refer to
>[/color]
http://msdn.microsoft.com/library/de...components.asp[color=blue][color=green]
> > MyClass c = new MyClass();
> > c = "my text";[/color]
> 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[color=green]
> > c[0] = "my new text";[/color]
>
> The reason why you would want to use indexer is to select ONE ITEM out of[/color]
a[color=blue]
> 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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
> news:O9rugmbJEHA.1940@TK2MSFTNGP10.phx.gbl...[color=green]
> > Maybe I'm doing something wrong or just don't understand the concept,[/color][/color]
but[color=blue][color=green]
> > 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[/color]
> instance.[color=green]
> >
> > If I'm understanding correctly, the way to declare a default property in[/color]
> C#[color=green]
> > 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[/color]
> crippled[color=green]
> > indexer.
> >
> >[/color]
>
>[/color]


Chuck Bowling
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Default properties


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" <fakher@msn.com> wrote in message
news:%23lcVDgcJEHA.232@TK2MSFTNGP12.phx.gbl...[color=blue]
> Chuck,
> Please refer to
>[/color]
http://msdn.microsoft.com/library/de...components.asp[color=blue][color=green]
> > MyClass c = new MyClass();
> > c = "my text";[/color]
> 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[color=green]
> > c[0] = "my new text";[/color]
>
> The reason why you would want to use indexer is to select ONE ITEM out of[/color]
a[color=blue]
> 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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
> news:O9rugmbJEHA.1940@TK2MSFTNGP10.phx.gbl...[color=green]
> > Maybe I'm doing something wrong or just don't understand the concept,[/color][/color]
but[color=blue][color=green]
> > 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[/color]
> instance.[color=green]
> >
> > If I'm understanding correctly, the way to declare a default property in[/color]
> C#[color=green]
> > 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[/color]
> crippled[color=green]
> > indexer.
> >
> >[/color]
>
>[/color]


Fakher Halim
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Default properties


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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
news:%23VbhYZlJEHA.3428@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
specifically[color=blue]
> what you mentioned below. My idea of a default property is the style of[/color]
VB6.[color=blue]
> 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" <fakher@msn.com> wrote in message
> news:%23lcVDgcJEHA.232@TK2MSFTNGP12.phx.gbl...[color=green]
> > Chuck,
> > Please refer to
> >[/color]
>[/color]
http://msdn.microsoft.com/library/de...components.asp[color=blue][color=green][color=darkred]
> > > MyClass c = new MyClass();
> > > c = "my text";[/color]
> > Actually it is wrong to try to expect assigning strings to object c, as[/color][/color]
c[color=blue][color=green]
> > has a type MyClass.
> >
> > Even assigning of a string to c[0] is not exactly very intuitive[color=darkred]
> > > c[0] = "my new text";[/color]
> >
> > The reason why you would want to use indexer is to select ONE ITEM out[/color][/color]
of[color=blue]
> a[color=green]
> > 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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
> > news:O9rugmbJEHA.1940@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Maybe I'm doing something wrong or just don't understand the concept,[/color][/color]
> but[color=green][color=darkred]
> > > 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[/color]
> > instance.[color=darkred]
> > >
> > > If I'm understanding correctly, the way to declare a default property[/color][/color][/color]
in[color=blue][color=green]
> > C#[color=darkred]
> > > 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[/color]
> > crippled[color=darkred]
> > > indexer.
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Fakher Halim
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Default properties


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" <fakher@msn.com> wrote in message news:%23%23PMxfoJEHA.2380@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
> news:%23VbhYZlJEHA.3428@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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[/color]
> specifically[color=green]
> > what you mentioned below. My idea of a default property is the style of[/color]
> VB6.[color=green]
> > 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" <fakher@msn.com> wrote in message
> > news:%23lcVDgcJEHA.232@TK2MSFTNGP12.phx.gbl...[color=darkred]
> > > Chuck,
> > > Please refer to
> > >[/color]
> >[/color]
> http://msdn.microsoft.com/library/de...components.asp[color=green][color=darkred]
> > > > MyClass c = new MyClass();
> > > > c = "my text";
> > > Actually it is wrong to try to expect assigning strings to object c, as[/color][/color]
> c[color=green][color=darkred]
> > > 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[/color][/color]
> of[color=green]
> > a[color=darkred]
> > > 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" <chuckbowling@sbcglobal-NO-SPAM.net> wrote in message
> > > news:O9rugmbJEHA.1940@TK2MSFTNGP10.phx.gbl...
> > > > Maybe I'm doing something wrong or just don't understand the concept,[/color]
> > but[color=darkred]
> > > > 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[/color][/color]
> in[color=green][color=darkred]
> > > 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.
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
Closed Thread