1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development C# - classes

Discussion in 'Tech Support' started by Journeyer, 15 May 2012.

  1. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    Help!

    I'm currently studying development in ASP .NET and C#, and I'm due to send in a report.
    In this report I have to design a website for a fictional company, and include a bit of logic that has to run in helper classes. Designing the website, getting productlistings coded and such is not a problem, but my problem is e-mail validation.

    So, the task at hand is to code a bit of logic that will check if an e-mail address entered is entered according to the valid format (xxxxxx...@xxxxx.xx(x)). Coding this using regex is not the problem, however the problem is getting the site to invoke the helper class at all.

    The task specifies that I have to run the logic in a helper class, and not in the code-behind file. I have been banging my head against various hard surfaces to no avail, and google has not been able to help me either.

    This is the code in my helper class file:

    namespace newsSubscription
    {
    public class emailValidation
    {
    public void Button2_OnServerClick(object sender, EventArgs e)
    {
    string email = subscribeNews.Text;
    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
    Match match = regex.Match(email);
    if (match.Success)
    {
    emailValidation.InnerHtml = "Great Success!";
    }
    else
    {
    emailValidation.InnerHtml = "Did not compute!";
    }
    }
    }
    }

    But I can't, for the life of me, figure out how to invoke it through the code-behind file. I know calling "Button2_OnServerClick" should probably be in the code-behind file, but I've tried every variation I could figure with no luck, and I'm almost ashamed to admit that I am at a loss. Though I am probably missing something obvious, and thus will be equally ashamed when it is pointed out to me, but please, if there are any C# proficient bit-techers out there - please help a fellow bit-techer in need. I'll send cookies.
     
  2. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    No-one? Really?
    Aw ... oh well, back to banging my head against various surfaces then.
     
  3. Krikkit

    Krikkit All glory to the hypnotoad! Super Moderator

    Joined:
    21 Jan 2003
    Posts:
    23,514
    Likes Received:
    392
    Is this any help? There's quite a few results for similar problems... I don't fully understand it, but it looks like similar trouble.
     
  4. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    Hm, no, not really.
    Using namespaces in the code-behind file I can access the helper class, I just can't invoke the code in it. The code, however, does compile so I'm quite certain the code itself is correct. I'm just missing something - a method, or a function ... something.

    Thanks for the effort though. :)
     
  5. Highland3r

    Highland3r Minimodder

    Joined:
    25 Jul 2003
    Posts:
    7,558
    Likes Received:
    16
    Note, I'm by no means a C# "expert" - mostly a dabbler but hopefully this will help. This logic works for a windows form calling methods in another class - the principle should be the same.

    My external class is called "sql" (it basically contains a load of methods to execute SPs and return data).

    In the form along with variable declarations is the line:

    SQL sql = new SQL();


    Later on (in the same form) where a method call is required is a line:

    DataTable dtable = sql.getCurrentAppVersionInfo();



    So to modify your code you could do something like.... Obviously you'll need to change the method/class names etc

    // This goes with the other variable declarations (or in the relevant method, given you'll want to re-use it stick it at the top)

    TheOtherClass helperClass = new theOtherClass();

    // Change your onClick method to call the method in the external class

    public void Button2_OnServerClick(object sender, EventArgs e)
    {

    string email = subscribeNews.Text;

    if (helperClass.ValidateEmail(email))
    {
    emailValidation.InnerHtml = "Great Success!";
    }
    else
    {
    emailValidation.InnerHtml = "Did not compute!";
    }
    }


    Then your method in the helperClass accepts "email" (a string) as input and returns a bool representing whether its valid or not
     
  6. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    Ahhh, now this looks promising.
    I will test this when I get home from work.
    Thanks a bunch - many cookies for you!
     
  7. Highland3r

    Highland3r Minimodder

    Joined:
    25 Jul 2003
    Posts:
    7,558
    Likes Received:
    16
    Did you have any joy last night?
     
  8. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    Alas no, I didn't get the time to have a go at it yesterday. And today is a national holiday - celebrating the day of our constitution - so my family gets priority, though I might find time later tonight. I'll let you know how I get on. The deadline for the report is Sunday, so I still have a few days to sort it out. :)
     
  9. Journeyer

    Journeyer Minimodder

    Joined:
    31 Aug 2006
    Posts:
    3,039
    Likes Received:
    99
    I'm sorry, I totally forgot to extend my full gratitude for your help in this matter.
    After messing around with it a bit I finally got it to work, and the problem was invoking the methods in the external class. Again, thank you for your help - it is much appreciated.
     

Share This Page