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]