SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Paris
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    New function in C#

    Hello
    How are the new functions in C# declared?

    I need to create something very simple:

    new function FuncName (String2, String2) {
    Response.Write(String1 + String2);
    }

    Thanks for all the replies

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    void FunctionName(string string1, string2)
    {
       Response.Write(String.Formt("{0} {1}", string1, string2);
    }
    I would recommend not using Response.Write(), have a look at ASP.NET's web controls, a Literal or Label control are better suited to deliver content on a page.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Paris
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dhtmlgod
    Code:
    void FunctionName(string string1, string2)
    {
       Response.Write(String.Formt("{0} {1}", string1, string2);
    }
    I would recommend not using Response.Write(), have a look at ASP.NET's web controls, a Literal or Label control are better suited to deliver content on a page.
    I have this while testint the function:

    Compiler Error Message: CS1547: Keyword 'void' cannot be used in this context

    Code:
    void FunctionName(string string1, string2)
    {
    	Response.Write("Hello");
    //   Response.Write(String.Formt("{0} {1}", string1, string2);
    }

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    public void FunctionName(string string1, string string2)
    {
       Response.Write(String.Formt("{0} {1}", string1, string2);
    }

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Paris
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my error:

    Compiler Error Message: CS1513: } expected
    Source Error:

    Line 26: for (int i=1; i<Qte; i++) {
    Line 27: Profondeur=Profondeur + "../";
    Line 28: }
    Line 29:
    Line 30:

    Here s the code

    Code:
    string Profondeur = "";
    
    for (int i=1; i<Qte; i++) {
    	Profondeur=Profondeur + "../";
    	}
    
    //Here must be the functions
    
    public void FunctionName(string string1, string string2)
    {
       Response.Write(String.Formt("{0} {1}", string1, string2);
    }
    
    //Here must finish the functions
    FunctionName("====","hhhhhhhhh");

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ok, looks like we need to start from square one.

    In C# every function (more properly method) must be a member of a class. Any code in a class must be in a method in general.

    You cannot have code and/or methods just hanging "loose" such as the above.

    I would recommend starting with this: http://www.csharp-station.com/Tutorial.aspx

  7. #7
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I dont get. If I'm coding in VB I can write a sub anywhere I want. but if I try to code a function in C# I get the same error as the user above. Do I have to use a code-behind technique to write functions?

    -=Sal=-

  8. #8
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using inline code you do not have to declare a class...

    Code c#:
    <&#37;@ Page Language="c#"%>
    <script runat="server">
     
        public void button_Clicked(Object Sender, EventArgs e) 
        {
            literal.Text = "You clicked me!";
        }
     
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript"></script>
        </head>
        <body>
     
            <form runat="server">
     
                <asp:Button id="button" runat="server" onclick="button_Clicked" text="Click me"></asp:Button>
                <asp:Literal id="literal" runat="server"></asp:Literal>
     
            </form>
     
        </body>
    </html>


  9. #9
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. That's exactly what I tried but I got:

    Compiler Error Message: CS1513: } expected

    I get this error with even the most basic functions, such as:
    Code:
    <% 
     
    public void test(){
    
    Response.Write("hello world");
    }
    
    test();
    }

    it also doesnt seem to matter where I call the function from. So long as there is a function or a class definition in my aspx page, I get Compiler Error Message: CS1513: } expected

    Any ideas?

    -=SAL=-

  10. #10
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    based on your example, no wonder. You have additional curly bracket and no closing &#37;> tag.

    You also have to define the language, as I have. To correctly test your code. Remove everything between my <script> tags and between the <form> tags on my example and put your own code.


  11. #11
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry I typed that quickly... the last } should have been %>

    -=Sal=-

  12. #12
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then it leaves your test();...

    You can't run code like this, you have to define a page_load event which will in turn call your test method...

    I assume you have come from a php background?


  13. #13
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP for web stuff yeah. Mostly Java and Actionscript though. The code I am replacing is writen in VB and defines and calls subs all over the place, so I didnt think there would be an issue writing functions.

    I dont want this to run from an event. I want to be able to drop in chuck code and call it repeatedly.


    -=Sal=-

  14. #14
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can only call methods by either an event or by assigning its output to a property or as a variable to another method. If you use

    Code asp.net:
    <%= test(); %>
    <!-- or -->
    <% Response.Write(test()); %>

    Inline with your html, it should work.


  15. #15
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgive me if I am being dense... but this is giving me the same error.

    Code:
    <&#37;
    public void test(){
    try
    	{
    	   conn1.Open();
    	   SqlCommand cmd1 = new SqlCommand("SELECT  Count(*) FROM x where xID=1 ",conn1);
    	   // get query results
    		rdr1 = cmd1.ExecuteReader();
    		rdr1.Read();
    		test.InnerHtml+=rdr1[0]+" "+Request.QueryString["game"];
    		
    	  }
    	finally
    	{
    		if (rdr1 != null)
    		{
    			rdr1.Close();
    		}
    		if (conn1 != null)
    		{
    			conn1.Close();
    		}
    	}
    }
    %>
    <%= test(); %>

  16. #16
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    If you want to run the class from anywhere, why not use the App_Code folder and add a normal, freestanding class?

  17. #17
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll look into that. The problem I have been having with any code not stored in my aspx page is that I get that annoying server issue where ASP does not recognize the machineKey when I try to post to the page. This is a known issue, I was just hoping to work aroundit. I'll try the app_code thanks.

    -=Sal=-


Bookmarks

Posting Permissions

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