CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104

    Question Defining a stand-alone method/function?

    I am learning Java (or more specifically, JSP) and am working on my final project for class. As part of the JSP page I am creating, I want to perform the same string concatenation on each field value to build the appropriate HTML. Normally (with other languages), I would define a function to do this. But I am having trouble getting the syntax right to make this work. Here is the code for my function declaration:
    Code:
    //Takes a string and builds an HTML value attribute out of it.
    String FieldToValue(String str)
    {
      String retStr = "";
      if (str.length() > 0) {
        retStr = "value='" + str + "' ";
      }
      return retStr;
    }
    And here is the error I am receiving:
    Code:
    <path removed>/_0002fidev_00031_00031_00031final_0002fcustdetail_0002ejspcustdetail_jsp_0.java:63: ')' expected.
                    String FieldToValue(String str)
                                              ^
    I've tried declaring the function with the public or private keywords, but then Tomcat complains that it wants a statement in their place.

    Can I even do this? Or do I need to actually create a .class file (something I really don't have time to play with right now) just to establish a function for use?
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  2. #2
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    You can have a method (function) on a JSP page, but it must be within the declarations tags
    Code:
    <%! // note the !
    
    String FieldToValue(String str)
    {
      String retStr = "";
      if (str.length() > 0) {
        retStr = "value='" + str + "' ";
      }
      return retStr;
    }
    
    %>

  3. #3
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    Excellent! Thanks Goodz
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured