如何在vb.net中我自己的类中的属性上创建方法 [英] How to create a method on a property within my own class in vb.net

查看:146
本文介绍了如何在vb.net中我自己的类中的属性上创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这可能是一个愚蠢的简单问题,但我似乎找不到一个例子.

我想要的是创建一个与类的属性绑定的方法.

因此,例如,如果我有一个字符串mystr,则可以使用mystr.trim(),它是对我创建的字符串的一种方法.

我想在一个类中创建相同的效果.如果我有一个类myclass,我知道我可以轻松创建将被称为myclass.method的方法,但是我真正想做的是创建myclass.property.method,以明确说明该方法正在该属性上运行而不是课程的其他部分.

我以为我可以通过创建一个带有方法的子类来实现这一目标……但是我的属性是一个字符串,因此创建一个类来容纳字符串,只允许一个类似乎有点浪费.我创建一个仅适用于该字符串的方法...尤其是考虑到我仍然希望能够将其视为字符串而不是其他类型的对象.

希望这是有道理的,有人可以指出正确的方向.

谢谢.

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.

推荐答案

这里有两个详细信息:首先不能从字符串派生任何类.字符串类被密封以防止这种情况.

使myclass.property.method起作用但使property.method不起作用的唯一方法是使用您自己的类-无论如何您不能从字符串派生.

恐怕,我认为您需要回到绘图板上!
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!


您的问题是您对某些侵入性思想的服从你自己的.从侧面看,您似乎遇到了一些高度人为的要求,并且很难满足该要求.

补救措施很简单:一些批判性思维并更笼统地看待这个问题.这就是为什么您提出这个问题的方法是错误的:首先,您需要描述自己的最终目标.这样,您可以获得一些实用建议.正如格里夫(Griff)所解释的那样,您不能按照自己的意愿做任何事情,但是我要注意的是:我不认为这是您真正感兴趣的东西.闯入虽然是闯入的-但您应该尝试摆脱它,而不是如您所见,夯实问题.

—SA
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


这篇关于如何在vb.net中我自己的类中的属性上创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