Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:38:42+00:00 2026-06-05T00:38:42+00:00

Forgive the lengthy code here, and I also realise this may be a very

  • 0

Forgive the lengthy code here, and I also realise this may be a very basic fundamental question for any object-oriented developer, but I’m a front-end developer in at the deep end with .NET and trying to learn about classes and methods with an actual example. I’ve read resources to explain this stuff but immediately get stuck with the complexities of real-world code.

Basically I have a bunch of methods for adding comments to a web page and manipulating the status (marking as spam, deleting etc). Many of these methods call an ‘EmailNotification’ method, which sends an email to an administrator at each stage. It works great.

However, I’d like to use the ‘EmailNotification’ method elsewhere in the project, calling it from a different .cs file. When I try to do this it doesn’t recognise the method because (I think!?) it’s not a public static method.

Can anyone explain to me how to extract the EmailNotification method so that I can use it in different places around the code? I have tried creating a new class with this method inside it, but I just can’t get it to work.

using System;
using System.Net.Mail;

namespace UComment.Domain
{
public class Comment
{
    public delegate void CommentCreatedEventHandler(Comment sender, EventArgs e);
    public delegate void CommentDeletedEventHandler(Comment sender, EventArgs e);
    public delegate void CommentSpamEventHandler(Comment sender, EventArgs e);
    public delegate void CommentApprovedEventHandler(Comment sender, EventArgs e);

    public static event CommentCreatedEventHandler CommentCreated;
    public static event CommentDeletedEventHandler CommentDeleted;
    public static event CommentSpamEventHandler CommentSpam;
    public static event CommentApprovedEventHandler CommentApproved;

    protected virtual void OnCommentCreated(EventArgs e)
    {
        if (CommentCreated != null) CommentCreated(this, e);
    }

    protected virtual void OnCommentSpam(EventArgs e)
    {
        if (CommentSpam != null) CommentSpam(this, e);
    }

    protected virtual void OnCommentApproved(EventArgs e)
    {
        if (CommentApproved != null) CommentApproved(this, e);
    }

    protected virtual void OnCommentDelete(EventArgs e)
    {
        if (CommentDeleted != null) CommentDeleted(this, e);
    }


    public int Id { get; set; }
    public int ParentNodeId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Website { get; set; }
    public bool Spam { get; set; }
    public bool Approved { get; set; }
    public DateTime Created { get; set; }
    public string CommenText { get; set; }
    public int StatusId { get; set; }

    public Comment(int id)
    {
        Id = id;
        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        var reader = sqlHelper.ExecuteReader("select * from Comment where id = @id",
                                             sqlHelper.CreateParameter("@id", id));

        if(!reader.HasRecords) throw new Exception(string.Format("Comment with id {0} was not found", id));

        reader.Read();

        Name = reader.GetString("name");
        ParentNodeId = reader.GetInt("nodeid");
        Email = reader.GetString("email");
        Website = reader.GetString("website");
        Approved = reader.GetBoolean("approved");
        Spam = reader.GetBoolean("Spam");
        Created = reader.GetDateTime("created");
        CommenText = reader.GetString("comment");
        StatusId = reader.GetInt("statusid");
    }

    private Comment()
    {
    }

    /// <summary>
    /// Set as approved, mark as Not Spam - ignore HAM status
    /// </summary>
    public void MarkAsApproved()
    {
        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery(
             "update comment set approved = 1, spam = 0, statusid = 2 where id = @id",
             sqlHelper.CreateParameter("@id", Id));

        OnCommentApproved(EventArgs.Empty);

        // Send approval email
        EmailNotification(1);

    }

    /// <summary>
    /// Remove approval status. Ignore Spam and Ham states
    /// </summary>
    public void MarkAsNotApproved()
    {
        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery(
             "update comment set approved = 0, statusid = 3 where id = @id",
             sqlHelper.CreateParameter("@id", Id));

        OnCommentApproved(EventArgs.Empty);

        // Send rejection email
        EmailNotification(2);
    }



    /// <summary>
    /// Spam cannot be ham or approved
    /// </summary>
    public void MarkAsSpam()
    {
        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery(
             "update comment set spam = 1, ham = 0, approved = 0, statusid = 3 where id = @id",
             sqlHelper.CreateParameter("@id", Id));

        OnCommentSpam(EventArgs.Empty);

        // No email notification required - spammer not worthy of a reason for rejection
    }


    /// <summary>
    /// Ham is "not spam" - approved comments from Akismet. 
    /// </summary>
    public void MarkAsHam()
    {
        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery(
           "update comment set spam = 0, ham = 1 where id = @id",
           sqlHelper.CreateParameter("@id", Id));

        // No email notification required, simply marking spam as ham
    }

    public void Delete()
    {
        if (Id < 1) return;

        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery("delete from comment where id = @id", sqlHelper.CreateParameter("@id", Id));

        Id = -1;
        OnCommentDelete(EventArgs.Empty);

        // Permanent deletion
    }

    public void Reject()
    {
        if (Id < 1) return;

        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);
        sqlHelper.ExecuteNonQuery("update comment set statusid = 3 where id = @id", sqlHelper.CreateParameter("@id", Id));

        //Id = -1;
        //OnCommentDelete(EventArgs.Empty);

