C# creating my own class that acts like a string
-
I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:
public class ORSerialLotNumber : string {}
public class SerialNumber : ORSerialLotNumber {}
public class LotNumber : ORSerialLotNumber {}// ......
List mySLNList = new List();
SerialNumber sNum1 = "Sd-23s-3sf";
LotNumber nNum1 = "2342434";mySLNList.Add(sNum1);
mySLNList.Add(nNum1);// ......
foreach(var v in mySLNList)
if(v.GetType() == typeof(SerialNumber))
string serial = v;
if(v.GetType() == typeof(LotNumber))
string lot = v; -
I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:
public class ORSerialLotNumber : string {}
public class SerialNumber : ORSerialLotNumber {}
public class LotNumber : ORSerialLotNumber {}// ......
List mySLNList = new List();
SerialNumber sNum1 = "Sd-23s-3sf";
LotNumber nNum1 = "2342434";mySLNList.Add(sNum1);
mySLNList.Add(nNum1);// ......
foreach(var v in mySLNList)
if(v.GetType() == typeof(SerialNumber))
string serial = v;
if(v.GetType() == typeof(LotNumber))
string lot = v;That example doesn't really depend on it being a struct, it's about the implicit conversion operator. Here's a minimal implementation of a class to implicitly converts to and from string:
class CustomString
{
public static implicit operator CustomString(string s)
{
return new CustomString();
}public static implicit operator string(CustomString s) { return ""; }
}
Do that for both subclasses. By the way, as far as I can tell you should be able to use is[^] instead of the GetType() == typeof() "not-quite-anti-pattern-but-close".
-
That example doesn't really depend on it being a struct, it's about the implicit conversion operator. Here's a minimal implementation of a class to implicitly converts to and from string:
class CustomString
{
public static implicit operator CustomString(string s)
{
return new CustomString();
}public static implicit operator string(CustomString s) { return ""; }
}
Do that for both subclasses. By the way, as far as I can tell you should be able to use is[^] instead of the GetType() == typeof() "not-quite-anti-pattern-but-close".
Thanks, this seems to be just what I need, though I imagine that I need to save the actual string value and have the constructor set that value and the operator string(CustomString s) return the string instead of an empty string?
class CustomString
{
private string _value;private CustomString(string s) { \_value = s; } public static implicit operator CustomString(string s) { return new CustomString(s); } public static implicit operator string(CustomString s) { return s.\_value; }
}
Would I also be able to implement INotifyPropertyChanged with this class? I don't see how as there's no public property. Would I just use "_value" even though it's private?
-
Thanks, this seems to be just what I need, though I imagine that I need to save the actual string value and have the constructor set that value and the operator string(CustomString s) return the string instead of an empty string?
class CustomString
{
private string _value;private CustomString(string s) { \_value = s; } public static implicit operator CustomString(string s) { return new CustomString(s); } public static implicit operator string(CustomString s) { return s.\_value; }
}
Would I also be able to implement INotifyPropertyChanged with this class? I don't see how as there's no public property. Would I just use "_value" even though it's private?
-
There wouldn't be a whole lot of point in implementing the interface if he doesn't! :laugh:
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
So the idea would be to include a public property, but still have the implicit operator act on that property? Would that then allow someone to either use the implicit operator or the property directly?
-
So the idea would be to include a public property, but still have the implicit operator act on that property? Would that then allow someone to either use the implicit operator or the property directly?
-
No, I just didn't even think about this, or even know that it was possible. I'm reading more about `implicit`, which seems very useful. It doesn't seem to be touched on in most 'beginner' reading, though.
-
I am trying to create some classes that act like a string, but allow me to test the type at a later date. Because string and String are sealed, I can't seem to inherit from them. I also found out you can't override the assignment operator. I found something close to what I want here on stackoverflow, but it uses structs which can't be inherited. Here's a example of what I want to do, but I can't figure out how:
public class ORSerialLotNumber : string {}
public class SerialNumber : ORSerialLotNumber {}
public class LotNumber : ORSerialLotNumber {}// ......
List mySLNList = new List();
SerialNumber sNum1 = "Sd-23s-3sf";
LotNumber nNum1 = "2342434";mySLNList.Add(sNum1);
mySLNList.Add(nNum1);// ......
foreach(var v in mySLNList)
if(v.GetType() == typeof(SerialNumber))
string serial = v;
if(v.GetType() == typeof(LotNumber))
string lot = v;I'm not quite sure what you're trying to achieve here. I think it is that you want a data object that has a 'number' (a string identifier) and also a type. Using GetType or is/as functions is generally a sign that you haven't quite got the solution you want; switching based on type usually implies you need more polymorphic behaviour. I think the better solution is
enum ItemType { Serial, Lot };
struct ObjectIdentifier {
string Identity { get; set; }
ItemType Type { get; set; }
}List<ObjectIdentifier> mySLNList = ...;
foreach(ObjectIdentifier oi in mySLNList)
switch(oi.Type)
...