Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Does anyone know how to unbox or convert a "1" as a nullable long?

Does anyone know how to unbox or convert a "1" as a nullable long?

Scheduled Pinned Locked Moved C#
questiontutorial
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alaric_
    wrote on last edited by
    #1

    ...That pretty much says it all:

    private void SetValue(object instance, PropertyInfo property, object data)
    {
    //some preliminary checks
    if(property.PropertyType.IsAssignableFrom(data.GetType()))
    property.SetValue(instance, data, null);
    else
    {
    if(data is int)
    {
    int? unboxed = (int?)data;
    property.SetValue(instance, unboxed, null);
    }
    else
    property.SetValue(instance, Convert.ChangeType(data.ToString(), property.PropertyType), null);
    }

    I have an object that has a property of type long? and the value "1" cannot be pushed into it. The failure is inside the "if(data is int)" branch. "Object of type 'System.Int32' cannot be converted to type 'System.Nullable '1[System.Int64]' How do I go about getting a System.Int32 into a System.Nullable [System.Int64] ??

    "I need build Skynet. Plz send code"

    A 1 Reply Last reply
    0
    • A Alaric_

      ...That pretty much says it all:

      private void SetValue(object instance, PropertyInfo property, object data)
      {
      //some preliminary checks
      if(property.PropertyType.IsAssignableFrom(data.GetType()))
      property.SetValue(instance, data, null);
      else
      {
      if(data is int)
      {
      int? unboxed = (int?)data;
      property.SetValue(instance, unboxed, null);
      }
      else
      property.SetValue(instance, Convert.ChangeType(data.ToString(), property.PropertyType), null);
      }

      I have an object that has a property of type long? and the value "1" cannot be pushed into it. The failure is inside the "if(data is int)" branch. "Object of type 'System.Int32' cannot be converted to type 'System.Nullable '1[System.Int64]' How do I go about getting a System.Int32 into a System.Nullable [System.Int64] ??

      "I need build Skynet. Plz send code"

      A Offline
      A Offline
      Alaric_
      wrote on last edited by
      #2

      omg...the int? is still boxed, isn't it

      "I need build Skynet. Plz send code"

      A 1 Reply Last reply
      0
      • A Alaric_

        omg...the int? is still boxed, isn't it

        "I need build Skynet. Plz send code"

        A Offline
        A Offline
        Alaric_
        wrote on last edited by
        #3

        if((property.PropertyType ==(typeof(long))) || ((property.PropertyType == (typeof(long?)))))
        {
        long converted;
        if(Int64.TryParse(data.ToString(), out converted))
        property.SetValue(instance, converted, null);
        }

        Money

        "I need build Skynet. Plz send code"

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups