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

  • SEARCH
  • Home
  • 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 8938605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:39:57+00:00 2026-06-15T10:39:57+00:00

Good day, I have a class that is used to store a value of

  • 0

Good day,

I have a class that is used to store a value of Type T that I don’t know what the type will be until runtime. I want to unbox/cast, not sure what the correct term is, a specific type (in this case a nullable decimal) back to type object.

Please forgive my code layout:

The class snippet:

public abstract class Types
    {
        public class ValueField<T>
        {
            [XmlAttribute]
            public int TemplateID { get; set; }

            [XmlAttribute]
            public int FieldID { get; set; }

            [XmlIgnore]
            [ScriptIgnore]
            public TemplateApprovalField Field { get; set; }

            [XmlIgnore]
            [ScriptIgnore]
            public InstanceTemplateActivityValues Values { get; set; }

            [XmlAttribute]
            public T Value { get; set; }
        }
    }

The function snippet:

I am stuck at the line “values.Add((Types.ValueField)field);”, don’t know how to cast it. At that moment, var field is a Types.ValueField.

 Values = new Func<XmlDocument, List<Types.ValueField<object>>>(xml =>
 {
  List<Types.ValueField<object>> values = new List<Types.ValueField<object>>();

if (xml != null)
{
  foreach (XmlNode node in xml.SelectNodes("//Field"))
  {

    if (node.Attributes["Type"].Value == "Numeric")
      {
        var field = new Types.ValueField<decimal?>()
        {
          Field = ApprovalFields.Find(f => f.FieldID == int.Parse(node.Attributes["ID"].Value)),
          FieldID = int.Parse(node.Attributes["ID"].Value),
          TemplateID = int.Parse(node.SelectSingleNode("../@ID").Value)
        };
        field.Value = new Func<string, decimal?>(val =>
          {
            if (string.IsNullOrEmpty(val))
              return null;
            else
            {
              decimal parsed = 0;
              if (decimal.TryParse(val, out parsed))
                return parsed;
            }
            return null;
        })(node.InnerText);


      values.Add((Types.ValueField<object>)field); //This is where my problem occurs...
   }
 }


}
   return values;
   })(row["Form_Values"] != DBNull.Value ? 
      new XmlDocument() { InnerXml =  row["Form_Values"].ToString() } : null)

Any input will be greatly appreciated.

Regards
YP

  • 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-15T10:39:58+00:00Added an answer on June 15, 2026 at 10:39 am

    You’re creating a ValueField<decimal?>. That isn’t a ValueField<object> – your ValueField<T> class isn’t covariant in T, and couldn’t be. (Classes can’t be covariant, and your API includes T in “in” positions too.)

    To demonstrate why this mustn’t work:

     Value<decimal?> foo = new Value<decimal?>();
     Value<object> bar = foo; // Imagine this worked
     bar.Value = "hello";
     decimal? x = foo.Value;
    

    What would you expect that to do? Everything other than the second line is above reproach, so we must make the second line fail, which it does.

    The simple answer here is to create a Value<object> in the first place, instead of a Value<decimal?>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have a class that implements the LoaderCallbacks, and hence have the
Good Day, I have a Hibernate mapping that looks something like this: public class
Good day, I have created an object that will manage data access. My app
Good day everyone. I have an audio class, that plays a .wav file. But
Good day. I have a class that I’m going to use to generate math
Good day, I have the following problem: class B extends class A and methods
Good day I have a custom TextBox that has a IndicatorTextBox.ui.xml file as well
Good Day, I have a simple working routine in Perl that swaps two words:
I know that it is poor programming and architecture when you have a class
Good day, I am having an interesting problem that I cannot understand. I have

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.