Hi
I am new at C# and would like to know how to write a new type of string
Let me explain
I want to build a string class
I mean, I need to build a class that I limit the size of a string and overload the operators so I can use it like a string
Example:
Class nString{
String str;
int size;
public nString(int maxsize){
size = maxsize;
}
str{
set{
here I limit the string str length to the size property of this class
}
Get{
Return str;
}
}
}
But, I would like to use it as a normal string
How would I do that?
Example:
nString stringer = new nString(20);
stringer =
Comments
[code]
public class nString
{
private string _str = string.Empty;
public nString() { }
public nString(string str) { _str = str; }
// NOTE: Called when nString = string
public static implicit operator nString(string str)
{
return new nString(str);
}
// NOTE: Called when string = nString
public static implicit operator string(nString str)
{
return str._str;
}
}
[/code]
The implicit or explicit overator lets you determine how objects behave when you try to assign it to specific variables. In this example, there is an implicit operator that lets you store the nString as a string and visa versa. If you use the explicit keyword instead then you would have to use cast operators to tell the compiler explicitedly.
Here is how you use your class now:
[code]
nString nstr = new nString();
nstr = "Test";
MessageBox.Show(nstr); // This would not normally work
[/code]
: Hi
: I am new at C# and would like to know how to write a new type of
: string
: Let me explain
: I want to build a string class
: I mean, I need to build a class that I limit the size of a string
: and overload the operators so I can use it like a string
: Example:
:
: Class nString{
: String str;
: int size;
:
: public nString(int maxsize){
: size = maxsize;
: }
:
: str{
: set{
: here I limit the string str length to the size property of
: this class
: }
: Get{
: Return str;
: }
: }
: }
: But, I would like to use it as a normal string
: How would I do that?
: Example:
: nString stringer = new nString(20);
: stringer =
: public static implicit operator nString(string str)
: {
: return new nString(str);
: }
because if i do this, then I lose the size that i entered in the declaration of the variable
[code]
: public class nString
: {
: private string _str = string.Empty;
private int size = 0;
:
: public nString() { }
public nString(int newSize){ size = newSize; }
: public nString(string str) { _str = str; }
:
: // NOTE: Called when nString = string
: public static implicit operator nString(string str)
: {
: return new nString(str);
: }
:
: // NOTE: Called when string = nString
: public static implicit operator string(nString str)
: {
: return str._str;
: }
:
: }
: [/code]
lets say i do
nString newStr = new nString(100);
then, if I do newStr = "TEST";
i will losse the 100 size that I entered, right?
i want to build a class that i tell the maximum length of the string without having to always have to do that, i mean, i want to say the size only at the declaration
like if it was char[100] in c++
then i remembered the c# overload...
can this be done?!
thanks for your help!!
: You can use operator overloading
:
: [code]:
: public class nString
: {
: private string _str = string.Empty;
:
: public nString() { }
: public nString(string str) { _str = str; }
:
: // NOTE: Called when nString = string
: public static implicit operator nString(string str)
: {
: return new nString(str);
: }
:
: // NOTE: Called when string = nString
: public static implicit operator string(nString str)
: {
: return str._str;
: }
:
: }
: [/code]:
:
: The implicit or explicit overator lets you determine how objects
: behave when you try to assign it to specific variables. In this
: example, there is an implicit operator that lets you store the
: nString as a string and visa versa. If you use the explicit keyword
: instead then you would have to use cast operators to tell the
: compiler explicitedly.
:
: Here is how you use your class now:
:
: [code]:
: nString nstr = new nString();
: nstr = "Test";
:
: MessageBox.Show(nstr); // This would not normally work
: [/code]: