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

  • Home
  • SEARCH
  • 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 351415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:38:30+00:00 2026-05-12T11:38:30+00:00

First let me explain how I currently handle validation, say, for an IPv4 address:

  • 0

First let me explain how I currently handle validation, say, for an IPv4 address:

public struct IPv4Address {
    private string value;

    private IPv4Address(string value) {
        this.value = value;
    }

    private static IPv4Address CheckSyntax(string value) {
        // If everything's fine...
        return new IPv4Address(value);

        // If something's wrong with the syntax...
        throw new ApplicationException("message");
    }

    public static implicit operator IPv4Address(string value) {
        return CheckSyntax(value);
    }

    public static implicit operator string(IPv4Address address) {
        return address.value;
    }
}

I have a bunch of structs like this one.

They often have additional private members to handle things but not methods are exposed publicly.

Here is a sample usage:

IPv4Address a;
IPv4Address b = "1.2.3.4";
a = b;
b = "5.6.7.8";

string address = a;

// c contains "1.2.3.4"
IPv4Address c = address;

// See if it's valid
try {
    IPv4Address d = "111.222.333.444";
}
catch (ApplicationException e) {
    // Handle the exception...
}

I can feel that there’s something very disturbing with this, hence I’m pondering about switching to a static class with methods like IsIPv4Address and so on.

Now, here’s what I think is wrong with the approach above:

  • New team members will have to wrap their heads around this

  • It might impede integration with 3rd party code

  • Exceptions are expensive

  • Never seen anything like this and I am a conservative type at heart 🙂

And then what I like about it:

  • Very close to having a lot of specialized primitives since you have value types.

  • In practice they can be often used like primitive types would, for example it isn’t a problem passing the struct above to a method that accepts a string.

  • And, my favourite, you can pass these structs between objects and be sure that they contain a syntactically valid value. This also avoids having to always check for correctness, which can be expensive if done unnecessarily and even forgotten.

I can’t find a fatal flaw to the approach above (just a beginner here), what do you think?

Edit: as you can infer from the first line this is only an example, I’m not asking for a way to validate an IP address.

  • 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-05-12T11:38:30+00:00Added an answer on May 12, 2026 at 11:38 am

    First of all, you should read posts about implicit casting, and when to use it (and why it’s bad to use it in your scenario), you can start here.
    If you need to have checking methods, they should be public static, instead of such strange constructs, beside this, having such methods allows you to choose if you want to throw exceptions (like .Parse() methods do), or signal by returning some value that should be checked (like .TryParse() methods).
    Beside this, having static methods for creating valid objects doesn’t mean you can’t use value type (struct) instead of a class, if you really want to. Also, remember that structs have implicit empty constructor, that you can’t “hide”, so even your construct can be used like this:

    IPv4Address a = new IPv4Address();

    which will give you invalid struct (value will be null).

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

Sidebar

Related Questions

First let me explain you what I mean by a nested transaction. Example: say
I'm using Subclipse 1.6x plugin in Eclipse. Let me explain the scenario first: Say
First, let me explain that I'm using Spring MVC 3.1.1, and Hibernate validation 4.2.0.
First let me explain. I have several addresses on the page that I put
Let me first explain what's I'm trying to do. So I have a entity
First things first, let me explain my situation: I'm working on a client and
Quite new to maven here so let me explain first what I am trying
It might sound unnecessary, but let me explain my problem first. Probably then it
i am stuck at scope resolution in python. let me explain a code first:
First let me say I have read this useful article thoroughly and am using

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.