0

I have managed to crunch down a several line code to this

For Each gal In galleries
   With New HtmlGenericControl("div")
                .ID = gal.Header
                .Controls.Add(New HtmlImage() With {.Src = "http://p.com/pic.jpg"})

                galleryContent.Controls.Add(**Me**)
   End With
Next

I cannot find any where how to reference back to the object i am currently working with to add the control back to 'galleryContent' - Using plain me crashes the whole web server... whooops.

Using does not offer the shorter hand of just using . - But it Using the only way to do it? I was sersiosly expecting some kind of .Me or something like this

Any ideas?

overslacked
  • 4,127
  • 24
  • 28
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
  • 1
    [Can't](http://stackoverflow.com/questions/1152983/how-to-access-the-object-itself-in-with-end-with). Even in the similar C# construct, you can't. – AakashM Apr 04 '12 at 15:57
  • 2
    Not attempting to answer your question, but if you get this solved, a good unit test for it would be to give it to another developer and ask him to figure out what it's trying to do. Readability is King, debuggability is heir to the Throne. – Adam Houldsworth Apr 04 '12 at 15:57
  • That is the beauty of .NET-VB.. It only makes readable sense as long as its as short as possible. in C# compacting like this will be horrendous, but C# has other benefits. – Piotr Kula Apr 04 '12 at 16:00
  • 1
    @ppumkin, I urge you to consider that readable, debuggable code may be a far better metric of quality than using the fewest number of characters. – overslacked Apr 04 '12 at 17:42

3 Answers3

5

You can't. At least not that way.

Try this:

For Each gal In galleries 
   Dim obj as New HtmlGenericControl("div") 
   With obj
        .ID = gal.Header 
        .Controls.Add(New HtmlImage() With {.Src = "http://p.com/pic.jpg"}) 

        galleryContent.Controls.Add(ojb) 
   End With 
Next 
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Alas,, This is the method I wrote an hour ago :-) But I thought I might try and squash it down and see how far I can go. But embedding anonymous functions proves to be unnatural in VB.. Better use Linq most likely? Got an example ? :-) – Piotr Kula Apr 04 '12 at 16:12
2

Mystere Man's is right, but think about that.... that will affect the code readability.

"Code is read much more often than it is written, so plan accordingly"

I would suggest to avoid using "With" in VB ... unless you really "have to".

Hope it helps.

Sebastian Siek
  • 2,045
  • 17
  • 16
1

You can do something like:

For Each gal in galleries
    galleryContent.Controls.Add(New HtmlGenericControl("div") With { .ID = gal.Header })
    galleryContent.Controls[galleryContent.Controls.Count - 1].Controls.Add(New HtmlImage() With {.Src = "http://p.com/pic.jpg"})
Next

But this is purely for line-reduction. It severely reduces readability so in any normal scenario you shouldn't be aiming to just cut down on lines of code. Readability is far more important than "line-efficiency".

Jason Larke
  • 5,289
  • 25
  • 28
  • makes sense.. I needed to wrap the adding of the anonymous function to the top level.. no wonder :) very nice.. testing now! – Piotr Kula Apr 04 '12 at 16:01
  • It does not work.. because the .Controls.Add does not exists as a property of HtmlGeneric control, but a function.. so bummer.. And yes.. Its starting to look a but odd. – Piotr Kula Apr 04 '12 at 16:07
  • Okay, that makes sense. I wasn't really thinking when I wrote out the snippet. Out of curiosity, does your "galleryContent.Controls" have a "AddRange" method? – Jason Larke Apr 04 '12 at 16:14