Introduction
This is a basic shell for creating your own string
class since the .NET's string
class is sealed leaving it unable to be inherited from.
Background
To create your own string
class, you will need to setup a few things.
- Name your new
string
type class. I named mine SuperString
, this can be anything you want that isn't already defined. - Implicit Operators:
string
type class must break down to another generic .NET class even if it's the very basic, System.Object
. Here's what implicit operators allow for in code: SuperString s = "hello";
The Basic Shell
public sealed class SuperString
{
public string _value;
public SuperString()
{
_value = null;
}
public SuperString(string value)
{
_value = value;
}
public static implicit operator SuperString(string value)
{
if (value == null)
return null;
return new SuperString(value);
}
public override string ToString()
{
return _value;
}
}
Better Version
My version of this class adds in two new properties: CompositionType
, CaseType
.
CompositionType
can be one of three values: Mixed
, AllNumbers
, AllLetters
.CaseType
can be one of three values: Mixed
, Upper
, Lower
.
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace DataTypes
{
public class SuperString : System.Collections.Generic.IList<char>
{
public enum CompositionType
{
Mixed,
AllNumbers,
AllLetters
}
public enum CaseType
{
Mix,
Lower,
Upper
}
private StringBuilder _value;
public SuperString()
{
_value = new StringBuilder(null);
}
public SuperString(string value)
{
_value = new StringBuilder(value);
}
public static implicit operator SuperString(string value)
{
if (value == null)
return null;
return new SuperString(value);
}
public override string ToString()
{
return _value.ToString();
}
public CompositionType GetCompisition()
{
if (IsNumeric(_value))
return CompositionType.AllNumbers;
else if (Regex.IsMatch(_value.ToString(), @"^[a-zA-Z]+$"))
return CompositionType.AllLetters;
else
return CompositionType.Mixed;
}
public CaseType GetCase()
{
if (Regex.IsMatch(_value.ToString(), @"^[A-Z]+$"))
return CaseType.Upper;
else if (Regex.IsMatch(_value.ToString(), @"^[a-z]+$"))
return CaseType.Lower;
else
return CaseType.Mix;
}
private bool IsNumeric(System.Object Expression)
{
if (Expression == null || Expression is DateTime)
return false;
if (Expression is Int16 || Expression is Int32 || Expression is Int64 ||
Expression is Decimal || Expression is Single ||
Expression is Double || Expression is Boolean)
return true;
try
{
if (Expression is string)
Double.Parse(Expression as string);
else
Double.Parse(Expression.ToString());
return true;
}
catch { }
return false;
}
}
}
Points of Interest
After a bit of searching on Google, I found that the string
is sealed and complicated to reproduce. I had to set out to create a shell to start with since I will likely keep adding on to this. I also found an open source version of the .NET framework and the base classes like string
, int
, double
, etc.
History
I started learning HTML in 7th grade (2007) then went on to learn any and everything I could.
HTML/CSS/JavaScript/ActionScript/ASP.Net (6 years)
Basic (5 years)
C/C++ (4 years)
Java (4 years)
C# (3 years)
VB (2 years)
Out of all of these I have to say C# is by far my favorite since its wide spread, works for almost any solution and allows me to use Visual Studio.