-1

I was trying to make an function using vb. This function is to collect data for a report. Follows the example:

Public Function HeaderData() As String
Dim dados As String
dados = ""  
If Not(frm.FormQuery.FieldByName("A").AsString  = "S" And _
     frm.FormQuery.FieldByName("B").AsString  = "S" And _
     frm.FormQuery.FieldByName("C").AsString  = "S" And _
     frm.FormQuery.FieldByName("D").AsString  = "S") Then

dados = dados + "Type: "

If frm.FormQuery.FieldByName(A).AsString = "S" Then
  dados = dados + "A"
End If

If frm.FormQuery.FieldByName(B).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Then
    dados = dados + ", "
  End If
  dados = dados + "B"
End If

If frm.FormQuery.FieldByName(C).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Or _
     frm.FormQuery.FieldByName(B).AsString = "S" Then
     dados = dados + ", "
  End If
  dados = dados + "C"
End If

 If frm.FormQuery.FieldByName(D).AsString = "S" Then
  If frm.FormQuery.FieldByName(A).AsString = "S" Or _
     frm.FormQuery.FieldByName(B).AsString = "S" Or _
     frm.FormQuery.FieldByName(C).AsString = "S" Then
     dados = dados + ", "
  End If
  dados = dados + "D"
End If

dados =  dados + "; "

And this function causes the error: Expecting an existing scalar var.

I did other functions with the same purpose, with similar code, using the same variable to collect the data (dados) and did not cause the error. What could be happening?

  • 2
    What line causes the error? – David Jun 19 '15 at 13:55
  • 2
    The code you posted is missing an End Function and a Return statement. Is it only a copy and paste error? – sblandin Jun 19 '15 at 13:55
  • 2
    **1.** Unclear what you're asking (where exactly is the exception thrown, and is that the exact exception message?), **2.** contains syntax errors (e.g. `FieldByName(D).`, i.e. missing double quotes), and **3.** non-reproducible error (what is `FormQuery`, what "fields" is it guaranteed to contain, what is the definition of `.FieldByName(…)` or `.AsString`; can they throw exceptions?) I'm voting to close (but will retract the close vote once these issues have been addressed). – stakx - no longer contributing Jun 19 '15 at 13:57
  • Just correcting: In the same function I did similar conditions to the above mentioned. – Augusto Ferbonink Jun 19 '15 at 14:03
  • I do not know which line is the exception, because the code editor that I'm using does not show it – Augusto Ferbonink Jun 19 '15 at 14:05
  • So just to be clear, the "error" is a compile-time error, not an exception thrown at run-time? The compiler will still indicate a rough location for the error; use a reasonable IDE or run the compiler from the command line and check its output. – stakx - no longer contributing Jun 19 '15 at 14:09
  • 1
    At the beginning you do FieldByName("A") than you do FieldByName(A). This is confusing and might be thesource of your problem. You should at least give us the line with the error. – the_lotus Jun 19 '15 at 14:10
  • Hey guys, is solved, was missing a double quotes... Feeling an jackass for not realizing it, I reviewed the code several times I did not realize it. Sorry and thanks – Augusto Ferbonink Jun 19 '15 at 14:12

1 Answers1

0

Your logic doesn't make much sense. I highly suggest that you write just a few lines and debug. Make sure everything works properly and than write a few more lines. Writing to much at the beginning will cause trouble.

    Dim possibleFields As String() = {"A", "B", "C", "D"}
    Dim foundFields As New List(Of String)
    Dim dados As String = ""

    For Each field As String In possibleFields
        If frm.FormQuery.FieldByName(field).AsString = "S" Then
            foundFields.Add(field)
        End If
    Next

    If foundFields.Count > 0 Then
        dados = String.Format("Type: {0}; ", String.Join(", ", foundFields.ToArray()))
    End If
the_lotus
  • 12,668
  • 3
  • 36
  • 53