Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Controller
Hi I got cannot implicitly convert type object to system.guid? An Explicit conversion exists (are you missing a cast?) error in this line taxinfotaxfiled.TaxFieldID = i.Value; Especially in "i" I gave "Value" (i.Value) but still showing same error. please any one suggest me the solution?

C#
public ActionResult Create(TaxInfoTaxFiled taxinfotaxfiled)
        {
           ArrayList Alist = new ArrayList();
            {
                Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
                Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
                Alist.Add("64B512E7-46AE-4989-A049-A446118099C4");
                Alist.Add("376D45C8-659D-4ACE-B249-CFBF4F231915");
                Alist.Add("59A2449A-C5C6-45B5-AA00-F535D83AD48B");
                Alist.Add("03ADA903-D09A-4F53-8B67-7347A08EDAB1");
                Alist.Add("2F405521-06A0-427C-B9A3-56B8931CFC57");
            }

            if (ModelState.IsValid)
            {
                taxinfotaxfiled.TaxInfoTaxFieldID = Guid.NewGuid();

                foreach (var i in Alist)
                {
                    taxinfotaxfiled.TaxFieldID = i.Value;
                   }

                db.TaxInfoTaxFileds.Add(taxinfotaxfiled);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

return View(taxinfotaxfiled);
        }



My Model

C#
public partial class TaxInfoTaxFiled
    {
        public System.Guid TaxInfoTaxFieldID { get; set; }
        public Nullable<System.Guid> TaxInfoID { get; set; }
        public Nullable<System.Guid> TaxFieldID { get; set; }
        public Nullable<System.Guid> FieldTypeID { get; set; }
        public string FieldValue { get; set; }
}
  public partial class TaxField
    {
        public System.Guid TaxFieldID { get; set; }
        public string DisplayName { get; set; }
        public string PrintName { get; set; }
}
Posted
Updated 6-Dec-15 18:16pm
v2

Why are you even bothering with ArrayList? It's pretty old and just about every piece of code that uses it can use a List<t> in its place.

The reason you get the error is because ArrayList always returns an Object, not necessarily the type you put in. In your case, a string that looks like a GUID. Replace the ArrayList with a List<guid> and you'll solve the problem.
List<guid> AList = new List<guid>();
AList.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
AList.Add(new Guid("64B512E7-46AE-4989-A049-A446118099C4"));
...</guid></guid>
 
Share this answer
 
Comments
Member 12087373 7-Dec-15 2:37am    
ok lets i have a try..
To answer the question, arraylist can contain anything so you are adding strings (the string could be represented as a GUID, but that's irrelevant, the string could contain "Hello world") which are stored as object references. To get the original type back you need to cast the object in the arraylist to whatever type it was when added, so you would need to cast it to a string. Once back as a string you need to use Guid.Parse (or TryParse) to create a GUID from the data that is in the string. So your code should be like this;

C#
foreach (var i in Alist)
{
    taxinfotaxfiled.TaxFieldID = Guid.Parse((string)i);
}


An alternative would be to store GUIDs in the ArrayList so you don't need to bother parsing;

C#
ArrayList Alist = new ArrayList();
{
    Alist.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
    Alist.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
    // ....
}

foreach (var i in Alist)
{
    taxinfotaxfiled.TaxFieldID = (Guid)i;
}


As the other solution suggests, though, it is far better to use typed List objects like List<Guid> as ArrayList is old from before generics came along and let us do this stuff strongly-typed.

The next issue is that you're looping through each item in the arraylist and overwriting the TaxFieldID property with each item, so the overall effect is that the TaxFieldID is going to be the last value in the list, so that probably isn't the behaviour you are expecting.
 
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