Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So this might be a stupidly simple question but I can't seem to find an example.

What I want is to create a method that is tied to a property of my class.

So for example if I have a string mystr I can use mystr.trim() which is a method on the string I've created.

I want to create the same effect within a class. If I have a class myclass I know I can easily create methods that will be called like myclass.method but what I'm really trying to do is create myclass.property.method to make it clear that the method is operating on the property and not other parts of the class.

I'm thinking I could do this the long way by creating a sub class that has the method on it ... but my property is a string so it seems a little wasteful to create a class to hold the string, only to allow me to create a method that works just on that string ... especially given I would still like to be able to treat it as a string and not as a different type of object.

Hopefully this has made sense and someone can point me in the right direction.

Thanks.
Posted

There are a couple of details here: You can't derive any class from string in the first place. The string class is sealed to prevent that.

The only way you can make myclass.property.method work but property.method not work is to use your own class - which you can't derive from string anyway.

I think you need to go back to the drawing board with this, I'm afraid!
 
Share this answer
 
Comments
BHort 29-Oct-11 8:47am    
Or explain better.


One of the properties of my class is a string. I want to create a method that works on that string that is called by doing a myclass.mystring.method call rather than simply a myclass.method call.


Does that make it clearer?

So what I was suggesting was that perhaps I needed to create

mystringclass

and within that create mystringclass.method

And then in my outer class I would have myclass.mystringclassobject which would then have a method associated which I could call by doing a myclass.mystringclassobject.method call.

Hopefully this makes some sense ...
OriginalGriff 29-Oct-11 9:31am    
Yes - the only problem is that you cannot derive your new class from string - the string class is sealed. So to do what you want, you have to set up your class to contain a string and either expose it via a getter and setter or implement each and every property of a string which you want to be usable on the string your class contains.
Otherwise you have to refer to
myClass.Property.Method
and
myClass.Property.ContentString.Substring(...)
and so forth
BHort 29-Oct-11 10:04am    
Ok ... I'm not quite sure how I gave you the impression that I cared if all the standard methods were hidden or not ... I don't care. I just want to be able to have my methods visible ... that's my goal, not hiding a bunch of other stuff.

So if I want myclass.mystring.mymethod to exist and work ... do I need to create a seperate class for mystring or can I create it all within myclass and how would the getter/setter and method be defined?

I tried Public ReadOnly Property MyString() As String but I couldn't figure out how to create the method to work on that rather than being a method for the class as a whole. So this created MyString and it worked but I couldn't figure out the method part of getting this to work as I'm hoping.
Sergey Alexandrovich Kryukov 30-Oct-11 0:52am    
My 5 for the explanation of the problem. After the explanation, the practical problem is reduced to the problem of creativity itself -- I tried to explain what to do with it, please see my solution.
--SA
Espen Harlinn 30-Oct-11 8:53am    
Hopefully this has made sense and someone can point me in the right direction - string is sealed and immutable
http://codebetter.com/patricksmacchia/2008/01/13/immutable-types-understand-them-and-use-them/

5'ed for pointing OP in the right direction - If you want a mutable string type, you have to roll your own
Your problem here is your submission to some intrusive thought, probably your own one. From a side view it looks like you came to some highly artificial requirement and suffer from a trouble to meet it.

The remedies are simple: some critical thinking and taking a more general look at the problem. That's why you approach to asking this question is wrong: first of all, you need to describe your ultimate goals. This way, you could get some practical advice. As Griff already explained, you cannot do literally what you want, but my note to this is: I don't think this is something you really interested in. Intrusive though is intrusive though — you should try to step out of it instead of ramming the problem as you see it.

—SA
 
Share this answer
 
Comments
BHort 31-Oct-11 11:45am    
Ok ... then how can I describe what I want.

I have a class myclass
The class has a string property (one of many properties) mystring
I want to process the string in a standard way but I don't want to give future programmers the impression that the method call is operating on anything other than just that string.
My thinking was that if the method call looked like

myclass.mystring.mymethod

It would be the most obvious that the method was only operating on the string.

I know how to create myclass.mymethod but that doesn't provide the opportunity for self documenting code.

Is there a way to do this or not.
BHort 31-Oct-11 11:54am    
I was thinking something like this might work:

Public Class mystringclass
Private mystring
Public Sub New(curString As String)
mystring = curString
End Sub
Public Function ProcStr() As String
Return somefunkyprocessingon_mystring
End Function
End Class
Public Class MyClass
Private MyStr As mystringclass
Other declarations here as well
Public Sub New(curStr As String, other items)
MyStr = New mystringclass(curStr)
other stuff
End Sub
End Class

I'm fairly new to .Net so would this allow me to have myclass.MyStr.ProcStr?

I know I've left out a lot of the plumbing ... I was trying to focus on just the core components ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900