Default Property of a class - C# 
Author Message
 Default Property of a class - C#

Hello, I have a property inside a class, called COMPANY that stores a string
with the name of a given company and other property called PHONES that is an
array [3] of 3 phone numbers. Id like to build a default property for this
2 properties in order to give back to a call of it the first element in
Phones array  and the Company name.

Sample:

MyObjAbove myObj = new myObj();

string mCompany;
string mPhone;

myObj.company = "Testing";
myObj.phone[1] = "1";
myObj.phone[2] = "2";
myObj.phone[3] = "3";

mCompany = myObj.Company;    // called the GET method of the property
mPhone      = myObj.Phone;         // In this case, Id like the number 1
was assigned to the variable phone[1] by default
                                                    // How can I do it?

Thanks



Thu, 30 Dec 2004 21:21:01 GMT  
 Default Property of a class - C#
Use [DefaultProperty("MyMethod")] on your class


Thu, 30 Dec 2004 21:52:54 GMT  
 Default Property of a class - C#


Quote:
> Hello, I have a property inside a class, called COMPANY that stores a
string
> with the name of a given company and other property called PHONES that is
an
> array [3] of 3 phone numbers. Id like to build a default property for
this
> 2 properties in order to give back to a call of it the first element in
> Phones array  and the Company name.

> Sample:

> MyObjAbove myObj = new myObj();

> string mCompany;
> string mPhone;

> myObj.company = "Testing";
> myObj.phone[1] = "1";
> myObj.phone[2] = "2";
> myObj.phone[3] = "3";

> mCompany = myObj.Company;    // called the GET method of the property
> mPhone      = myObj.Phone;         // In this case, Id like the number 1
> was assigned to the variable phone[1] by default
>                                                     // How can I do it?

I don't think you can. See, the problem is, you want to assign
the property as a string[], but you want to read it as a string.
You can't have dual-type properties, unfortunately.

What you could do is have a property called PhoneNumbers which is
of type string[] and then have a property called FirstPhoneNumber
which is string and gets the first element of the phone number
array.

-c



Thu, 30 Dec 2004 23:33:15 GMT  
 Default Property of a class - C#

Quote:
> myObj.company = "Testing";
> myObj.phone[1] = "1";
> myObj.phone[2] = "2";
> myObj.phone[3] = "3";

start index at 0 and not 1

Quote:
> mCompany = myObj.Company;    // called the GET method of the property
> mPhone      = myObj.Phone;         // In this case, Id like the number 1
> was assigned to the variable phone[1] by default

public string Phone {
 get{return phone!=null && phone.Length > 0 ? phone[0] : string.Empty;}
Quote:
}



Fri, 31 Dec 2004 00:15:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Custom Class.cs default file

2. Equivalent class of "Properties" in C#

3. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

4. Global Properties (i.e. application properties) in C#

5. Creating Property Sheets and Property Pages w/o the Class Wizard

6. Indexable Default field/property like []?

7. Specifying Property Defaults in a WSDL

8. The Default Index Property

9. How to reset the XML Text Editor default property

10. default value for custom component properties

11. Properties, default value

12. Property default value

 

 
Powered by phpBB® Forum Software