Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:32:07+00:00 2026-06-13T06:32:07+00:00

I have this cod: [Serializable] public class testC { string sir; public testC() {

  • 0

I have this cod:

 [Serializable]
    public class testC
    {
        string sir;
        public testC()
        {
            sir = string.Empty;
        }
        public string GetString
        {
            get { return sir; }
            set { sir = value; }

        }
    }
public class cookieTest
    {
        testC test;
        public cookieTest()
        {
            test = new testC();
        }
        public testC GetTestC
        {
            get
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["test"];
                test = cookie["first"] as testC;
                return test;
            }
            set
            {
                HttpCookie cookie = new HttpCookie("test");
                cookie.Expires = DateTime.Now.AddHours(8);
                cookie["first"] = value.ToString();
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }

And i get this error

Cannot convert type ‘string’ to ‘testC’ via a reference conversion,
boxing conversion, unboxing conversion, wrapping conversion, or null
type conversion

Is possible to get the object from cookie? Or I must to write into cookie all my data to set and get all data and create a new object to get?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:32:09+00:00Added an answer on June 13, 2026 at 6:32 am

    Is possible to get the object from cookie?

    No, not directly as you are trying to. Don’t think in terms of ASP.NET. Think in terms of what an HTTP cookie is in reality. It is an HTTP header. HTTP headers are only plain string values. The notion of object doesn’t exist in the HTTP protocol.

    So you will need to serialize the .NET object that you have into a string and then deserialize it back.

    There are different serializers in .NET that you could use. For example use the BinaryFormatter and then Base64 encode the resulting byte array to store into the cookie.

    The deserialization is the inverse process – you read the value from the cookie (which is always a string), then you Base64 decode it into a byte array which you deserialize back to the original object using the BinaryFormatter.

    Bear in mind though that the size of the cookies is limited and would vary between the different browsers. So don’t expect to put large objects into cookies. The value will be stripped and you would get corrupt data. I wouldn’t use them if the total serialized value of the object is larger than 2k characters.

    Let’s exemplify the process described earlier:

    public class cookieTest
    {
        testC test;
        public cookieTest()
        {
            test = new testC();
        }
        public testC GetTestC
        {
            get
            {
                var cookie = HttpContext.Current.Request.Cookies["test"];
                return Deserialize<testC>(cookie.Value);
            }
            set
            {
                var cookie = new HttpCookie("test");
                cookie.Expires = DateTime.Now.AddHours(8);
                cookie["first"] = Serialize(value);
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    
        private static string Serialize<T>(T instance)
        {
            using (var stream = new MemoryStream())
            {
                var serializer = new BinaryFormatter();
                serializer.Serialize(stream, instance);
                return Convert.ToBase64String(stream.ToArray());
            }
        }
    
        private static T Deserialize<T>(string value)
        {
            using (var stream = new MemoryStream(Convert.FromBase64String(value)))
            {
                var serializer = new BinaryFormatter();
                return (T)serializer.Deserialize(stream);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Model: public class EntryModel { [Required] public string Date { get; set; }
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have this string: 1001,Fitzsimmons, Des Marteau, Beale and Nunn,109,George,COD,Standard,,109,8/14/1998 8:50:02 What regular expression
I have this domain class. class Book { String code String description static mapping
I have a servlet program. This is my code: public class CompanionProxy extends HttpServlet
That sounds pretty odd but i have this cod and i need to convert
I have a SELECT statement like this: SELECT T1.COD, T1.NAME, (SELECT MAX(T2.DATA) FROM dbo.TAB2
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
I have two tables like this. Table1 Column | Type | ---------+------------------+ cod |
Please check this Entity: @Entity @Table(name = css_empresa) public class Empresa extends EntidadContactable implements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.