Wrox Programmer Forums
|
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2008, 08:14 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default CANNOT APPLY INDEXING WITH []

Hi,

I have used the PHP assistant to try to convert php into aspx (C#) but I am getting this error when I run the code in Visual Studio 2005

'cannot apply indexing with [] to an expression of type object'

These are the lines that cause the problem:

foreach (string name in form_variables.Keys)
{
  ...
  object properties = form_variables[name];
  entry += " 'regexp': " + properties["regexp"] + ",\n";
  entry += " 'error': '" + PHP.StringSupport.AddSlashes(properties["error"]) + "'\n";

....
}

Could someone please shed some light about what I have to do? I have tried to cast the form_variables[name]; to a string object but to no avail - string(form_variables[name]);


Cheers

C

This is the full C# function:

public virtual string build_javascript(PHP.OrderedMap form_variables)
    {
        string js;
        PHP.OrderedMap entries = new PHP.OrderedMap();
        string entry;
        js = "var validationSet = {\n";
        entries = new PHP.OrderedMap();
        foreach (string name in form_variables.Keys)
        {
            object properties = form_variables[name];
            Response.Write(properties.GetType().ToString());
            entry = " '" + name + "': {\n";
            entry += " 'regexp': " + properties["regexp"] + ",\n";
            entry += " 'error': '" + PHP.StringSupport.AddSlashes(properties["error"]) + "'\n";
           entry += " }";
            entries[null] = entry;
        }

        js += PHP.StringSupport.Join(",\n", entries) + "\n";
        js += "}\n";
        return js;
    }

This is the php function:

function build_javascript($form_variables) {
    $js = "var validationSet = {\n";
    $entries = array();
    foreach ($form_variables as $name => $properties) {
      $entry = " '$name': {\n";
      $entry .= " 'regexp': {$properties['regexp']},\n";
      $entry .= " 'error': '".addslashes($properties['error'])."'\n";
      $entry .= " }";
      $entries[] = $entry;
    }
    $js .= join(",\n", $entries) . "\n";
    $js .= "}\n";
    return $js;
  }
 
Old April 7th, 2008, 08:24 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

PHP is a weakly typed language, so the line $properties['regexp'] is fine, as it doesn't need to know that $properties is an array. However C# wont let you do that on something that is declared as object as it doesn't know if it is an array.

What it Response.Write(properties.GetType().ToString()) outputting?

You need to do something like this:

PHP.OrderedMap properties = (PHP.OrderedMap)form_variables[name];


/- Sam Judson : Wrox Technical Editor -/
 
Old April 7th, 2008, 09:08 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi samjudson,

Many thanks for your help.

>What it Response.Write(properties.GetType().ToString()) outputting?

I was just trying to output the properties type to see what type it was but it is not part of the code. I just forgot to remove it.

I will try your suggestion right now.

Thanks

C
 
Old April 7th, 2008, 09:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

But what it output might be useful :)

If it outputs anything other than "PHP.OrderedMap" then you will need to change my code above.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 7th, 2008, 02:43 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Sam,

I am sorry to take so long to reply but I had to take my wife to hospital for an emergency.

I tried your casting and it worked like a charm. The only problem I had was with the second statement but adding a ToString() resolved it. Please see below:

entry += " 'error': '" + PHP.StringSupport.AddSlashes(properties["error"].ToString()) + "'\n";

This is the output:

var validationSet = {
  'email': {
    'regexp': /^.+?@.+?\..+$/,
    'error': 'This email address is invalid. It should be of the form someone@example.com.'
  },
  'phone': {
    'regexp': /^[- ()0-9]+$/,
    'error': 'A phone number must be digits only.'
  },
  'country': {
    'regexp': /^[a-zA-Z][a-zA-Z]$/,
    'error': 'Country codes are two letters only. Examples are US, UK or FR'
  }
}

Again I really appreciate your help.

Cheers

C
 
Old April 7th, 2008, 06:23 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Sam,

I am also having problems with the Response.Write() method and would appreciate very much if you could shed some light. Here is the error messege I get when I run the code below to call the build_form() method.

'cannot convert from void to string'

'the best overload method match for System.Web.HttpResponse.Write(string) has some invalid arguments'

Response.Write(build_form(form_variables, errors, new PHP.OrderedMap(Request.Form, false)));

