5

I am extending some of the basic data types in vb.net for a large application. This includes integer, string, short, etc. As it stands, my new data type objects have names like MYInteger and MYString. Since these are the only types I'm using for my application and they are mostly compatible with the default types, is there a way I can override the defaults with mine so when you Dim iThing as Integer you're actually using my slightly customized integer type?

3
  • 2
    Is there a reason you have to use new primitive types? Why could you not use extension methods?
    – cdhowie
    Nov 12, 2010 at 21:49
  • 1
    I think that breaks just about every programming rule. Nevertheless, I also don't think it's possible. Overall, it sounds like a good candidate for extension methods.
    – Inisheer
    Nov 12, 2010 at 21:51
  • 2
    First check if the FBI's witness protection program is also accessible to programmers who have recently quit their job. Nov 12, 2010 at 21:59

3 Answers 3

6

No. If you could, then imagine the chaos that would cause. There would be two possibilities in this scenario:

  1. All code running in the same process would use those types, even the code that isn't expecting it. Bad.

  2. Your concept of an Integer is different from the CLR's concept of an Integer, and suddenly two objects of the same type are different. Bad.

You can add extension methods to sealed types. Something like:

Module MyExtensions
    <Extension()>
    Public Function Extended(i as Integer) As Integer
        return i + 4
    End Function
End Module

4.Extended() ' evaluates to 8
1

I'm not sure if you really want to do that. It will make it more difficult to maintain in the future. Why not just use MyInteger,etc. If you are importing external source code, just do a find&replace.

1
  • 1
    I was afraid I couldn't do that. It's mostly because the code in question is going to be worked upon by a lot of people and we've already had problems with some people using normal integers and some using the custom type and then lots of chaos ensuing. Some functions are written to use the default and some the custom and I was looking to simplify things.
    – user457586
    Nov 12, 2010 at 22:05
1

I don't know if it applies in your case, but sometimes people want to make what amounts to "specialized" scalar/primitive types.

For example, see some of the types defined in this codeplex project. Some of the types include Latitude, Longitude, Angle, etc. Each of these types is actually a struct with a single data member, usually a double, float, or int/long, such as this:

public struct Latitude : IFormattable, IComparable<Latitude>, IEquatable<Latitude>
{
  private readonly double _DecimalDegrees;

  //Some constants

  //Some constructors

  //Some static fields like...
  public static readonly Latitude Equator = new Latitude(0.0);
  public static readonly Latitude TropicOfCapricorn = new Latitude(-23.5);
  public static readonly Latitude TropicOfCander = new Latitude(23.5);

  //Some operators
  public static Latitude operator +(Latitude left, Latitude right)
  {
    return new Latitude(left.DecimalDegrees + right.DecimalDegrees);
  }

  public static Latitude operator +(Latitude left, double right)
  {
    return new Latitude(left.DecimalDegrees + right);
  }
}

Technically, these types are just class or structs (primarily structs in the case of the linked project), but they are used to represent values that are often (usually?, almost always??) simply scalar values. If you have an object that has an Angle property, most people would probably just make that a double.

public MyObject
{
  public double AngleInDegrees { set; get; }
}

When a value is assigned to AngleInDegrees, MyObject might want to do some processing:

public double AngleInDegrees
{
  get
  {
    return _AngleInDegrees;
  }
  set
  {
    if (value < 0 || value > 360)
    {
      _AngleInDegrees = NormalizeAngle(value);
    }
    else
    {
      _AngleInDegrees = value;
    }
  }
}

What if you had an AngleInDegrees property on many objects? What if you have a class that consumes angles produced by other components in your application? Who should do the validation? It is useful to be able to count on always working with "good" angles?

By having an AngleInDegrees type, it becomes possible to put all validation and special "Angle" operations in the type. It also becomes possible to strongly type all of the places where you want to use AngleInDegrees.

As I said, I don't know if this is the type of thing that you are trying to achieve or not. It just occurred to me as I read your question that I had sort of the same thought when I first saw the project that I linked (i.e. that it seemed that they were, in effect, subclassing the primitive data types to make more restrictive primitive types).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.