Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ JAVA and C# _ Cannot Implicitly Convert 'object' To 'string'

Posted by: Ueberyak May 22 2014, 05:24 PM

In one bit of code I have these variables declared.
!--c1-->

CODE
string oSalesOrder = oROW.Cells[4].Value.ToString();
string oLineItem = oROW.Cells[5].Value.ToString();

Later on in the code I pass these variables to ContentReport.
CODE
ContentReport oSBXContent = new ContentReport(oSalesOrder,oLineItem);

When I get to the last line of the code below, I get the ol' "Cannot implicitly convert 'object' to 'string'. An explicit conversion exists (are you missing a cast?)" error.
It's driving me mad! I've been looking online to learn about the error, but I still don't see where an explicit conversion needs to take place.
CODE
public class ContentReport
{
    public string CustomerString { get; set; }
    public ContentReport(string oSalesOrder, string oLineItem)
    {
        // Read the Content Report raw data file. Each newline signifies a new record. Generates a raw string that contains the data.
        string oLine;
        string oProgramsRAW = "";
        System.IO.StreamReader file = new System.IO.StreamReader(Application.StartupPath.ToString() + @"\DOWNLOADS\" + oSalesOrder + "_" + oLineItem + ".txt");
        while ((oLine = file.ReadLine()) != null)
        {
            if (oLine.ToString() != "")
            {
                oProgramsRAW += oLine.ToString() + "|";
            }
        }
        file.Close();
        //Create a 3D array based off the string.
        string[] oProgramsRAWArray = oProgramsRAW.Split(new char[] { '|' });
        string oMaterial = "";
        string oMaterial_Desc = "";
        string oDVDNumber = "";
        int oCounter = 0;
        //Queries the ZP_Sched table in order to identify and capture the customer name for use on the report.
        //SBX_Downloader.MASTER_BACK_ENDDataSetTableAdapters.tbl_vw_ZPSchedTableAdapter tableAdapter = new SBX_Downloader.MASTER_BACK_ENDDataSetTableAdapters.tbl_vw_ZPSchedTableAdapter();
        //string oCustomer = tableAdapter.Customer_Name(oSalesOrder, oLineItem);
        SBX_Downloader.MillenniumDataSetTableAdapters.tbl_ZPSchedTableAdapter tableAdapter = new SBX_Downloader.MillenniumDataSetTableAdapters.tbl_ZPSchedTableAdapter();
        string oCustomer = tableAdapter.Customer_Name(oSalesOrder);

Posted by: jleach May 22 2014, 06:18 PM

>> string oCustomer = tableAdapter.Customer_Name(oSalesOrder); <<
The table adapter isn't going to return a string by default... try putting the cast there:
string oCustomer = tableAdapter.Customer_Name(oSalesOrder) as string;
// or
string oCustomer = (string)tableAdapter.Customer_Name(oSalesOrder);
hth

Posted by: Ueberyak May 23 2014, 11:36 AM

Jack! That worked perfectly. Thanks a bunch.