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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:38:21+00:00 2026-05-20T06:38:21+00:00

Say I have a class with three string properties: public class Foo { public

  • 0

Say I have a class with three string properties:

public class Foo
{
  public string Bar1 { get; set; }
  public string Bar2 { get; set; }
  public string Bar3 { get; set; }
}

Now say I want to assign to one of the string properties, but that which of the three properties I assign to depends upon some condition. Knowing that strings are supposedly reference types, I might be tempted to write some code like this:

string someString;
if (condition1) someString = foo.Bar1;
else if (condition2) someString = foo.Bar2;
else if (condition3) someString = foo.Bar3;
someString = "I can't do that, Dave.";

This doesn’t work. I know it’s got something to do with string immutability (at least I think it does) but I haven’t any idea how to do it.

Strings basically confuse the bejesus out of me.

Um, yeah, so my question is what’s the most concise way to do this?

  • 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-20T06:38:22+00:00Added an answer on May 20, 2026 at 6:38 am

    Personally I would probably just go ahead and assign the property:

    string value = "I can't do that, Dave.";
    if (condition1) foo.Bar1 = value;
    else if (condition2) foo.Bar2 = value;
    else if (condition3) foo.Bar3 = value;
    

    If you really want to use the approach you suggest, you can wrap it in a delegate I guess:

    Action<string> assignString;
    if (condition1) assignString = s => foo.Bar1 = s;
    else if (condition2) assignString = s => foo.Bar2 = s;
    else if (condition3) assignString = s => foo.Bar3 = s;
    assignString("I can't do that, Dave.");
    

    …but in this case that would only make things unnecessarily complex. For the kind of scenario that is described in the question, I can’t think of any reason you would want to do this.

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

Sidebar

Related Questions

Let's say I have a class: class Foo { public string Bar { get
Let's say I have: public class Item { public string SKU {get; set; }
Let's say I have a class which has three properties as below. public class
Say I have three classes: class X{}; class Y{}; class Both : public X,
Say I have class A with class A { final String foo() { //
Let's say I have the following two classes: public class Person { public string
Say I have a class named Frog, it looks like: public class Frog {
Let's say I have following classes. (only most important things included) public class Client
Say I have public class family { public person father; public person mother; public
I have a class: public class SomeClass { // properties and methods here }

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.