private Basket createNewBasket(HttpContextBase httpContext)
{
//create a new basket.
//fisrt create a new cookie.
HttpCookie cookie = new HttpCookie(BasketSessionName);
//now create a new basket and set the creation date.
Basket basket = new Basket();
basket.DateBasket = DateTime.Now;
basket.Id = Guid.NewGuid();
//add and persist in the database.
baskets.Insert(basket);
baskets.Commit();
//add the basket id to a cookie
cookie.Value = basket.Id.ToString();
cookie.Expires - DateTime.Now.AddDays(1);
httpContext.Response.Cookies.Add(cookie);
return basket;
}
I have the following errors:
basket.Id = Guid.NewGuid();
the error is: cannot implicitly convert type "System.Guid" to "int"
Look's like you've defined Id as an integer so you will not be able to assign a Guid to it. Look to where Id is defined and change it if you need a Guid
A guid is a 128 bits. If .net supported int128, you’d could store a guid in an int. By convention, when a guid is converted to a string, it’s hex with dashes.
Member
6 Points
19 Posts
cannot implicitly convert type "System.Guid" to "int"
Feb 01, 2020 11:32 PM|lujain1996m|LINK
the full code:
I have the following errors:
the error is: cannot implicitly convert type "System.Guid" to "int"
and the other error:
How to fix them?
All-Star
160043 Points
13198 Posts
ASPInsiders
Moderator
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 02, 2020 12:26 AM|mbanavige|LINK
Look's like you've defined Id as an integer so you will not be able to assign a Guid to it. Look to where Id is defined and change it if you need a Guid
For this one, replace the - with an =
i.e.
Contributor
5961 Points
2466 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 02, 2020 05:26 AM|KathyW|LINK
To elaborate, GUID's are not integers, and can't be cast to integers. They are hex numbers (alphanumeric) with hyphens.
Contributor
4923 Points
4200 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 02, 2020 12:48 PM|DA924|LINK
A GUID can never be an integer.
All-Star
58164 Points
15647 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 02, 2020 06:00 PM|bruce (sqlwork.com)|LINK
A guid is a 128 bits. If .net supported int128, you’d could store a guid in an int. By convention, when a guid is converted to a string, it’s hex with dashes.
Contributor
5961 Points
2466 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 02, 2020 08:42 PM|KathyW|LINK
The OP was doing this:
NewGuid() does not produce an int. It produces the alphanumeric hex numbers with hyphens.
https://docs.microsoft.com/en-us/dotnet/api/system.guid.newguid?view=netframework-4.8
All-Star
58164 Points
15647 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 03, 2020 12:35 AM|bruce (sqlwork.com)|LINK
this is not true. NewGuid produces a structure of 128 bits (16 bytes). The ToString() is the hex with dashes.
Contributor
3710 Points
1043 Posts
Re: cannot implicitly convert type "System.Guid" to "int"
Feb 03, 2020 09:26 AM|Yongqing Yu|LINK
Hi lujain1996m,
As the previous replies, System.Guid cannot be converted to int type.
Therefore, it is recommended that you convert the Id field in the class Basket to a string type, and then receive Guid.NewGuid().ToString() again.
You can refer to this link for more details:
C# guid and SQL uniqueidentifier
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.