473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Default properties

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.
Nov 16 '05 #1
5 10197
Chuck,
Please refer to
http://msdn.microsoft.com/library/de...components.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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl... 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.

Nov 16 '05 #2
A more specific example at:
http://msdn.microsoft.com/library/en...asp?frame=true

"Fakher Halim" <fa****@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Chuck,
Please refer to
http://msdn.microsoft.com/library/de...components.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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 16 '05 #3
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" <fa****@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Chuck,
Please refer to
http://msdn.microsoft.com/library/de...components.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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 16 '05 #4
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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <fa****@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Chuck,
Please refer to

http://msdn.microsoft.com/library/de...components.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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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.



Nov 16 '05 #5
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" <fa****@msn.com> wrote in message news:%2******************@TK2MSFTNGP09.phx.gbl...
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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <fa****@msn.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Chuck,
Please refer to

http://msdn.microsoft.com/library/de...components.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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
> 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.
>
>



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marti | last post by:
Dear everyone, Its my understanding that IE6 now uses a default text-size of "Small" rather than IE5's "Medium". Since I have used relative font-sizes (usually in ems) on all my sites, I am...
3
by: CodeToad | last post by:
I am using the Windows MCAD/MCSD book on VB.NET/VC.NET to refresh myself on some C# concepts and I ran into one issue in the properties area I just do not understand. In VB.NET this is easy to...
7
by: Simon Jefferies | last post by:
Hello, How do I make a function the "default" function? In VB.NET its the keyword default, is there a C# equivalent? Thanks In Advance. Regards Simon Jefferies Tools Programmer, Headfirst...
3
by: countd4 | last post by:
I have built a working user control. However, to make it work, I always have to set certian properties using the properties sheet for the control when using it on other forms. I want to be able to...
4
by: XNoDE | last post by:
Hi all, VS.Net 2002. Is there a way to set default properties for controls in the Toolbox? There a some properties that I implement as standard in my apps and would like to get around...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
15
by: simonoficina | last post by:
Hello all! I am a vb.net beginner in Spain. When I use VB6 ,the button object has a property called "default" that can set this button like press "ENTER" key. But in the VB.net I can't find this...
8
by: Grizlyk | last post by:
To continue some ideas of "C++ Archeology: Limits of OO paradigm", i want to ask about C++ development. There are C++ compilers, that has been written in old good C. Are C or pascal perfect...
10
by: =?Utf-8?B?SmFtZXMgV29uZw==?= | last post by:
Hi everybody, I'm trying to use the new VB 2008 right now and I want to know how to preset the company name and copyright informtion in Assembly Information. In my current VB 2005, company name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.