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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:22:20+00:00 2026-05-13T01:22:20+00:00

I am not sure if I have asked the question correctly. Please correct me

  • 0

I am not sure if I have asked the question correctly. Please correct me if I am wrong.

Anyways, we would like to use a variable’s value on a different phases of the page’s life cycle.

So for example,

public partial class TestUserControl: UserControl{
    public TestUserControl(){
        Objects = new List<object>(){
            Property1,
            Property2,
            Property3
        };
    }

    public int Property1 { get; set; }
    public bool Property2 { get; set; }
    public string Property3 { get; set; }
    public List<object> Objects { get; set; }

    protected override OnLoad(EventArgs e){
        foreach(var item in Objects){
            Page.Controls.Add(new LiteralControl(item.ToString() + "<br/>"));
        }
    }
}

So if we say the values of Property1, Property2, Property3 are set as they were written on the tag, how could we use the properties’ values just as when needed?

That on the constructor, instead of the values of the properties will be listed down on the List, only the Properties’ names will be listed down so their current values will be used on OnLoad.

Thanks a lot.


Edit: I have adopted deerchao’s and Jon’s approach, but there seems to be another problem we will be facing… It’s like:

public partial class TestUserControl: UserControl{
    public TestUserControl(){
        Objects = List<Func<object>>{
            () => Property1,
            () => Property2,
            () => Property3
        };
    }

    public int Property1 { get; set; }
    public bool Property2 { get; set; }
    public string Property3 { get; set; }
    public List<Func<object>> Objects { get; set; }

    protected override OnLoad(EventArgs e){
        foreach (var item in Objects) {
            //Here is the problem... can we get the variable name using this approach?
            //We have tried some other ways like reflection and expressions but to no avail. Help please =)
            string key = GetVariableName(item());

            string value = item() == null ? string.Empty : item().ToString();
            Page.Controls.Add(new LiteralControl(key + " : " + value + "<br/>"));
        }
    }
}
  • 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-13T01:22:20+00:00Added an answer on May 13, 2026 at 1:22 am

    You can pass variables by reference (using ref) but not properties. Additionally, the “ref-ness” is only for the duration of the method: it aliases the argument and the parameter, but if you copy the parameter value into another variable, that other variable isn’t an alias.

    It’s not really clear exactly what you want to do here, but you could use delegates instead:

        Objects = new List<Func<object>>(){
            () => Property1,
            () => Property2,
            () => Property3
        };
    

    Then:

    protected override OnLoad(EventArgs e){
        foreach(var itemRetriever in Objects){
            Page.Controls.Add(new Literal(itemRetriever().ToString() + "<br/>"));
        }
    }
    

    It’s pretty nasty though – I’d try to find an alternative design if you can.

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

Sidebar

Related Questions

Not sure what's going on here. I have a DateTime object, and when I
I have been told and I'm not sure I believe this: Removing white space
I have the following Clojure code and I'm not sure why it's not working:
I'm not sure if this is even possible but here goes: Currently I have
I'm not sure if the title is very clear, but basically what I have
I have a basic understanding of mock and fake objects, but I'm not sure
I have a function that resembles the one below. I'm not sure how to
I have a piece of fortran code, and I am not sure which standard
I have an extremely simple unit test. I'm not sure why it fails when
I have been using dean edwards ie7/8 script . Not sure if it's my

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.