2

What I'd like to be able to do is have a struct return a variable without having to specify it by it's property. I'm probably phrasing this question incorrectly so I'll give an example (this fails of course). Apologies for any confusion or inconsistancies but this is what I'd like to achieve and not sure how to do this or even go about searching for it.

struct theString{  
    public string my_String;  
}

theString myString = new theString();  
string currentMethod = myString.my_String;  
string how_I_would_like_it = myString; 

Thanks in advance for any/all help.

[edit] The property string is just for the example. You can replace string with anything you'd prefer, be it int, object, webhttprequest, etc...

The problem is that when I do the string how_I_would_like_it = myString; It won't assign the my_String variable to how_I_would_like_it and the cannot implicitly convert error.

3
  • 1
    I'm not sure I understand this. It looks like from string how-I-would-like-it = myString that you're asking for an implicit conversion from your struct type theString to string. If that's the case, why not just specify ToString()? Commented Dec 15, 2009 at 12:46
  • The OP wants the default implementation found in VB. Commented Dec 15, 2009 at 12:46
  • 1
    btw; public fields are not a good idea, nor are mutable structs. By all means write a struct, but don't make it mutable! Seriously, they are evil. Commented Dec 15, 2009 at 12:50

2 Answers 2

4

You could do something like this:

public class MyString
{
    public static implicit operator string(MyString ms)
    {
        return "ploeh";
    }
}

The following unit test succeeds:

[TestMethod]
public void Test5()
{
    MyString myString = new MyString();
    string s = myString;
    Assert.AreEqual("ploeh", s);
}

In general however, I don't think this is a sign of good API design, because I prefer explicitness instead of surprises... but then again, I don't know the context of the question...

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer, but just for completeness: there is no fundamental need for it to be a class in this scenario- it could be a struct (ideally immutable with a property accessor).
@Marc Gravell: Yes I considered a struct as well, but then I would have had to override Equals and GetHashcode and whatnot, or you (or someone else) would have pointed fingers at me for that reason instead :) So to get the point across, I chose the simpler route, by I agree that a struct would make a lot of sense.
It works, thanks :) But is using the "implicit operator" bad or improper? I'd prefer to learn the correct way to do things the first time.
Implicit operators aren't necessarily bad, but you should only use them when the conversion involves no loss of information because conversions may happen inintended if developer isn't aware that the operator exists. I tend to prefer more explicit APIs myself, but YMMV.
0

If you only want one string value returned you can try overload the struct's ToString method:

public override string ToString()
{
  return my_String;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.