473,767 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10227
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**********@s bcglobal-NO-SPAM.net> wrote in message
news:O9******** ******@TK2MSFTN GP10.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******** *******@TK2MSFT NGP12.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**********@s bcglobal-NO-SPAM.net> wrote in message
news:O9******** ******@TK2MSFTN GP10.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******** *******@TK2MSFT NGP12.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**********@s bcglobal-NO-SPAM.net> wrote in message
news:O9******** ******@TK2MSFTN GP10.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.Ca ption = "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**********@s bcglobal-NO-SPAM.net> wrote in message
news:%2******** ********@TK2MSF TNGP09.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******** *******@TK2MSFT NGP12.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**********@s bcglobal-NO-SPAM.net> wrote in message
news:O9******** ******@TK2MSFTN GP10.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.WriteLi ne(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******** **********@TK2M SFTNGP09.phx.gb l...
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.Ca ption = "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**********@s bcglobal-NO-SPAM.net> wrote in message
news:%2******** ********@TK2MSF TNGP09.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******** *******@TK2MSFT NGP12.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**********@s bcglobal-NO-SPAM.net> wrote in message
news:O9******** ******@TK2MSFTN GP10.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
7108
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 now getting reports of "your fonts are too tiny" from various users. Of course, the sites still look fine if those users set their text-size to medium, but face it -- most people don't even know thats an option.
3
1496
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 understand, in VC.NET I am lost, can someone simplify it for me? Here is some sample code: public int this { get
7
3794
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 Productions
3
6634
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 set default values for most properties so the control will work as-is without requiring the developer to set them. I notice in the compment initialization code create when I drop the control on a form that all the properties are set to null or...
4
2020
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 setting these same basic things each time a use a control.
3
6760
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 following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
15
12548
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 property. Where is it? Or mabye change the other thing,No?Thanks ! Simon
8
1920
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 languages that can be used to build any powerful OO language with mutable source representation, as C++ could be? Obviously no. Is current C++ can be used to build the OO language? Obviously no, because unfortunatelly C++ simultaneously is...
10
9685
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 and copyright information (the word "CopyRight" with company name and year) is filled in automatically once a new project is created. However, I have no idea where I can configurate it in my new VB 2008. Thanks for your kindly advice!
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.