Results 1 to 9 of 9

Thread: make my code better - the IIF statement?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    Hi there,

    Over a month ago I posted a message on making a random number of numbers, from 4 to 8 numbers, appear in sequence:

    e.g: 1,3,4,7 or 3,7,8,10,12,13 etc....

    I got some useful code but it was way over my head (for the moment), so I tried to do it in a way I could relate to.

    I managed to do it using the code below, but I'd appreciate it someone could help me cut it down a bit - I think the IIF statement can do it, but I don't know how.

    Anyway, here it goes:

    'a command button and picturebox
    Private Sub Command1_Click()
    Picture1.Cls
    Randomize
    'decide the number of numbers, from 4 to 8, to print
    Dim numbertoprint As Integer
    numbertoprint = Int(Rnd * 5) + 4
    Dim firstofsequence As Integer
    Dim secondofsequence As Integer
    Dim thirdofsequence As Integer
    Dim fourthofsequence As Integer
    Dim fifthofsequence As Integer
    Dim sixthofsequence As Integer
    Dim seventhofsequence As Integer
    Dim eigthofsequence As Integer
    firstofsequence = Int(Rnd * 4) + 1

    secondofsequence = firstofsequence + Int(Rnd * 3) + 1
    thirdofsequence = secondofsequence + Int(Rnd * 3) + 1
    fourthofsequence = thirdofsequence + Int(Rnd * 3) + 1
    fifthofsequence = fourthofsequence + Int(Rnd * 3) + 1
    sixthofsequence = fifthofsequence + Int(Rnd * 3) + 1
    seventhofsequence = sixthofsequence + Int(Rnd * 3) + 1
    eigthofsequence = seventhofsequence + Int(Rnd * 3) + 1

    If numbertoprint = 4 Then
    'Can I use the IIF statement here?
    Picture1.Print firstofsequence & "," & secondofsequence & "," & _
    thirdofsequence & "," & fourthofsequence
    End If
    If numbertoprint = 5 Then
    Picture1.Print firstofsequence & "," & secondofsequence & "," & _
    thirdofsequence & "," & fourthofsequence & "," & fifthofsequence
    End If
    If numbertoprint = 6 Then
    Picture1.Print firstofsequence & "," & secondofsequence & "," & _
    thirdofsequence & "," & fourthofsequence & "," & fifthofsequence & _
    "," & sixthofsequence
    End If
    If numbertoprint = 7 Then
    Picture1.Print firstofsequence & "," & secondofsequence & "," & _
    thirdofsequence & "," & fourthofsequence & "," & fifthofsequence & _
    "," & sixthofsequence & "," & seventhofsequence
    End If
    If numbertoprint = 8 Then
    Picture1.Print firstofsequence & "," & secondofsequence & "," & _
    thirdofsequence & "," & fourthofsequence & "," & fifthofsequence & _
    "," & sixthofsequence & "," & seventhofsequence & "," & eigthofsequence
    End If
    End Sub

    Thanks for any help!

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Your code can be shortened by using an array for the eight numbers rather than 8 separate variables. I'll show you that below. But first, regarding the IIf function: you would generally use that to assign a different value to the same variable, depending on a condition. For example, you could write:
    Code:
    strMessage = IIf(intStudentGrade >= 60, "Passed", "Failed")
    instead of:
    Code:
    If intStudentGrade >= 60 Then
        strMessage = "Passed"
    Else
        strMessage = "Failed"
    End If
    The IIf function will not help in stringing the numbers together (I use a For loop to do that), but I was able to use it to determine whether or not a comma is needed to separate one number from the next (check out the code to see what I mean).

    Anyway, here's a modification of your code, using an array:
    Code:
    Private Sub Command1_Click() 
    
        Dim numbertoprint As Integer 
        Dim intSeqNbr(1 To 8) As Integer 
        Dim intX As Integer
        Dim strPrintData As String
        Dim strCommaOrNot As String
    
        Picture1.Cls 
        Randomize 
    
        'decide the number of numbers, from 4 to 8, to print 
        numbertoprint = Int(Rnd * 5) + 4 
    
        intSeqNbr(1) = Int(Rnd * 4) + 1 
        For intX = 2 To 8
            intSeqNbr(intX) = intSeqNbr(intX - 1) + Int(Rnd * 3) + 1 
        Next
    
        strPrintData = ""
    
        For intX = 1 to numbertoprint
             ' Here's where you can use IIf:
             strCommaOrNot = IIf(intX = 1, "", ",")
             ' The above is the same as saying:
             ' If intX = 1 Then
             '     strCommaOrNot = ""
             ' Else
             '     strCommaOrNot = ","
             ' End If
             strPrintData = strPrintData & strCommaOrNot & intSeqNbr(intX)
        Next
    
        Picture1.Print strPrintData
    
    End Sub


    [Edited by BruceG on 08-14-2000 at 03:07 PM]
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    no, a select statment would be more appropriate.

    I'd also use an array...
    Code:
    Option Explicit
    'a command button and picturebox
    Private Sub Command1_Click()
    Picture1.Cls
    Randomize
    'decide the number of numbers, from 4 to 8, to print
    Dim numbertoprint As Integer
    numbertoprint = Int(Rnd * 5) + 4
    Dim sequence(7) As Integer
    Dim i As Integer
    
    sequence(0) = Int(Rnd * 4) + 1
    For i = 1 To 7
        sequence(i) = sequence(i - 1) + Int(Rnd * 3) + 1
    Next i
    
    Select Case numbertoprint
        Case 4
            Picture1.Print sequence(0) & "," & sequence(1) & "," & _
            sequence(2) & "," & sequence(3)
        
        Case 5
            Picture1.Print sequence(0) & "," & sequence(1) & "," & _
            sequence(2) & "," & sequence(3) & "," & sequence(4)
    
        Case 6
            Picture1.Print sequence(0) & "," & sequence(1) & "," & _
            sequence(2) & "," & sequence(3) & "," & sequence(4) & _
            "," & sequence(5)
            
        Case 7
            Picture1.Print sequence(0) & "," & sequence(1) & "," & _
            sequence(2) & "," & sequence(3) & "," & sequence(4) & _
            "," & sequence(5) & "," & sequence(6)
            
        Case 8
            Picture1.Print sequence(0) & "," & sequence(1) & "," & _
            sequence(2) & "," & sequence(3) & "," & sequence(4) & _
            "," & sequence(5) & "," & sequence(6) & "," & sequence(7)
    End Select
    End Sub
    iif() can be used instead of if else

    eg
    Code:
    Dim strtemp As String
    Dim i As Integer
    i = 4
    
    strtemp = IIf(i = 0, "i was nil", "i was <> 0")
    
    MsgBox strtemp
    
    'this is the same as :
    
    If i = 0 Then
        strtemp = "i was nil"
    Else
      strtemp = "i was <> 0"
    End If
    MsgBox strtemp
    IIFs are thought to be slower to execute than If Then Else

    Mark
    -------------------

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Mark - do you see a flaw in the For loop? If not, don't you think it's more efficient than using the Select Case for this problem?
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Question What the heck is the IIF statement?

    Well, what is it?
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  6. #6
    Guest
    Code:
    Result = IIf(Expression, TruePart, FalsePart)
    Returns one of two parts, depending on the evaluation of anexpression.

    Syntax

    IIf(expr, truepart, falsepart)

    The IIf function syntax has thesenamed arguments:

    Part Description
    expr Required. Expression you want to evaluate.
    truepart Required. Value or expression returned if expr is True.
    falsepart Required. Value or expression returned if expr is False.


    Remarks

    IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.

    Code:
    Function CheckIt (TestMe As Integer)
       CheckIt = IIf(TestMe > 1000, "Large", "Small")
    End Function


  7. #7
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Thumbs up

    Interesting...I give it a thumbs up!
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    BruceG

    The loop is fine!

    I think you posted your reply while I was composing mine.


    To be honest I didn't actually read the code within the IFs, I just saw that a Select would be better than lots of IFs and changed that bit and then did search/replace to change to an array.
    Mark
    -------------------

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs up

    Thanks a lot everybody! I think I've got the hang of it now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width