Response.Write(build_form(form_variables, new PHP.OrderedMap(), new PHP.OrderedMap()));

This is an extract of the the build_form method. Please find the full method below:

public virtual void build_form(PHP.OrderedMap form_variables, PHP.OrderedMap errors, PHP.OrderedMap data)
{
......
}

FULL C# VERSION

public virtual void build_form(PHP.OrderedMap form_variables, PHP.OrderedMap errors, PHP.OrderedMap data)
    {
        // Ensure $errors and $data have empty strings for incorrect fields
        object javascript;
        foreach (object name in form_variables.GetKeysOrderedMap(null).Values)
        {

            data[name] = (data[name] != null) ? System.Web.HttpUtility.HtmlEncode(data[name].ToString()) : "";
            if (!(errors[name] != null))
            {
                errors[name] = "";
            }
            else if (PHP.TypeSupport.ToBoolean(errors[name]))
            {
                data[name] = "";// Don't redisplay invalid data
            }
        }


        javascript = build_javascript(form_variables);

                Response.Write(" <script type=\"text/javascript\">\r\n" +
        " " + javascript + "\r\n" +
        " </script>\r\n" +
        " <script type=\"text/javascript\" src=\"genericValidation.js\"></script>\r\n" +
        " <form action=\"\" method=\"post\">\r\n" +
        " <p>\r\n" +
        " <label for=\"email\">Email address</label>\r\n" +
        " <input type=\"text\" name=\"email\" id=\"email\"\r\n" +
        " value=\"" + data["email"] + "\">\r\n" +
        " <span id=\"error_email\" class=\"errormessage\"\r\n" +
        " >" + errors["email"] + "</span>\r\n" +
        " </p>\r\n" +
        " <p>\r\n" +
        " <label for=\"phone\">Phone number</label>\r\n" +
        " <input type=\"text\" name=\"phone\" id=\"phone\"\r\n" +
        " value=\"" + data["phone"] + "\">\r\n" +
        " <span id=\"error_phone\" class=\"errormessage\"\r\n" +
        " >" + errors["phone"] + "</span>\r\n" +
        " </p>\r\n" +
        " <p>\r\n" +
        " <label for=\"country\">Country code</label>\r\n" +
        " <input type=\"text\" name=\"country\" id=\"country\"\r\n" +
        " size=\"2\" maxlength=\"2\" value=\"" + data["country"] + "\">\r\n" +
        " <span id=\"error_country\" class=\"errormessage\"\r\n" +
        " >" + errors["country"] + "</span>\r\n" +
        " </p>\r\n" +
        " <p><input type=\"submit\" value=\"submit\"></p>\r\n" +
        " </form>");


    }


EXTRACT PHP VERSION

function build_form($form_variables, $errors=array(), $data=array()) {
    // Ensure $errors and $data have empty strings for incorrect fields
    foreach (array_keys($form_variables) as $name) {
      $data[$name] = isset($data[$name]) ? htmlspecialchars($data[$name]) : '';
      if (!isset($errors[$name])) {
        $errors[$name] = '';
      } elseif ($errors[$name]) {
        $data[$name] = ''; // Don't redisplay invalid data
      }
    }

    $javascript = build_javascript($form_variables);

    echo <<<EOD
      <script type="text/javascript">
        $javascript
      </script>
      <script type="text/javascript" src="genericValidation.js"></script>
........
........

Cheers

C
 
Old April 8th, 2008, 02:53 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Just get rid of the Response.Write's completely. The build_form method doesn't return anything (it is set to return 'void') therefore they have nothing to print out.

Replace:
Code:
Response.Write(build_form(form_variables, errors, new PHP.OrderedMap(Request.Form, false)));
With:
Code:
build_form(form_variables, errors, new PHP.OrderedMap(Request.Form, false));
/- Sam Judson : Wrox Technical Editor -/
 
Old April 8th, 2008, 06:40 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Thanks Sam. The buil_form is working now.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Indexing ~Bean~ ASP.NET 1.x and 2.0 Application Design 2 August 16th, 2005 06:28 AM
indexing within grouping jkmyoung XSLT 3 November 15th, 2004 04:08 PM
indexing vohra_vikas SQL Server 2000 2 September 28th, 2004 11:49 PM
Indexing. kilika SQL Server 2000 3 May 24th, 2004 03:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.