Simple Error that I cannot solve

Dear all,

I am developing an app that medical students can use for reviewing simulated patient’s lab test results. I have a menu with three cases: SVT, CHF and PE.

For each case, students check checkboxes with the tests they want to see, click BtnOrder pushbutton, and the text area is populated with test results. If a parameter’s value is out of normal range, it is presented in red font. All seems to be simple and straightforward. When I try to compile the code below I am getting the following errors

Window1.BtnOrder.Pressed, line 214 - Syntax error End Sub
Window1.Name - Syntax error Window 1

Removing Sub BtnOrder_Pressed() and End Sub produces dozens of error.

I’ve checked the names of UI elements (checkboxes), buttons, text field - all are properly named.

Can someone tell me what I am doing wrong?

Sub BtnOrder_Pressed()
  // Assume NormalRanges is a global Dictionary loaded at app startup
  // It maps test names to Dictionaries with keys: "Low", "High", "Unit"
  // Example: NormalRanges.Value("Na") = Dictionary with Low=135.0, High=145.0, Unit="mmol/L"
  
  Var labs As New Dictionary
  
  // Select labs based on CurrentCase (global variable)
  Select Case CurrentCase
  Case "P1 CV - SVT"
    labs.Value("Na") = 140
    labs.Value("K") = 4.2
    labs.Value("Cl") = 102
    labs.Value("CO2") = 24
    labs.Value("BUN") = 15
    labs.Value("Creatinine") = 1.0
    labs.Value("Glucose") = 90
    labs.Value("Ca") = 9.5
    labs.Value("Mg") = 2.0
    
    labs.Value("WBC") = 8.0
    labs.Value("Hgb") = 14.0
    labs.Value("Hct") = 42.0
    labs.Value("Platelets") = 250
    labs.Value("Neutrophils") = 58.0
    labs.Value("Lymphocytes") = 32.0
    labs.Value("Monocytes") = 6.0
    labs.Value("Eosinophils") = 2.0
    labs.Value("Basophils") = 0.5
    
    labs.Value("Troponin") = 0.02
    labs.Value("CKMB") = 2.0
    labs.Value("BNP") = 50
    
    labs.Value("pH") = 7.40
    labs.Value("pCO2") = 40
    labs.Value("pO2") = 95
    labs.Value("HCO3") = 24
    labs.Value("SaO2") = 98
    
    labs.Value("D-Dimer") = 0.2
    labs.Value("PT") = 12.5
    labs.Value("INR") = 1.0
    labs.Value("aPTT") = 30
    
    labs.Value("ALT") = 30
    labs.Value("AST") = 25
    labs.Value("ALP") = 90
    labs.Value("BilirubinTotal") = 0.8
    labs.Value("Albumin") = 4.0
    
    labs.Value("ThyroidStimHormone") = 2.0
    labs.Value("HIV") = 0  // Negative
    labs.Value("Pregnancy") = 0  // Negative
    labs.Value("Urinalysis") = 0
    labs.Value("BloodCultures") = 0
    labs.Value("UrineCultures") = 0
    labs.Value("UrineTox") = 0
    labs.Value("HepatitisPanel") = 0
    labs.Value("LacticAcid") = 1.0
    
  Case "P1 RUS - CHF_PE"
    labs.Value("Na") = 131
    labs.Value("K") = 5.4
    labs.Value("Cl") = 96
    labs.Value("CO2") = 20
    labs.Value("BUN") = 28
    labs.Value("Creatinine") = 1.6
    labs.Value("Glucose") = 110
    labs.Value("Ca") = 8.3
    labs.Value("Mg") = 1.8
    
    labs.Value("WBC") = 9.0
    labs.Value("Hgb") = 13.5
    labs.Value("Hct") = 40
    labs.Value("Platelets") = 240
    labs.Value("Neutrophils") = 65.0
    labs.Value("Lymphocytes") = 25.0
    labs.Value("Monocytes") = 7.0
    labs.Value("Eosinophils") = 2.5
    labs.Value("Basophils") = 0.5
    
    labs.Value("Troponin") = 0.08
    labs.Value("CKMB") = 6.0
    labs.Value("BNP") = 1200
    
    labs.Value("pH") = 7.28
    labs.Value("pCO2") = 52
    labs.Value("pO2") = 60
    labs.Value("HCO3") = 20
    labs.Value("SaO2") = 88
    
    labs.Value("D-Dimer") = 0.3
    labs.Value("PT") = 13.0
    labs.Value("INR") = 1.1
    labs.Value("aPTT") = 31
    
    labs.Value("ALT") = 55
    labs.Value("AST") = 40
    labs.Value("ALP") = 140
    labs.Value("BilirubinTotal") = 1.1
    labs.Value("Albumin") = 3.5
    
    labs.Value("ThyroidStimHormone") = 2.5
    labs.Value("HIV") = 0  // Negative
    labs.Value("Pregnancy") = 0  // Negative
    labs.Value("Urinalysis") = 0
    labs.Value("BloodCultures") = 0
    labs.Value("UrineCultures") = 0
    labs.Value("UrineTox") = 0
    labs.Value("HepatitisPanel") = 0
    labs.Value("LacticAcid") = 2.0
    
  Case "P1 PULM - PE"
    labs.Value("Na") = 138
    labs.Value("K") = 4.0
    labs.Value("Cl") = 102
    labs.Value("CO2") = 22
    labs.Value("BUN") = 18
    labs.Value("Creatinine") = 1.0
    labs.Value("Glucose") = 95
    labs.Value("Ca") = 9.0
    labs.Value("Mg") = 2.1
    
    labs.Value("WBC") = 10.5
    labs.Value("Hgb") = 14.2
    labs.Value("Hct") = 43
    labs.Value("Platelets") = 260
    labs.Value("Neutrophils") = 62.0
    labs.Value("Lymphocytes") = 30.0
    labs.Value("Monocytes") = 5.0
    labs.Value("Eosinophils") = 2.0
    labs.Value("Basophils") = 1.0
    
    labs.Value("Troponin") = 0.05
    labs.Value("CKMB") = 3.0
    labs.Value("BNP") = 150
    
    labs.Value("pH") = 7.48
    labs.Value("pCO2") = 30
    labs.Value("pO2") = 68
    labs.Value("HCO3") = 22
    labs.Value("SaO2") = 92
    
    labs.Value("D-Dimer") = 1.2
    labs.Value("PT") = 12.0
    labs.Value("INR") = 1.0
    labs.Value("aPTT") = 29
    
    labs.Value("ALT") = 28
    labs.Value("AST") = 22
    labs.Value("ALP") = 100
    labs.Value("BilirubinTotal") = 0.9
    labs.Value("Albumin") = 4.2
    
    labs.Value("ThyroidStimHormone") = 1.8
    labs.Value("HIV") = 0  // Negative
    labs.Value("Pregnancy") = 0  // Negative
    labs.Value("Urinalysis") = 0
    labs.Value("BloodCultures") = 0
    labs.Value("UrineCultures") = 0
    labs.Value("UrineTox") = 0
    labs.Value("HepatitisPanel") = 0
    labs.Value("LacticAcid") = 1.1
    
  Case Else
    MessageBox("Unknown clinical case selected.")
    Return
  End Select
  
  // Clear previous results
  TextArea1.StyledText.Text = ""
  
  // Display formatted results with color coding
  For Each key As String In labs.Keys
    Var val As Variant = labs.Value(key)
    Var norm As Dictionary = NormalRanges.Value(key)
    
    If norm = Nil Or (norm.Value("Low") = 0 And norm.Value("High") = 0) Then
      // Non-numeric flags like HIV, Pregnancy etc.
      Var displayText As String
      Select Case key
      Case "HIV", "Pregnancy", "BloodCultures", "UrineCultures", "UrineTox", "HepatitisPanel"
        displayText = If(val = 0, "Negative", "Positive")
      Case "Urinalysis"
        displayText = If(val = 0, "Normal", "Abnormal")
      Else
        displayText = val.StringValue
      End Select
      
      Var line As String = key + " = " + displayText + EndOfLine + EndOfLine
      TextArea1.StyledText.AppendText(line)
      TextArea1.StyledText.TextColor(TextArea1.StyledText.Length - line.Length, line.Length) = &c000000 // black
      
      Continue For
    End If
    
    // Numeric lab test with ranges
    Var low As Double = norm.Value("Low")
    Var high As Double = norm.Value("High")
    Var unit As String = norm.Value("Unit")
    
    Var line As String = key + " = " + val.StringValue + " " + unit + EndOfLine + _
    "Normal range: " + low.ToString + " - " + high.ToString + " " + unit + EndOfLine + EndOfLine
    TextArea1.StyledText.AppendText(line)
    
    If val < low Or val > high Then
      TextArea1.StyledText.TextColor(TextArea1.StyledText.Length - line.Length, line.Length) = &cFF0000 // red
    Else
      TextArea1.StyledText.TextColor(TextArea1.StyledText.Length - line.Length, line.Length) = &c000000 // black
    End If
  Next
