Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
When i ran the build i got this error "Cannot implicitly convert type 'DevotionJson.Devotion' to 'string'"

//This is my class
C#
public class Devotion
{
    public string Date { get; set; }
    public string Title { get; set; }
    public string Verse { get; set; }

    [JsonProperty("Read Chapter")]
    public string ReadChapter { get; set; }

    [JsonProperty("Read Text")]
    public string ReadText { get; set; }

    [JsonProperty("Bible In One Year")]
    public string BibleInOneYear { get; set; }

    public string Message { get; set; }
    public string Notes { get; set; }


    public override string ToString()
    {
        string[] props = new string[8];
        props[0] = "DATE - " + Date;
        props[1] = "TITLE - " + Title;
        props[2] = "VERSE - " + Verse;
        props[3] = "READ CHAPTER - " + ReadChapter;
        props[4] = "READ TEXT - " + ReadText;
        props[5] = "BIBLE IN ONE YEAR - " + BibleInOneYear;
        props[6] = "MESSAGE - " + Message;
        props[7] = "NOTES - " + Notes;
        return String.Join("\r\n\r\n", props);
    }                     
}

//This is my xaml.cs
C#
CustomMessageBox cmb;

private void openMsgBox(object sender, RoutedEventArgs e)
{
    // create new Stack panel
    StackPanel sp = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Vertical };

    //get policy text from file
    string devotion = getDevotion("MyDevotion/Devotion1.json");

    //Create new label
    TextBlock tb = new TextBlock()
    {
        Text = devotion,
        TextWrapping = TextWrapping.Wrap,
        FontSize = 25,
    };

    //Create new Scroll viewer
    ScrollViewer sv = new ScrollViewer()
    {
        VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
        Height = 700,
    };

    // add texblock in scroll viewer
    sv.Content = tb;

    //Add the controls to stack panel
    sp.Children.Add(sv);

    cmb = new CustomMessageBox()
    {
        // Set its content
        Content = sp,
        Opacity = 0.9,

        // Left button of message box Button
        LeftButtonContent = "Back",              
    };

    //Show the message box...
    cmb.Show();
}
        
public string getDevotion(string JsonfilePath)
{
    Devotion[] d = null;

    using (StreamReader r = new StreamReader(JsonfilePath))
    {
        string json = r.ReadToEnd();
        d = JsonConvert.DeserializeObject<devotion[]>(json);
    }
    return d[0];//**Error here**
}

Kindly help
Posted
Updated 23-Jan-15 3:14am
v2
Comments
ZurdoDev 23-Jan-15 8:33am    
So, what is the question? You can't convert your Devotion class to a string.
Member 10627743 23-Jan-15 8:44am    
yes!!!
ZurdoDev 23-Jan-15 8:46am    
Yes what?

1 solution

Your getDevotion method is expecting you to return a string but you are trying to return a Devotion.

 
Share this answer
 

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