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 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

I'm not sure if I asked the question correctly. I have some code I
I'm not sure I've asked that question correctly, but hear me out... In my
I'm not exactly sure if this is the correct question to be asked but
I know similar questions have been asked, but I'm not sure about the answers
I am not sure if this was asked before but I have to do
not sure its a stack overflow question I have a Mac and am hosting
another memory management question: I have asked this before, but did not really get
I'm not sure if this question has been asked before, but I can't seem
I have the below question, and would like to know if I have interpreted
I have asked this question before with other adapters like SimpleAdapter but the solution

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.