C#->VB Snippet Translation Help Needed

P

Phil C.

Hi.
I'm having trouble translating a routine to alter validation at the client
and the server.

A portion of the C# code is:namespace Test.WebControls
{

public class WebPage : System.Web.UI.Page
{
public WebPage()
{
validatorAssignments = new NameValueCollection();
}

private void Page_Load( object sender, System.EventArgs e )
{
if( this.validatorAssignments != null &&
this.validatorAssignments.HasKeys() )
{
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append(Environment.NewLine);
sb.Append("Page_ValidatorAssignments = true;");
sb.Append(Environment.NewLine);
for( int i = 0; i < this.validatorAssignments.Count; i++ )
{
if( this.validatorAssignments.Keys.ToString() != string.Empty &&
this.validatorAssignments.ToString() != string.Empty )
{
sb.AppendFormat(" var {0}Validators = new Array(",
this.validatorAssignments.Keys.ToString());
string[] keys = this.validatorAssignments.Split(",".ToCharArray());
for( int k = 0; k < keys.Length; k++ )
{
sb.AppendFormat("'document.all[\"{0}\"]'", keys[k].Trim()); ///This
Line has Problems

if( k != keys.Length - 1 )
sb.Append(",");
}
sb.Append(");");
sb.Append(Environment.NewLine);

}
}
sb.Append(Environment.NewLine);
sb.Append("</script>");
Page.RegisterClientScriptBlock("ValidatorAssignments", sb.ToString());
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

The problem is with the line:
sb.AppendFormat("'document.all[\"{0}\"]'", keys[k].Trim());

The error reads "Overload resolution failed because no accessible
AppendFormat can be called with these arguments....."

Any help would be appreciated

My VB translation up to that line reads:

If (Me.validatorAssignments Is Nothing) AndAlso
(Me.validatorAssignments.HasKeys() = True) Then

Dim sB As New StringBuilder

sB.Append("<script>")

sB.Append(Environment.NewLine)

sB.Append("Page_ValidatorAssignments = true;")

sB.Append(Environment.NewLine)

Dim i, j As Integer

For i = 0 To Me.validatorAssignments.Count - 1

If Me.validatorAssignments.Keys(i).ToString <> "" AndAlso
Me.validatorAssignments(i).ToString <> "" Then

sB.AppendFormat(" var {0}Validators = new Array(",
Me.validatorAssignments.Keys(i).ToString())

Dim keys As String() = Me.validatorAssignments(i).Split(",".ToCharArray())

For j = 0 To keys.Length - 1

sb.AppendFormat("'document.all[\"{0}\"]'", keys(j).Trim())

Next

End If

Next

End If





<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
C

Cor Ligthert

Phil,


\" (single quote) is in VBNet "" (double quote)

Therefore
"""This""" is "This"

I hope this helps,

Cor
 
C

charlestek

Thanks for saving my skin Cor,
I should remember that backslash escapes are strictly C language type
constructs.
I learned C from K&R and used it more in the past than VB,
so when I translate C# to VB I frequently think things look ok. I
remember now constructing sql queries in regular asp vbscript
and having to concatenate strings and escape the single quotes too.

I learned .Net in VB, so I started this project with it.
In the future I'm going to do everything in C# so I don't
get a headache ;-)
 
Top