        // Send rejection email
        EmailNotification(2);
    }




    public static Comment MakeNew(int parentNodeId, string name, string email, string website, bool approved, bool spam, DateTime created, string commentText, int statusId)
    {

        var c = new Comment
            {
                ParentNodeId = parentNodeId,
                Name = name,
                Email = email,
                Website = website,
                Approved = approved,
                Spam = spam,
                Created = created,
                CommenText = commentText,
                StatusId = statusId
            };

        var sqlHelper = DataLayerHelper.CreateSqlHelper(cms.GlobalSettings.DbDSN);

        c.Id = sqlHelper.ExecuteScalar<int>(
            @"insert into Comment(mainid,nodeid,name,email,website,comment,approved,spam,created,statusid) 
                values(@mainid,@nodeid,@name,@email,@website,@comment,@approved,@spam,@created,@statusid)",
            sqlHelper.CreateParameter("@mainid", -1),
            sqlHelper.CreateParameter("@nodeid", c.ParentNodeId),
            sqlHelper.CreateParameter("@name", c.Name),
            sqlHelper.CreateParameter("@email", c.Email),
            sqlHelper.CreateParameter("@website", c.Website),
            sqlHelper.CreateParameter("@comment", c.CommenText),
            sqlHelper.CreateParameter("@approved", c.Approved),
            sqlHelper.CreateParameter("@spam", c.Spam),
            sqlHelper.CreateParameter("@created", c.Created),
            sqlHelper.CreateParameter("@statusid", c.StatusId));

        c.OnCommentCreated(EventArgs.Empty);

        if (c.Spam)
        {
            c.OnCommentSpam(EventArgs.Empty);
        }

        if (c.Approved)
        {
            c.OnCommentApproved(EventArgs.Empty);
        }

        return c;
    }

    public override string ToString()
    {
        return @"ParentNodeId " + ParentNodeId + @"
        Name " + Name + @"
        Email " + Email + @"
        Website " + Website + @"
        Approved " + Approved + @"
        Spam " + Spam + @"
        Created "+ Created + @"
        CommenText " + CommenText + Environment.NewLine;
    }


    /// <summary>
    /// Send email notification
    /// </summary>
    public void EmailNotification(int notificationType)
    {

        var uCommentAdminEmail = Config.GetUCommentSetting("uCommentAdminEmail");

        MailAddress to = null;
        MailAddress from = new MailAddress(uCommentAdminEmail);
        string subject = null;
        string body = null;

        switch (notificationType)
        {
            case 1:
                // Comment approved
                to = new MailAddress("me@mydomain.com");
                subject = "Comment approved";
                body = @"The comment you posted has been approved";
                break;
            case 2:
                // Comment rejected
                to = new MailAddress("me@mydomain.com");
                subject = "Comment rejected";
                body = @"The comment you posted has been rejected";
                break;
        }

        MailMessage message = new MailMessage(from, to);
        message.Subject = subject;
        message.Body = body;
        SmtpClient client = new SmtpClient();           

        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in EmailNotification: {0}", ex.ToString());
        }
        finally
        {
            //
        }


    }


}
}

Thanks for any pointers folks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:38:45+00:00Added an answer on June 5, 2026 at 12:38 am

    What you have here is a public method, but because it’s not declared as static (public static void EmailNotification…), it cannot be used without creating an instance of the class that it lives in.

    using System;
    
    namespace UComment.Domain
    {
        public class MyOtherClass
        {
             public void MyMethod()
             {
                 Comment c = new Comment();
                 c.EmailNotification(1);
             }
        }
    }
    

    You could declare the method static which would let you call it like this:

    using System;
    
    namespace UComment.Domain
    {
        public class MyOtherClass
        {
             public void MyMethod()
             {
                 Comment.EmailNotification(1);
             }
        }
    }
    

    If you’re trying to use it from a different namespace then you would need to include the namespace either by a using statement or by specifying the full namespace inline.

    using System;
    using UComment.Domain;
    
    namespace UComment.OtherNamespace
    {
        public class MyOtherClass
        {
             public void MyMethod()
             {
                 Comment c = new Comment();
                 c.EmailNotification(1);
             }
        }
    }
    

    Or

    using System;
    
    namespace UComment.OtherNamespace
    {
        public class MyOtherClass
        {
             public void MyMethod()
             {
                 UComment.Domain.Comment c = new UComment.Domain.Comment();
                 c.EmailNotification(1);
             }
        }
    }
    

    You are correct in thinking that if you wish to make this a common method, it should independent of the Comment class. The same limitations that I’ve just described apply to doing that. In addition, you’ll have to make sure that any appropriate using statements are on the new class and that the dependencies within the EmailNotification are accounted for as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Forgive me for this basic question. I have loaded a set of data as
Forgive me if this question is very vague Some time back i faced this
Forgive me for such a basic question; this is (I suppose) more of an
First question here, so please forgive me if this seems obvious. I'm having a
Forgive me if this is a stupid question. I'm a bit new to mobile
Forgive me if this is a stupid question, I know there have been similar
Forgive what might seem to some to be a very simple question, but I
Forgive me if this is a repeat question. I've searched StackOverflow and did not
I'm pretty new to Javascript, so forgive me if this is a simple question.
Forgive me if this is a silly question! But to run trinidad as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.