1

I'm trying to convert an object array of my class to a string array.

In this example I have two classes. In one of my classes I want to creat two variables, objects of the other class. On variable will be a array and the other a "regular" variable.

Two classes name. ShowResult and Game.

In the Game class I write:

private ShowResult[] _showResults;
private ShowResult _showResult;

In my properties in my Game class I try to convert this two variables to string array and string:

    public string[] ShowResults
    {
        get { return _showResults.ToString().ToArray(); }
        set { _showResults.ToString().ToArray() = value; }
    }

    public string ShowResult
    {
        get { return _showResult.ToString(); }
        set { _showResult = value; }
    }

This doesn't work. I really want my custom object to be converted to string array and to string. But I get an error.. I know I can, but I don't know just how..

If anyone has any suggestion I would be greatful. OBS, this is just an example. But still I don't get why it wont work..?

By the way, sorry for my bad english. ;)

Best regards

3
  • msdn.microsoft.com/en-us/library/w86s7x04(v=vs.80).aspx Commented Sep 24, 2012 at 10:21
  • Do you have code that parses a string into an instance of your ShowResult? You will need to use that in your property setter. Commented Sep 24, 2012 at 10:27
  • Good point Hans Kesting. No I don't. Do you have any ideas how to parse a string array to my ShowResult array? Hmm. Commented Sep 24, 2012 at 12:19

2 Answers 2

4

Try this:

_showResults.Select(a => a.ToString()).ToArray()
Sign up to request clarification or add additional context in comments.

Comments

0

Given the nature of your code (i.e. strings appear to map directly to ShowResults instances), you might want to consider implementing implicit operators:

ShowResults s = "hello";
string ss = s;
Console.WriteLine(ss); // "hello"



class ShowResults
{
    public string SomeProp
    {
        get; private set;
    }
    public static implicit operator ShowResults(string s)
    {
        //this is where you'd parse your string
        //to form a valid ShowResults
        return new ShowResults{SomeProp = s};
    }
    public static implicit operator string(ShowResults s)
    {
        return s.SomeProp;
    }
}

1 Comment

Very intresting solution! Thank you for your time. :)

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.