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 8685507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:35:33+00:00 2026-06-12T22:35:33+00:00

I’m trying to create classes to encapsulate validation and logic for objects like Email

  • 0

I’m trying to create classes to encapsulate validation and logic for objects like Email , URL , phone number and so on . in the first try I found that I’m repeating the same code in all classes specially static IsValid and the Constructor . so I decided to create a base class to put all the same codes in it . so there is a base class that other classes inherit it . it’s abstract as I don’t want it to be used directly .

public abstract class BaseClass
{
            protected string value;
            private bool isValid;

            public bool IsValid{get { return this.isValid;}}

    protected virtual string RegexPattern{get;}

    protected virtual RegexOptions RegexOption{get;}

    private BaseClase(){}

    protected BaseClass(string value)
    {
        this.isValid = Validator.IsValid(value , RegexPattern, RegexOption);
        this.value = this.isValid ? value : string.Empty;
    }

    public static bool Validate(string value)
    {
        return Validator.IsValid(value ,RegexPattern, RegexOption); // rror
    }

}

public class Email
{
    private override string RegexPattern
    {
        get
        {
            return ".*";
        }
    }

    private override RegexOptions RegexOption
    {
        get
        {
            return RegexOptions.SingleLine;
        }
    }

            public string Address{get {return this.value; }}

    public Email(string address) : base(address){}
}

the problem is with the static IsValid method of BaseClass . in the current code it throws errors as RegexPattern and RegexOption are instance members . I don’t want to defined the same value twice for instance and static methods . it works if I use

const string RegexPattern= ".*";
const RegexOptions RegexOption =RegexOptions.SingleLine;

but I need to be able to override these values in sub classes so this is not applicable . and as I have defined the BaseClass as abstract I cant instantiate it inside static method to have access to properties .

and as I want to use static method just like Email.IsValid("foo@bar.com"); , I don’t know how to pass an instance through it .

so , how can I have access to RegexOption and RegexPattern in this static method ?

  • 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-12T22:35:34+00:00Added an answer on June 12, 2026 at 10:35 pm

    so , how can I have access to PropOne and PropTwo in this static method ?

    You need to have an instance somehow, otherwise it’s a meaningless operation. So the question is – how would you want to identify the instance that you’re interested in? Do you really need Validate to be static at all?

    Note that instead of having abstract properties, if you expect the values to always stay the same throughout an instance of the class, you might want to make the values part of the constructor for BaseClass instead, and just keep them in fields.

    If what you’re trying to achieve is that each subclass has a single separate validator, I would separate the two concerns – give each subclass a static property of a different type. You wouldn’t be able to call this polymorphically, but it sounds like you don’t really want to anyway.

    We can’t really tell what your classes are meant to represent here – if you can give us more concrete context, we can probably be more helpful.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I would like to run a str_replace or preg_replace which looks for certain words

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.