473,320 Members | 1,821 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,320 software developers and data experts.

Properties with Methods

Hello,

I am sure this is easy, but I am not sure what to search for but I am looking to be able to do something like:

Expand|Select|Wrap|Line Numbers
  1. MyObject.MyProperty = "1234F";
  2. MyObject.MyProperty.IsValid();
  3. MyObject.MyProperty.MyOtherMethod();
  4.  
I want to be able to add methods to properties of different types such as Strings, Ints and GUIDs.

Cheers,
Scott
Jul 4 '09 #1
3 1503
cloud255
427 Expert 256MB
Hi

Well it is pretty straight forward, you have to create a class which has properties (variables) and methods (functions).

Hope this helps, feel free to post if the above msdn links don't help you achieve what you want to.
Jul 6 '09 #2
Curtis Rutland
3,256 Expert 2GB
So consider this code:
Expand|Select|Wrap|Line Numbers
  1. //CLASS B
  2. public class B
  3. {
  4.   public float Value { get; set; }
  5.   public bool IsValid
  6.   {
  7.     get
  8.     {
  9.       //use whatever logic you need here
  10.       return Value > 0;
  11.     }
  12.   }
  13.   public void MyOtherMethod()
  14.   {
  15.     //do whatever
  16.   }
  17. }
  18.  
  19.  
  20. ///CLASS A
  21. public class A
  22. {
  23.   public B MyProperty { get; set; }
  24.  
  25.   public A ()
  26.   {
  27.     MyProperty = new B();
  28.   }
  29. }
You create a class "A" that has all the properties you need. Then you create your class "B" which has a property of type "A". That should do what you need.

Note that in my example, "IsValid" is a property, not a method, and wouldn't need () when called.
Jul 6 '09 #3
Christian Binder
218 Expert 100+
Hi,

you could also use Extension-Methods to solve your problem if you are using .NET 3.5.
With this you can add Methods to the Integer-Type for example, wo you would be able to do:
Expand|Select|Wrap|Line Numbers
  1. int myInt = 0; 
  2. myInt.IsValid()
  3.  
But i would prefer using something like insertAlias' example. You could overload the Implicit Conversion Operator, so you could directly assign the float like you did in your example
MyObject.MyProperty = "1234F";
instead of using the .Value-property of class B
Jul 10 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
23
by: Marcin Grzêbski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
3
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
3
by: Sam Sungshik Kong | last post by:
Hello! While using panel control, I wondered a thing. Panel class is subclass of Control class. Control class has KeyPress event and Focus() method, etc... Then Panel class must have them. I...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
0
by: Giorgio | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older...
1
by: erik | last post by:
Hi! Is it possible to expose customer properties used in a class in a web servies. Can I use on a property or how do I do? The thing is that I do not want to use parameters for the method I have...
17
by: Bruce One | last post by:
Lets consider a class called Currency. This class must be the responsible for taking care of all calculations over currency exchanges, in such a way that I pass values in Euros and it returns the...
39
by: Paul Mcilreavy | last post by:
Hi, i would like to get some view points on the use of properties within classes. My belief is that properties should generally be used to return values of private members. They should not do...
22
by: Peter | last post by:
Hi I was wondering, why use properties instead of get and set methods? I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.