locked
Custom Classes and IntelliSense RRS feed

  • Question

  • User794486199 posted

    I created a class file called ADHelper and placed it in the App_Code folder.  It has various methods like CurrentUserLogin(), GetManager(string userName), etc.

    When refering to these methods in an aspx page, is there a way to make intellisense show the method names without prefacing it with ADHelper?  For example:

    UserLabel.Text = CurrentUserLogin();

    vs

    UserLabel.Text = ADHelper.CurrentUserLogin();

     

    Monday, July 7, 2008 4:00 PM

Answers

  • User-802213120 posted

    As the error message alludes to, you can only reference namespaces with the "using" directive.  As I suggested perviously, the best you can do is use a wrapper method in your base Page:

    public abstract class BasePage : Page
    {
           protected string CurrentUserLogin()
           {
                  return ADHelper.CurrentUserLogin();
           }
    }

    public class MyPage : BasePage
    {
            protected void Page_Load(object sender, EventArgs e)
            {
                  UserLabel.Text = CurrentUserLogin();
            }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:21 PM
  • User-789181464 posted

     Well.. the reason is that it is looking for a namespace and ADHelper is a class, and you can't use a class next to using. I understand that this is not what you're looking for but this is how it is:

     cs class (on the App_Code folder)

     

    namespace ADHelper_Namespace
    {
        public class ADHelper
        {
            //
            // TODO: Add constructor logic here
            //
            public string CurrentUserLogin()
            {
                return "it is me";
            }
        }
    }
    asp.cs (code behind) 
     
    using ADHelper_Namespace; //ADHelper_Namespace is a namespace i made on the cs class
    
    ....
    
    ADHelper aADHelper = new ADHelper();
    TextBox3.Text = aADHelper.CurrentUserLogin();
     I've never tried using Bravo's method but you can certainly give it a shot. It looks pretty good to me.  
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:58 PM

All replies

  • User595841651 posted

    1) make  sure they are Shared methods.

    2) add an Imports/Using statement to your code, at the top....

    Monday, July 7, 2008 4:02 PM
  • User-802213120 posted

    No, not really.

    Things you could try to get close:

    1) Make a base page that dirives form Page.  Have this class wrap calls to your ADHelper class.  This would allow derived classes to invoke methods in the manner you spoke of.

    2) Use the "With" construct in VB.NET

    Monday, July 7, 2008 4:04 PM
  • User-802213120 posted

    1) make  sure they are Shared methods.

    2) add an Imports/Using statement to your code, at the top....



    Well, you still have to resolve the method to the class that it belongs to.
    Monday, July 7, 2008 4:05 PM
  • User595841651 posted

    1) make  sure they are Shared methods.

    2) add an Imports/Using statement to your code, at the top....



    Well, you still have to resolve the method to the class that it belongs to.

    If you mean you still have to have the Class Name specified... well yes.. you always will have this SOMEWHERE. Even the embeded MS ones are specified, just not always at a layer you can access them.

    Embedding it in a Base class (as you mentioned) will eliminate this on the Page, but it's still referenced by name in the base....
    It's the nature of the system.

    Monday, July 7, 2008 4:15 PM
  • User-802213120 posted

    1) Your response seemed to indicate that it was possible. He had already mentioned that he was using a static class, so I wasn't sure where you were going with it in your response.

    2) Since he said he wanted to do it on .aspx pages, my suggestion for a work-around was for just that situation.

     

     

    Monday, July 7, 2008 4:26 PM
  • User-789181464 posted

    1) Your response seemed to indicate that it was possible. He had already mentioned that he was using a static class, so I wasn't sure where you were going with it in your response.

    2) Since he said he wanted to do it on .aspx pages, my suggestion for a work-around was for just that situation.

     

     

     a 280-point member is trying to take on a thirty-one-thousand-five-hundren-and-twenty-points All-Star? lol... i am not sure if this forum is for this type of discussion... Anyways, i totally agree with the All-star. You have got to refer to the class somewhere in order to use its methods and variables. This is the 'nature' of any programming language.

    Monday, July 7, 2008 4:32 PM
  • User-802213120 posted

    Oh jeez... nobody's "taking on" (whatever that means) anybody.  The original post asked how to call a helper method directly from within an .aspx page, without the need to reference the parent (static) class.

    photoshoper - BTW, I wouldn't call it the nature of any programming language.  For example, most functional programming languages allow for functions as first class citizens.  This would, of course, mean that they do not require a parent class.

    Monday, July 7, 2008 4:41 PM
  • User-789181464 posted

     

    photoshoper - BTW, I wouldn't call it the nature of any programming language.  For example, most functional programming languages allow for functions as first class citizens.  This would, of course, mean that they do not require a parent class.

     Please give me one example of such programming language that you don't have to refer to the class in order to call its methods and variables.

    Monday, July 7, 2008 4:48 PM
  • User-802213120 posted

    F#, Haskell, OCaml.... Heck, even JavaScript.  Lots and lots more. :)


    BTW - I never said that I was calling a class method without referencing the class.
    Monday, July 7, 2008 4:54 PM
  • User794486199 posted

    So 

    1) make  sure they are Shared methods.

     would have in the ADHelper class:

    public static string CurrentUserLogin()

    and 

    2) add an Imports/Using statement to your code, at the top....

    would have at the top of the asp.cs code behind file:

    using ADHelper;

    Is that all?  Running the page says "A using namespace directive can only be applied to namespaces; 'ADHelper' is a type not a namespace" on the line "using ADHelper;"  Google is not my friend today... not finding good examples of how to make this work... 

     

    Monday, July 7, 2008 5:07 PM
  • User-802213120 posted

    As the error message alludes to, you can only reference namespaces with the "using" directive.  As I suggested perviously, the best you can do is use a wrapper method in your base Page:

    public abstract class BasePage : Page
    {
           protected string CurrentUserLogin()
           {
                  return ADHelper.CurrentUserLogin();
           }
    }

    public class MyPage : BasePage
    {
            protected void Page_Load(object sender, EventArgs e)
            {
                  UserLabel.Text = CurrentUserLogin();
            }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:21 PM
  • User-789181464 posted

     Well.. the reason is that it is looking for a namespace and ADHelper is a class, and you can't use a class next to using. I understand that this is not what you're looking for but this is how it is:

     cs class (on the App_Code folder)

     

    namespace ADHelper_Namespace
    {
        public class ADHelper
        {
            //
            // TODO: Add constructor logic here
            //
            public string CurrentUserLogin()
            {
                return "it is me";
            }
        }
    }
    asp.cs (code behind) 
     
    using ADHelper_Namespace; //ADHelper_Namespace is a namespace i made on the cs class
    
    ....
    
    ADHelper aADHelper = new ADHelper();
    TextBox3.Text = aADHelper.CurrentUserLogin();
     I've never tried using Bravo's method but you can certainly give it a shot. It looks pretty good to me.  
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 6:58 PM