Modfifying ToString() and Passing values from struct to Main Method()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Modfifying ToString() and Passing values from struct to Main Method()

  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Modfifying ToString() and Passing values from struct to Main Method()

    In the following code, I am trying to change it to declare a read-only property Hour returning the number of hours and a read-only property Minute returning the number of minutes. For instance, new Time(23, 45).Minute should be 45. I need to modify the ToString() method so that it shows a Time in the format hh:mm, for instance 10:05, instead of 605. How can I use String.Format to do the formatting in the Main() Method?



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Timers;
    namespace TestTime
    {
    class Program
    {
    static void Main(string[] args)
    {

    Time timenow = new Time(1,2);

    Console.WriteLine("Time={0} ", timenow);
    Console.WriteLine("Press Any Key");
    Console.ReadKey();

    }
    public struct Time
    {
    public readonly int minutes;

    public Time(int hh, int mm)
    {
    Console.WriteLine("Enter Hour");
    hh = Int32.Parse( Console.ReadLine());
    Console.WriteLine("Enter Min");
    mm = Int32.Parse( Console.ReadLine());

    this.minutes = 60 * hh + mm;
    }
    public override String ToString()
    {
    return minutes.ToString();
    }


    }
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,295

    Re: Modfifying ToString() and Passing values from struct to Main Method()

    Have you looked up how to use String.Format( ) in msdn?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center