Overriding ToString() with arguments
-
I have a struct:
public struct IITObjectPersistentID
{
public int highID;
public int lowID;public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } }
I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:
IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
string displayAsHex = OPIDx.ToString("X");How can I write conditional code in the override statement that lets me pass arguments to
OPIDx.ToString()
? -
I have a struct:
public struct IITObjectPersistentID
{
public int highID;
public int lowID;public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } }
I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:
IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
string displayAsHex = OPIDx.ToString("X");How can I write conditional code in the override statement that lets me pass arguments to
OPIDx.ToString()
?You don't. You just overload ToString by writing a new ToString method that takes the argument. Just as int does.
public string
ToString
(
string Format
)
{
return "{" + highID.ToString ( Format ) + "." + lowID.ToString ( Format ) + "}" ;
} -
I have a struct:
public struct IITObjectPersistentID
{
public int highID;
public int lowID;public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } }
I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:
IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
string displayAsHex = OPIDx.ToString("X");How can I write conditional code in the override statement that lets me pass arguments to
OPIDx.ToString()
?You don't need to override ToString if your passing arguments. Every object has a default ToString() with no parameters, which will return the objects name. If you need your own paramaterless ToString then you use override to override the default method with the same signature.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
You don't need to override ToString if your passing arguments. Every object has a default ToString() with no parameters, which will return the objects name. If you need your own paramaterless ToString then you use override to override the default method with the same signature.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)I'm not following you. My struct has two fields. The default ToString() simply returns the name of the struct. I want my ToString to either return the format I provided {122345.67879} or, say, the same fields in hex {FFFFF.00000}. (yes I realize that's not proper hex) So how do I write my ToString override to accept an argument (like "X")?
-
You don't. You just overload ToString by writing a new ToString method that takes the argument. Just as int does.
public string
ToString
(
string Format
)
{
return "{" + highID.ToString ( Format ) + "." + lowID.ToString ( Format ) + "}" ;
} -
I'm not following you. My struct has two fields. The default ToString() simply returns the name of the struct. I want my ToString to either return the format I provided {122345.67879} or, say, the same fields in hex {FFFFF.00000}. (yes I realize that's not proper hex) So how do I write my ToString override to accept an argument (like "X")?
Voting 1 because you don't understand isn't going to get you very far on these forums! I'm in a good mood today however so how about something like this?
public override string ToString()
{
return ToString(string.Empty);
}
public string ToString(string format)
{
return "{" + highID.ToString(format) + "." + lowID.ToString(format) + "}";
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Voting 1 because you don't understand isn't going to get you very far on these forums! I'm in a good mood today however so how about something like this?
public override string ToString()
{
return ToString(string.Empty);
}
public string ToString(string format)
{
return "{" + highID.ToString(format) + "." + lowID.ToString(format) + "}";
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I'm sorry, your code confuses me. Are you saying I just need to remove the "override" portion of my method?
You do appear to be saying that, then, as it worked! Thanks. So for any subsequent readers, the answer is: You write the first ToString method as "override" and without parameters - then you write more ToString() overloaded methods without the "override" conditional/declaration/whatever that part of the signature is. So:
public struct IITObjectPersistentID { public int highID; public int lowID; public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } public string ToString(string Format) { return "{" + highID.ToString(Format) + "." + lowID.ToString(Format) + "}"; } }
-
I have a struct:
public struct IITObjectPersistentID
{
public int highID;
public int lowID;public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } }
I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:
IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
string displayAsHex = OPIDx.ToString("X");How can I write conditional code in the override statement that lets me pass arguments to
OPIDx.ToString()
?You just need to crate one more method in you structure with ToString which contains single argument. like
public struct IITObjectPersistentID {
public int highID;
public int lowID;
public IITObjectPersistentID(int HighID, int LowID)
{
highID = HighID;
lowID = LowID;
}
public override string ToString()
{
return "{" + highID.ToString() + "." + lowID.ToString() + "}";
}internal string ToString(string str) { //Do ur code here }
}
-
I have a struct:
public struct IITObjectPersistentID
{
public int highID;
public int lowID;public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } }
I have overridden ToString(), but I want to be able to specify the normal ToString() arguments. Ie, I want to be able to get the hex display of the numbers, too, like:
IITObjectPersistentID OPIDx = new IITObjectPersistentID(25,50);
string displayAsHex = OPIDx.ToString("X");How can I write conditional code in the override statement that lets me pass arguments to
OPIDx.ToString()
?Hi, there are no optional parameters in C#; each combination of parameters results in another method. ToString() and ToString(string) are two different methods that share their name, but not their signature (=list of parameter types). Object class has a ToString() but not a ToString(string). So when you class derives from Object, it needs an override to provide its own ToString() and it is not allowed an override keyword on ToString(string). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
You do appear to be saying that, then, as it worked! Thanks. So for any subsequent readers, the answer is: You write the first ToString method as "override" and without parameters - then you write more ToString() overloaded methods without the "override" conditional/declaration/whatever that part of the signature is. So:
public struct IITObjectPersistentID { public int highID; public int lowID; public IITObjectPersistentID(int HighID, int LowID) { highID = HighID; lowID = LowID; } public override string ToString() { return "{" + highID.ToString() + "." + lowID.ToString() + "}"; } public string ToString(string Format) { return "{" + highID.ToString(Format) + "." + lowID.ToString(Format) + "}"; } }
Correct, the term to remember here is "overload".