1

I am currently working with displaying a list of items. I have created a method GetNextItem that returns obj1. When I am calling the method from the buttonClick I get this following error:

Cannot implicitly convert type 'TreeFarm.Form1.fruit_trees' to 'string'.

Not sure why is doing such thing.

public items_list GetNextItem()
{
    items_list obj1 = this.current_item;
    if (obj1 != null)
    {
        current_item = current_item.next_item;
    }
    return obj1;
}

ListForItems mainlist = new ListForItems();
private void ShowNextItem_Click(object sender, EventArgs e)
{

    labelSpecificItem.Text = mainlist.GetNextItem();         
}
3
  • 1
    what is the type of item_list ? Commented Dec 14, 2012 at 0:53
  • 1
    it's saying an items_list cannot be implicitly converted to a string (.Text = mainList.GetNextItem() instead of .Text = (String)mainlist.GetNextItem() or .Text = mainlist.GetNextItem().ToString()) Commented Dec 14, 2012 at 0:54
  • Is there a component of items_list that you want to display? Return that from your GetNextItem() method instead of the whole object. Commented Dec 14, 2012 at 0:56

4 Answers 4

5

You're trying to convert a value of type items_list in to a string (.Text is of String type). So, if this is one of your objects you can create an implicit cast operator or maybe try using .ToString() or explicitly casting it to a string using (String)mainlist.GetNextItem().

if you desire this kind of assignment and items_list is one of your objects, I would suggest the following addition to that class:

public static implicit operator String(items_list itemslist)
{
    return /* however you want to portray itemslist as a string */;
}

Otherwise you're going to have to rely on ToString() getting it right.

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

Comments

0

As other answers have mentioned, you cannot cast your items_list object to a string. If there is a component of items_list that you want to appear in your label, then assign that instead of the whole object:

labelSpecificItem.Text = mainlist.GetNextItem().textYouWantToSee;

Comments

0

Your items need to override the ToString() method, and then use that to convert them into strings.

Follow this pattern:

public class FruitTree 
{
    public string Name { get; set; }
   // your code
   public override string ToString()
   {
       return string.Format("A {0} tree.", Name);
   }
}
// later in the click handler
{
    labelSpecificItem.Text = tree_item.ToString();
} 

Comments

0

I had this problem myself, but that's because I forgot how it was done. This was my code before:

public static string MakeRequest(string GetCountry, string GetTime, string Server, string Database)
    {
        var filter = Builders<RequestAccess>.Filter;
        var getCountryfilter = filter.Eq(x => x.GetCountry, GetCountry);
        var getTimefilter = filter.Eq(x => x.GetTime, GetTime);
        var databasefilter = filter.Eq(x => x.Database, Database);
        var serverfilter = filter.Eq(x => x.Servers, Server);

        var makeRequest = RequestCollection.Find(filter.Or(getCountryfilter, getTimefilter, databasefilter, serverfilter)).ToList();

        return makeRequest;
    }

But the code below is the correct one :)

public static List<RequestAccess> MakeRequest(string GetCountry, string GetTime, string Server, string Database)
    {
        var filter = Builders<RequestAccess>.Filter;
        var getCountryfilter = filter.Eq(x => x.GetCountry, GetCountry);
        var getTimefilter = filter.Eq(x => x.GetTime, GetTime);
        var databasefilter = filter.Eq(x => x.Database, Database);
        var serverfilter = filter.Eq(x => x.Servers, Server);

        var makeRequest = RequestCollection.Find(filter.Or(getCountryfilter, getTimefilter, databasefilter, serverfilter)).ToList();

        return makeRequest;
    }

So that means if you're trying to make a list of something then dont use "string" in public static ... :)

3 Comments

Can you see string in OP's code? It's different issue
It's not an different issue.. He declares "Item_List" somewhere, and when he declares it, he have made a "string" otherwise it wouldn't give him that error
labelSpecificItem.Text = mainlist.GetNextItem(); this line is the source of error, Text should be set to some string but mainlist.GetNextItem(); returns item of type TreeFarm.Form1.fruit_trees