A question on string object

  • Thread starter Thread starter Saran
  • Start date Start date
S

Saran

Hi all,

Sorry if my question looks weird. But i still want to
understand. Let's see the below code

Dim myStr as string = "Hello"
Msgbox(myStr) ' Would display hello

I am wondering about the behaviour of the string as an object.
How comes it can receive and produce values without accessing any of
its properties. In other words, How to create a class that could behave
exactly like how String behaves.

i.e., For example, if i create a class called "MyOwnString"
What should i do so that below code can work

Dim myStr as MyOwnString = "Hello" ' Should get assgined
Msgbox(myStr) ' Should
display assigned value

Thanks,
Saran.
 
It's a good question, and I can give you a partial answer.

First, you must understand that strings are very special in .NET. The
functionality you describe works by calling special IL instructions, in this
case ldstr (load string). All assignment operators are that have a left hand
side string variable will use ldstr. There's no real way for you to do the
exact same thing.

As shown here:
http://blog.dreamprojections.com/archive/2005/04/05/768.aspx

you can overload the implicit operator back to the underying type to achieve
something somewhat like the same behaviour, but the underlyign
implementation is quite different.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top