Results 1 to 5 of 5

Thread: How to set the property name of an object based on a variable?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    774

    How to set the property name of an object based on a variable?

    How to set the property name of an object based on a variable?

    I expect the following, the _media_type is a variable that can contain "video", "image", "file" etc

    Code:
                 var _params = new
                {
                    _media_type = new 
                    {
                         caption = _message,
                         url = _media_url
                    }
                };

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,172

    Re: How to set the property name of an object based on a variable?

    You need to be less vague. What variable? Do you actually mean "property value" rather than "property name"? "video", "image" and "file" are not types so what will the data actually be? Does this _media_type type actually exist already? Please provide a FULL and CLEAR description with SPECIFIC details of what is and what isn't.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    774

    Re: How to set the property name of an object based on a variable?

    simple:

    string _media = "image";

    can I do the following?

    Code:
                 var _params = new
                {
                    _media_type = new 
                    {
                         caption = _message,
                         url = _media_url
                    }
                };

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,172

    Re: How to set the property name of an object based on a variable?

    I'm not sure that you know what "full", "clear" or "specific" mean. I'm going to try if you're not going to.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    774

    Re: How to set the property name of an object based on a variable?

    I have a string variable named _media, it will contain "video", "image", "audio", etc. I also have an object named _params (see below, there is a property named _media), I expect to have it dynamically based on the _media value (like macro), so for example if the media is "image" then it will be:

    Expected result:
    ------------------

    Code:
                 object _params = new
                {
                    image = new 
                    {
                         caption = _message,
                         url = _media_url
                    }
                };
    Code:
                 object _params = new
                {
                    _media_type = new 
                    {
                         caption = _message,
                         url = _media_url
                    }
                };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width