locked
setting properties of one object RRS feed

  • Question

  • This is a syntax related question.

    There is a way of setting the Properties of one object similar to

    object name
    {
      .Property1 = A;
      .Property2 = B;
    }
    But I can't recall the syntax exactly. Please help!
    Monday, September 6, 2010 8:46 AM

Answers

  • Loose the dots:

    SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder{InitialCatalog = "A", DataSource = "B"};
    
    Monday, September 6, 2010 9:19 AM

All replies

  • object obj = new object{Property1 = "A", Property2 = "B"};

    Providing obj is of some type that has a property called Property1 and Property2

    Monday, September 6, 2010 8:57 AM
  • Sorry, but I don't see how this works for you. I'm getting a syntax error. Also, I remember this (what I'm asking about) was not at initialization time only.
    Monday, September 6, 2010 9:12 AM
  • You didn't just copy-paste it right? You have to replace "object" with the class you want to use, like:

    TextBox tb = new TextBox {Text = "SomeText"};
    
    Monday, September 6, 2010 9:14 AM
  • fair example of what I did:

              

     SqlConnectionStringBuilder sqlConnStrBuilder = new SqlConnectionStringBuilder
          {
            .InitialCatalog = "DatabaseName",
            .DataSource = "(local)"
          };

    this returns

    Syntax error, ',' expected.
    Monday, September 6, 2010 9:17 AM
  • Loose the dots:

    SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder{InitialCatalog = "A", DataSource = "B"};
    
    Monday, September 6, 2010 9:19 AM
  • I see now what the problem is. (the period mark).

    I've inserted it, because I'm trying to recall a different syntax. Your example is correct, though! Thanks!

    Do you, by any chance, know how I can set several properties (not  when initializing the object), by writing the object's name only once?

    Thanks!

    Monday, September 6, 2010 9:19 AM
  • I don't think that's possible.

    Monday, September 6, 2010 9:22 AM
  • The dots and the possibility to use it anywhere belong to VB, not C#.
    Monday, September 6, 2010 10:29 AM
  • Uh, I'm affraid you're right! Should anyone have a different answer, please let me know!
    Monday, September 6, 2010 10:54 AM