End Sub

You can’t do this:

For Each key As String In labs.Keys

…since labs.Keys is an array of type Variant. That will cause all sorts of havoc in the compiler. You’ll have to cast the value thusly:

var currentKeyString as string
var labKeys() as variant

labKeys=labs.Keys

For Each key as Variant in labKeys
    currentKeyString=key
    //processing goes here
Next

I guess AI helped you with this code, right?

You can’t have Sub/End Sub in your code. You need to remove that and then tackle all the errors.

For example: .StyledText.AppendText, .StyledText.Length are not available in the Xojo version I have.

1 Like

Yes, AI helped. Removing Sub and End Sub and placing a simple code inside works. When I put AI-generated code without Sub and End Sub, it creates dozens of errors.

Because the AI you are using does not create good Xojo code and is ‘guessing’ what should work (but it doesn’t work).

You need to review the dozens of errors and fix them.

1 Like

[quote=“AlbertoD, post:3, topic:86247, username:AlbertoD”]
I used AI to add Case structure to my existing code. I have everything working well for the SVT scenario. I just wanted a quick fix for adding CHF and Pulmonary Embolism scenarios. I guess, I have to do it manually.

Do you get errors on the Case structure?
What version of Xojo are you using?
Does

TextArea1.StyledText.AppendText(line)
TextArea1.StyledText.Length

work on your Xojo version? Here you can find the current StyledText Methods:

Not true, I do it all the time without issue. The compiler does the casting automagically.

2 Likes

Really? I even tried that in a test project before posting. Maybe I did something wrong.

1 Like

Do you know what a Method is in Xojo and how to use it ?

You can add one using Insert → Method.
You can select (one click to hilight it) a Window or a Module in the Navigator pane and paste the Method definition in the Cmipboard (Sub …/… End Sub).

HTH

1 Like

I’m using 2021r2.1, maybe it’s changed since.