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 6979309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:12+00:00 2026-05-27T17:52:12+00:00

Possible Duplicate: How can I code a C# function to accept a variable number

  • 0

Possible Duplicate:
How can I code a C# function to accept a variable number of parameters?

I have the following class:

public class Product : AuditableTable  
{
    public string Position { get; set; }
    public string Quantity { get; set; }
    public double Location { get; set; }
}

What I need is to be able to update fields in the class with the following function.

Parameters:

  • ac and pr define the keys and enable me to get an instance of the class.
  • fld is the field name of the class to update. It could be “Position”, “Quantity” or “Location” or ??
  • val is the value. It could be something like “London” or “1.234”

How can I arrange for the field name to be set dynamically without using a case statement to
check each value of fld and many different setters. Also if there’s some way of setting the field
dynamically how do I deal with casting it to the correct object type for that field?

    public void Update(string ac, string pr, string fld, string val) 
    { 
        try 
        { 
            vm.Product = _product.Get(ac, pr);
            vm.Product. xxx = fld 
        }
        catch (Exception e) { log(e); }
    }

Update

Here’s the solution proposed by Pieter:

public void Update(string ac, string pr, string fld, string val) { 
            try { 
                vm.Product = _product.Get("0000" + ac, pr);
                if (vm.Product != null)
                {
                    var property = vm.Product.GetType().GetProperty(fld);
                    var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                    val = Convert.ChangeType(val, type);
                    property.SetValue(vm.Product, val, null); 
                } 
                _product.AddOrUpdate(vm.Product);
            }
            catch (Exception e) { log(e); }
        }
  • 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-05-27T17:52:13+00:00Added an answer on May 27, 2026 at 5:52 pm

    You can use reflection:

    vm.Product.GetType().GetProperty(field).SetValue(vm.Product, val, null);
    

    SetValue requires that the value is of the correct type. If this isn’t the case, you can convert it with the following:

    var property = vm.Product.GetType().GetProperty(field);
    
    // Convert.ChangeType does not work with nullable types, so we need
    // to get the underlying type.
    
    var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
    
    object convertedValue = Convert.ChangeType(val, type);
    
    property.SetValue(vm.Product, convertedValue, null);
    

    This automatically converts val to the type of the property before assigning it.

    Note however that both using reflection and Convert.ChangeType are horribly slow. If you do this a lot, you should have a look at DynamicMethod.

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

Sidebar

Related Questions

Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Possible Duplicate: In MATLAB, can I have a script and a function definition in
Possible Duplicate: How to convert this code to use string I have a function
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside
Possible Duplicate: Are there gotchas using varargs with reference parameters Hi, I have a
Possible Duplicate: What's the role of the parentheses in the following piece of code?
Possible Duplicate: In Perl, how can I check from which module a given function

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.