none
Populating a user chosen flag field programmatically RRS feed

  • Question

  • Hi,

    I'm struggling with an assignment I was given. While the code I have
    certainly works, I was wondering if there was a better way of doings
    things as it seems rather cumbersome.

    I've been asked to create a user form which after selecting a few tick
    boxes and clicking 'go' will populate a Task's flag field based on
    some calculations. This all works fine. The part I struggle with is
    that the user is allowed to select which of the 10 standard flag
    fields is being used.

    Initially I thought I could use a construct like below which has the
    advantage of keeping the code fairly straightforward:

    Private Sub ClearFlags()
      Dim jTask As Task
      Dim jFlag As PjField
      jFlag = pjTaskFlag2
      For Each jTask In ActiveProject.Tasks
        If Not (jTask Is Nothing) Then
          If jTask.GetField(FieldID:=jFlag) = True Then
    jTask.SetField FieldID:=jFlag, Value:=False
        End If
      Next jTask
    End Sub
    

    Unfortunately, the GetField and SetField methods do not like the boolean comparison and assignment as the values that are returned or stored are "No" and "Yes" (in English). While I could cater to this there is no guarantee that everyone has an English installation.

    The workaround I've now come up with is the following:

    Private Sub ClearFlags2()
      Dim jTask As Task
      Dim jFlag As PjField
      jFlag = pjTaskFlag2
      For Each jTask In ActiveProject.Tasks
        If Not (jTask Is Nothing) Then
          Select Case jFlag
          Case pjTaskFlag1
            If jTask.Flag1 = True Then jTask.Flag1 = False
          Case pjTaskFlag2
            If jTask.Flag2 = True Then jTask.Flag2 = False
          Case pjTaskFlag5
            If jTask.Flag3 = True Then jTask.Flag3 = False
          '
          ' More cases
          '
          Case Else
            'some exception
          End Select
        End If
      Next jTask
    End Sub
    
    

    As you can see the solution works but it's rather verbose to say the least. Any suggestions are welcome.

    Rgds,
    Thomas V.

    Wednesday, June 29, 2011 7:07 PM

Answers

  • Dag Thomas,

    Essentially I see no major improvements. You could win a few characters by using:

    jTask.flag1=false

    because the result of your if is always False.

    Another idea is to forget the select case and use:

    jtask.flag1=jtask.flag1 AND Not jflag=pjtaskflag1

    jtask.flag2=jtask.flag2 AND Not jflag=pjtaskflag2

    jtask.flag3=jtask.flag3 AND Not jflag=pjtaskflag3

    Groeten,

    Wednesday, June 29, 2011 7:46 PM
    Moderator

All replies

  • Dag Thomas,

    Essentially I see no major improvements. You could win a few characters by using:

    jTask.flag1=false

    because the result of your if is always False.

    Another idea is to forget the select case and use:

    jtask.flag1=jtask.flag1 AND Not jflag=pjtaskflag1

    jtask.flag2=jtask.flag2 AND Not jflag=pjtaskflag2

    jtask.flag3=jtask.flag3 AND Not jflag=pjtaskflag3

    Groeten,

    Wednesday, June 29, 2011 7:46 PM
    Moderator
  • Hi Thomas,

    Another direction you can take is to use reflection. Code looks better, but will be a lot more complicated and error prone.


    Regards, J
    Thursday, June 30, 2011 8:38 AM
  • Bedankt Jan,

    I figured I'd be stuck with this but it was worth a try :-)

    Obviously the ClearFlags() will always set to False. The main routine would require similar logic but was far more complex than necessary to illustrate my question.

    Thanks for the help.

    Rgds,
    Thomas V

     

    Thursday, June 30, 2011 8:44 AM
  • Graag gedaan!

    Thursday, June 30, 2011 10:45 AM
    Moderator