Property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Property

    Hello,

    I have the following properties in a class:

    public List<ObjAA { get; set; }
    public string B { get; set; }

    B is always an array created from A.Name property in all A property.

    How can I define B as described? I am using Net 3.5 so I can use Linq.

    Thanks,
    Miguel
  • Jon Skeet [C# MVP]

    #2
    Re: Property

    On Sep 26, 1:24 pm, shapper <mdmo...@gmail. comwrote:
    I have the following properties in a class:
    >
        public List<ObjAA { get; set; }
        public string B { get; set; }
    >
    B is always an array created from A.Name property in all A property.
    B isn't an array at all, it's a string. Did you mean to declare it as
    a string[]?
    How can I define B as described? I am using Net 3.5 so I can use Linq.
    Should B really be writable? If not, you might want:

    public string[] B
    {
    get { return A.Select(x =x.Name).ToArra y(); }
    }

    Jon

    Comment

    • shapper

      #3
      Re: Property

      On Sep 26, 1:31 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On Sep 26, 1:24 pm, shapper <mdmo...@gmail. comwrote:
      >
      I have the following properties in a class:
      >
          public List<ObjAA { get; set; }
          public string B { get; set; }
      >
      B is always an array created from A.Name property in all A property.
      >
      B isn't an array at all, it's a string. Did you mean to declare it as
      a string[]?
      >
      How can I define B as described? I am using Net 3.5 so I can use Linq.
      >
      Should B really be writable? If not, you might want:
      >
      public string[] B
      {
          get { return A.Select(x =x.Name).ToArra y(); }
      >
      }
      >
      Jon
      I did a mistake: I want it to be in a CSV format so I changed your
      code to:
      string.Join(", ", return A.Select(x =x.Name).ToArra y());

      Is this ok? Maybe I could do this directly from the list?

      And yes it should be writable.

      Thanks,
      Miguel


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Property

        On Sep 26, 2:01 pm, shapper <mdmo...@gmail. comwrote:
        I did a mistake: I want it to be in a CSV format so I changed your
        code to:
        string.Join(", ", return A.Select(x =x.Name).ToArra y());
        >
        Is this ok? Maybe I could do this directly from the list?
        Your "return" is in the wrong place, but otherwise it should work.
        It's not terribly efficient (because ToArray will copy everything),
        but it'll work.
        And yes it should be writable.
        What will you make it do if someone writes to it then? Split the
        value, check the length is right and assign the names within A?
        Doesn't sound terribly nice.

        Jon

        Comment

        • shapper

          #5
          Re: Property

          On Sep 26, 2:09 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On Sep 26, 2:01 pm, shapper <mdmo...@gmail. comwrote:
          >
          I did a mistake: I want it to be in a CSV format so I changed your
          code to:
          string.Join(", ", return A.Select(x =x.Name).ToArra y());
          >
          Is this ok? Maybe I could do this directly from the list?
          >
          Your "return" is in the wrong place, but otherwise it should work.
          It's not terribly efficient (because ToArray will copy everything),
          but it'll work.
          >
          And yes it should be writable.
          >
          What will you make it do if someone writes to it then? Split the
          value, check the length is right and assign the names within A?
          Doesn't sound terribly nice.
          >
          Jon
          Yes, problem when typing. It is:
          string.Join(", ", A.Select(x =x.Name).ToArra y());

          So how should I do it to make it more efficient?

          The writable part is still under thinking ... this class is ViewData
          to communicate between a View and a Controller.

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Property

            On Sep 26, 2:13 pm, shapper <mdmo...@gmail. comwrote:
            Yes, problem when typing. It is:
              string.Join(", ", A.Select(x =x.Name).ToArra y());
            >
            So how should I do it to make it more efficient?
            Unfortunately I suspect you'd need to write your own implementation of
            String.Join. (It takes an array, right?)
            The writable part is still under thinking ... this class is ViewData
            to communicate between a View and a Controller.
            But does that part need to be modifiable?

            Jon

            Comment

            • shapper

              #7
              Re: Property

              On Sep 26, 2:25 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
              On Sep 26, 2:13 pm, shapper <mdmo...@gmail. comwrote:
              >
              Yes, problem when typing. It is:
                string.Join(", ", A.Select(x =x.Name).ToArra y());
              >
              So how should I do it to make it more efficient?
              >
              Unfortunately I suspect you'd need to write your own implementation of
              String.Join. (It takes an array, right?)
              >
              The writable part is still under thinking ... this class is ViewData
              to communicate between a View and a Controller.
              >
              But does that part need to be modifiable?
              >
              Jon
              I am a little bit confuse about this. I am using the following:

              public class PostPaper {

              public Post Post { get; set; }
              public List<TagTags {
              get;
              set {
              Tags = this.TagsCSV.Sp lit(',').Select (t =new Tag { Name =
              t.Trim() }).ToList();
              }
              }
              public string TagsCSV {
              get {
              return string.Join(", ", this.Tags.Selec t(t =>
              t.Name).ToArray ()); ;
              }
              set;
              }

              Sometimes I define Tags by converting from TagsCSV ... but sometime I
              can also do this using Linq as follows:

              PostPaper paper = (from p in database.Posts
              where p.PostID == id
              select new PostPaper {
              Post =
              p,
              Tags = (from pt in database.PostsT ags
              join t in database.Tags on pt.TagID
              equals t.TagID
              where pt.PostID == id
              orderby t.Name
              select t).ToList()
              }).SingleOrDefa ult();
              }

              Can I integrate this on my properties? Or maybe on my class?

              Thanks,
              Miguel

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Property

                shapper <mdmoura@gmail. comwrote:
                On Sep 26, 2:25 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
                On Sep 26, 2:13 pm, shapper <mdmo...@gmail. comwrote:
                Yes, problem when typing. It is:
                  string.Join(", ", A.Select(x =x.Name).ToArra y());
                So how should I do it to make it more efficient?
                Unfortunately I suspect you'd need to write your own implementation of
                String.Join. (It takes an array, right?)
                The writable part is still under thinking ... this class is ViewData
                to communicate between a View and a Controller.
                But does that part need to be modifiable?

                Jon
                I am a little bit confuse about this. I am using the following:

                public class PostPaper {

                public Post Post { get; set; }
                public List<TagTags {
                get;
                set {
                Tags = this.TagsCSV.Sp lit(',').Select (t =new Tag { Name =
                t.Trim() }).ToList();
                }
                }
                You've got a problem there to start with - you're assigning to Tags
                from within Tags, and you haven't provided a getter.

                Sometimes I define Tags by converting from TagsCSV ... but sometime I
                can also do this using Linq as follows:

                PostPaper paper = (from p in database.Posts
                where p.PostID == id
                select new PostPaper {
                Post =
                p,
                Tags = (from pt in database.PostsT ags
                join t in database.Tags on pt.TagID
                equals t.TagID
                where pt.PostID == id
                orderby t.Name
                select t).ToList()
                }).SingleOrDefa ult();
                }

                Can I integrate this on my properties? Or maybe on my class?
                I don't think we know nearly enough about what you're trying to do at
                this point, I'm afraid. I doubt that you really want to do a database
                lookup each time someone accesses a property though.

                --
                Jon Skeet - <skeet@pobox.co m>
                Web site: http://www.pobox.com/~skeet
                Blog: http://www.msmvps.com/jon.skeet
                C# in Depth: http://csharpindepth.com

                Comment

                Working...