Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Override ToString()

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
Don't know if this can even be done or if my thinking is way off but I'll try to explain what I would like to do.

I've got a class called "Issue"; it holds properties for a Help Desk Issue (IssueID, UserID, IssueTypeID, etc.). I have a need in the code-behind to store the "string" values of these properties at any time. I originally thought of a simple stored procedure that would go out and return the string value but I have close to 15+ properties of the "int" type that are ID's and that would make for a lot of SP calls and DB hits.

So I was wondering if there's a way to override the ToString() method of any of my int type properties so I can handle it once in there. Pseudo Code might look something like:
Code:
public int UserID {get; set;}
public int IssueTypeID {get; set;}

public override int ToString()
{
   //code here to override the int.
   return "test";
}

Then in code elsewhere it would be:
Code:
string IssueType = oIssue.IssueTypeID.ToString();

So the variable IssueType would have the string value of "test".

Make any sense? From a programming standpoint, does it make any sense to try to accomplish this? Can it be done?

Thanks for any info & help!
 
The first thing I see that may cause an issue is your are saying in your method line to return an int value
Code:
public override [b]int[/b] ToString()
but then you are returning a string
Code:
return "test";
I think the complier will fail you here.

I am not sure I fully understand your need but why not just return each property as an int and then convert as a string in the calling code, OR just create string properties of each each (duplicating work yes) but then return the string representaton of the int
Code:
 class Issue
    {
        int gintNumber = 100;
        public int Number
        {
            get { return gintNumber; }
            set { gintNumber = value; }
        }

        public string sNumber
        {
            get { return gintNumber.ToString(); }
        }
    }

 
another way to do it like this.

Code:
class Issue
    {
        string gstrPropertyIWant = string.Empty;
        public string PropertyIWant{set { gstrPropertyIWant = value; }}
        public int Number1{get { return 100; }}
        public int Number2{get { return 200; }}
        public int Number3{get { return 300; }}
        public int Number4{get { return 400; }}

        public override string ToString()
        {
            System.Reflection.PropertyInfo[] pis = (typeof(Issue)).GetProperties(BindingFlags.Public|BindingFlags.Instance);
            string lstrValue = string.Empty;
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name.Equals(gstrPropertyIWant))
                {
                    int objvalue = 0;
                    objvalue = Convert.ToInt32(pi.GetValue(this, null));
                    lstrValue += string.Format("{0} is {1}", pi.Name, objvalue.ToString());
                    lstrValue += Environment.NewLine;
                }
            }
            return string.Format(lstrValue);
        }
    }

You have one property that will let the user ask for a specific property and when you call ToString, you return just that one.

Code:
class Program
    {
        static void Main(string[] args)
        {
            Issue i = new Issue();
            i.PropertyIWant = "Number2";
            Console.WriteLine(i.ToString());
            Console.ReadLine();

        }
    }
// Output is [b]Number2 is 200[/b]

Does that work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top