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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:22:42+00:00 2026-05-27T07:22:42+00:00

Given the following code; public class CustomControl { private object _dataItem; public object DataItem

  • 0

Given the following code;

public class CustomControl {
    private object _dataItem;

    public object DataItem {
        get { return _dataItem; }
        set { _dataItem = value; }
    }

    public void Update(ref string t) {
        t = "test";
    }
}

public class Consume {
    public void Example() {
        CustomControl ctrl = new CustomControl();
        ctrl.DataItem = anyObject.anyProperty;

        string prop = anyObject.anyProperty;
        ctrl.Update(ref prop);
        anyObject.anyProperty = prop;
    }
}

How can I change it so that the DataItem property is itself a reference, allowing you to pre-emptively set it to point to a variable thus allowing you to call Update() without any parameters.

So the Consume class would then look similar to;

public class Consume {
    public void Example() {
        CustomControl ctrl = new CustomControl();
        ctrl.DataItem = anyObject.anyProperty;
        ctrl.Update();

        // anyObject.anyProperty has been updated to "test"
    }
}

So the assigment of anyObject.anyProperty is then handled internally within CustomControl

  • 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-27T07:22:43+00:00Added an answer on May 27, 2026 at 7:22 am

    You need to store the act of setting something to a string, so store an Action<string>:

    public class CustomControl {
        public Action<string> SetData { get; set; }
    
        public void Update() {
            // TODO nullity check
            SetData("test");
        }
    }
    

    Then Consume would look like:

    public class Consume {
        public void Example() {
            CustomControl ctrl = new CustomControl();
    
            // store {the act of setting this property of this object to a string}
            ctrl.SetData = s => anyObject.anyProperty = s;
    
            ctrl.Update();
        }
    }
    

    The Update call will set anyObject.anyProperty to test. Note that you are storing specifically the act of setting this property of the particular anyObject you refer to in the assignment to SetData.


    To expand on the lambda: we want to create a value of type Action<string>, that is, a thing which takes a string and returns no result. Such a thing is going to be executable code. Prior to C# 3, to create a ‘value’ that was executable code, we would have had to do something like:

        ctrl.SetData = delegate(string s) { someObject.SomeProperty = s; };
    

    With this syntax it’s more obvious that we’re creating a method – it has a { } delimited body, it has statements in it, and it’s clear there is a string parameter that is used by the body.

    One thing achieved by lambda expressions in C# 3 is the ability to condense this down; loosely, the whole of

    // not compilable code
    delegate(parameters) { body; }
    

    can be replaced with

    // not compilable code
    (parameters) => body;
    

    and in the case where there’s only one parameter

    // not compilable code
    parameter => body;
    

    which is what we have here: the expression assigned to ctrl.SetData is a piece of behaviour that accepts a string (s) and sets anyObject.anyProperty to that string. The real power is in the way the C# compiler can work out the types to it know we’re creating an Action<string>.

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

Sidebar

Related Questions

Given the following code: public interface Selectable { public void select(); } public class
Given the following code, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public
Given the following object, public class Question { [Required] public string QuestionText { get;
given the following code: public static class Helpers { private static Char[] myChars =
Given the following code : package core; public abstract class GeometricElement { private float
Given the following Java code: public class Test { static private class MyThread extends
Given the following code : import java.io.*; public class Main { public static void
Given the following code: #pragma once class B { public: B(void) { } ~B(void)
Given the following code: Class User{ Task m_Task; public function getTask(Do work) { return
In c#, Given the following code: public class Person { public int PersonID {

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.