Author Topic: Small but useful tip (I think) ...  (Read 5795 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Small but useful tip (I think) ...
« on: April 13, 2005, 07:40:56 PM »
In VB(A) one frequently simplifies one's code by using the "With" statement to eliminate redundant object references and/or make code more maintainable.

Instead of --

Code: [Select]
myObject.Method1 parameters
myObject.Method2 parameters

You might do this --

Code: [Select]
With myObject
    .Method1 parameters
    .Method2 parameters
End With

Thank you Michael, been doing that for years, move on son.

Hang on a sec.

What you might not realize is that you can use the "With" statement with any expression that returns an object or type.

Eh?

Ok, a quick, but real example. Say you're adding items to a listview object and each entry has sub items. Here's a typical approach --

Code: [Select]
Dim Item As ListItem    

Set Item = listview1.ListItems.Add(, , "Item")
Item.SubItems(1) = "Detail 1"
Item.SubItems(2) = "Detail 2"

Which can be simplified to (and frequently is) --

Code: [Select]
Dim Item As ListItem    

Set Item = listview1.ListItems.Add(, , "Item")
With Item
    .SubItems(1) = "Detail 1"
    .SubItems(2) = "Detail 2"
End With

But what about --

Code: [Select]
With listview1.ListItems.Add(, , "Item")
    .SubItems(1) = "Detail 1"
    .SubItems(2) = "Detail 2"
End With

:cheesy:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Small but useful tip (I think) ...
« Reply #1 on: April 13, 2005, 09:10:49 PM »
This is the kind of tip that will allow good programmers to become great....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

TR

  • Guest
Small but useful tip (I think) ...
« Reply #2 on: April 13, 2005, 09:50:50 PM »
Good tip. I use "with" whenever I can as I'm lazy and don't like to type. Also it produces nicer looking code.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Small but useful tip (I think) ...
« Reply #3 on: April 14, 2005, 10:34:35 AM »
Thanks guys, wasn't sure if it would be useful for or not but that apparently couldn't stop me from posting it. This of course has never happened to me before.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Small but useful tip (I think) ...
« Reply #4 on: May 07, 2005, 11:52:21 AM »
And going further:

Code: [Select]
With MyGrid
  Set .DataSource = MyRecSet
  With .Columns(1)
    .Caption = "Cap1"
    .Width = 100
    .AllowSizing = False
    .Locked = True
  End With
  With .Columns(2)
    .Caption = "Cap2"
    .Width = 200
    .AllowSizing = False
    .Locked = True
  End With
  '...
End With


Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Small but useful tip (I think) ...
« Reply #5 on: May 07, 2005, 11:58:30 AM »
Good stuff Jürg, thanks.

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Small but useful tip (I think) ...
« Reply #6 on: May 08, 2005, 10:33:16 AM »
Glad to contribute... :D

Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Small but useful tip (I think) ...
« Reply #7 on: May 08, 2005, 10:36:44 AM »
You have a lot to contribute Jürg, hope we see you here frequently.

:cheesy:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Small but useful tip (I think) ...
« Reply #8 on: May 10, 2005, 03:38:48 PM »
Probably yes, but my greatest problem is to find enough time to scan/read all the threads in this forum. Maybe 'cause I'm not (yet) familiar with web based groups like TheSwamp. :(

Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

M-dub

  • Guest
Small but useful tip (I think) ...
« Reply #9 on: May 10, 2005, 03:43:17 PM »
One tip for you...
Create a Bookmark / Favourite and give it this address:

http://www.theswamp.org/phpBB2/search.php?search_id=newposts&sid=47b0892991d610d7b8377df9ea3906a0


(View posts since last visit)

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Small but useful tip (I think) ...
« Reply #10 on: May 11, 2005, 04:13:56 PM »
Quote from: M-dub
One tip for you...
Create a Bookmark / Favourite and give it this address:

Thx, that was it...

Cheers
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18