2

I am new to ASP.NET C#. I try to follow this tutorial page to make a function using globally, but no luck. https://web.archive.org/web/20210612122420/http://aspnet.4guysfromrolla.com/articles/122403-1.aspx

What I try to do is use a function global any where in my code. I have a function called "FormatDateNoTime". I have create a Class file under App_Code folder. But when I call that function in one of my code behind page (example Page1.aspx.cs), it gives me the error:

Error: The name 'MyClass' does not exist in the current context

MyClass.cs file under the App_Code folder.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public class MyClass
{

        //
        // TODO: Add constructor logic here
        //

        public static string FormatDateNoTime(string input)            {
            string thedate;
            DateTime strDate = DateTime.Parse(input);
            thedate = strDate.ToString("MM/dd/yyyy");
            return thedate;
        }
}

The code in my Page1.aspx.cs calling the FormateNoTime function

TextBox BeginDate = (TextBox)FormView1.FindControl("BeginDate");
BeginDate.Text = MyClass.FormatDateNoTime(objDs.Tables[0].Rows[0]["BeginDate"].ToString());

It seems like other pages don't recognize this class.function().

Please help. Thanks in advance.

7
  • 1
    I see a couple of compile errors with the snippet you wrote above: One is, make sure that you defined input in the class MyClass as a static variable and Second is, while calling FormatDateNoTime method, you are passing just one argument. Have a look at them once Commented Oct 19, 2012 at 16:19
  • 1
    There's no way that code is ever compiled: wrong number of arguments when calling the function, and the function looks for an input variable that does not exist. This is cs101 stuff. Commented Oct 19, 2012 at 16:30
  • I am not sure I understand. Can you please help me correct my codes above if possible? Thanks for reply. Commented Oct 19, 2012 at 16:38
  • Why are you passing object and EventArgs parameters?? They are meant for event handlers.. Commented Oct 19, 2012 at 16:41
  • Sorry, I am new. I have updated the code to pass a string now. Stilll not able to recognize MyClass.FormatDateNoTime function from another page. Commented Oct 19, 2012 at 16:47

2 Answers 2

2

Right click the source file in App_Code and set its "Build Action" property to "Compile". Click on the .cs file in App_Code and hit F4 key (or right click -> Properties), and you should see an option for "Build Action" That way, it will build the code in your App_Code folder, and you should be able to access your static method in your class. If the above doesn't help, remove the class from the app_code and drop it in the root folder and try compiling it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for replay. I right click the MyClass.cs file and hit F4, but don't see any "Build Action", I only see two FileName and Full Page. any suggestion?
Right click on the MyClass.cs file and go to properties. It should look like this: dimarzionist.files.wordpress.com/2008/07/build-action.jpg
Thanks @tranceporter, I think this is a right track, but some how I could not figure out to the cs file in App_Code from "Build Action" to "Compile"
1

The code you have doesn't compile. I'm pretty sure Visual Studio is throwing an error when you try to run it; pay attention to what it's saying.

Also, change the following:

public static string FormatDateNoTime(object sender, EventArgs e)
{
    string thedate;
    DateTime strDate = DateTime.Parse(input);
    thedate = strDate.ToString("MM/dd/yyyy");
    return thedate;
}

To

public static string FormatDateNoTime(String input)
{
    string thedate;
    DateTime strDate = DateTime.Parse(input);
    thedate = strDate.ToString("MM/dd/yyyy");
    return thedate;
}

I'd go further and validate that the input is parseable into a DateTime, but that's for you to explore.

1 Comment

Thanks for reply. I changed the code as you suggested, but still getting error the "Error: The name 'MyClass' does not exist in the current context". So I need to Compile this class? How? Sorry, I am new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.