Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Am tried to translate my VB code to C# and facing an error.

Below is my VB code....

VB
While rs.Read()
            this_key = keys(CStr(rs.Item("BillingCode")))

            If x(this_key) Mod 2 = 0 Then
                rows(this_key).Append("<tr>")
            Else
                rows(this_key).Append("<tr class=list_row_alt>")
            End If

            If rs.Item("amount") is DBNull.Value Then
                this_amount = 0
            Elseif this_key = "check" or this_key = "card" Then
                this_amount = -1 * rs.Item("amount")
            Else
                this_amount = rs.Item("amount")
            End If


            rows(this_key).Append("<td class=list_cell>" & rs.Item("date") & "</td>")
            rows(this_key).Append("<td class=list_cell><a href=""../../classroom_program/billing.aspx?ClassroomProgramID=" & rs.Item("ClassroomProgramID") & """>" & rs.Item("program_title") & "</a></td>")
            If rs.Item("SchoolID") <> 0 Then
                rows(this_key).Append("<td class=list_cell><a href=""../../school/default.aspx?SchoolID=" & rs.Item("SchoolID") & """>" & rs.Item("school_name") & "</a></td>")
            Else
                rows(this_key).Append("<td class=list_cell>&nbsp;</td>")
            End If
            If rs.Item("ClassroomStudentID") <> 0 Then
                rows(this_key).Append("<td class=list_cell><a href=""../../classroom_student/billing.aspx?ClassroomStudentID=" & rs.Item("ClassroomStudentID") & """>" & rs.Item("student_first_name") & " " & rs.Item("student_last_name") & "</a></td>")
            Else
                rows(this_key).Append("<td class=list_cell>&nbsp;</td>")
            End If
            rows(this_key).Append("<td class=list_cell>" & rs.Item("billing_method") & "</td>")
            If this_key = "check" or this_key = "bounced" Then
                rows(this_key).Append("<td class=list_cell align=center>" & rs.Item("check_no") & "</td>")
            End If
            rows(this_key).Append("<td class=list_cell>" & rs.Item("Note") & "</td>")
            rows(this_key).Append("<td class=list_cell align=right>" & Format(this_amount, "$0.00;($0.00)") & "</td>")
            rows(this_key).Append("</tr>")

            x(this_key) += 1
            total(this_key) += this_amount

        End While


below is the C# code.....

XML
while (rs.Read())
       {
           this_key = Keys(Convert.ToString(rs["BillingCode"]));
         //  this_key = keys(Convert.ToString(rs["BillingCode"].ToString()));

           if (Convert.ToInt32(x[this_key]) % 2 == 0)
           {
               rows[this_key].Append("<tr>");
              // rows[this_key].Append("<tr>");
           }
           else
           {
               rows[this_key].Append("<tr class=list_row_alt>");
           }

           if (object.ReferenceEquals(rs["amount"], DBNull.Value))
           {
               this_amount = 0;
           }
           else if (this_key == "check" | this_key == "card")
           {
               this_amount = -1 *(Convert.ToInt32( rs["amount"]));
           }
           else
           {
               this_amount =Convert.ToDecimal(rs["amount"]);
           }
           rows[this_key].Append("<td class=list_cell>" + rs["date"] + "</td>");
           rows[this_key].Append("<td class=list_cell><a href=\"../../classroom_program/billing.aspx?ClassroomProgramID=" + rs["ClassroomProgramID"] + "\">" + rs["program_title"] + "</a></td>");
           if (Convert.ToInt32(rs["SchoolID"]) != 0)
           {
               rows[this_key].Append("<td class=list_cell><a href=\"../../school/default.aspx?SchoolID=" + rs["SchoolID"] + "\">" + rs["school_name"] + "</a></td>");
           }
           else
           {
               rows[this_key].Append("<td class=list_cell>&nbsp;</td>");
           }
           if (Convert.ToInt32(rs["ClassroomStudentID"]) != 0)
           {
               rows[this_key].Append("<td class=list_cell><a href=\"../../classroom_student/billing.aspx?ClassroomStudentID=" + rs["ClassroomStudentID"] + "\">" + rs["student_first_name"] + " " + rs["student_last_name"] + "</a></td>");
           }
           else
           {
               rows[this_key].Append("<td class=list_cell>&nbsp;</td>");
           }
           rows[this_key].Append("<td class=list_cell>" + rs["billing_method"] + "</td>");
           if (this_key == "check" | this_key == "bounced")
           {
               rows[this_key].Append("<td class=list_cell align=center>" + rs["check_no"] + "</td>");
           }
           rows[this_key].Append("<td class=list_cell>" + rs["Note"] + "</td>");
           rows[this_key].Append("<td class=list_cell align=right>" + String.Format((this_amount).ToString(), "$0.00;($0.00)") + "</td>");
           rows(this_key).Append("</tr>");

           x(this_key) += 1;
           total(this_key)+= this_amount;

       }


here is the error:
rows[this_key].Append("");
Error: 'object' does not contain a definition for 'append'
Posted
Comments
F. Xaver 11-Jun-15 6:19am    
Neither String or Object own a 'Append' method.
Looks like rows[something] is returning a different object, like a Stringbuilder.

OP should specify 'rows' definition

1 solution

C# can't assume the object type of row[this_key].

I think VB .Append will cast the object as a string. C#'s extension model can't do that and doesn't have the same "Append" method anyway.

Instead, try using "+=":
C#
rows[this_key] += "<\td>";


This is shorthand for:
C#
rows[this_key] = rows[this_key] + "<\td>";


The right hand side should be able to either cast or call ToString() as it is effectively concatenating two strings.

Failing that, just use the second version adding the ToString() yourself:


C#
rows[this_key] = rows[this_key].ToString() + "<\td>";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900