Results 1 to 4 of 4

Thread: Implicit operation! Help please

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Implicit operation! Help please

    Hi people!

    I have created a class that contains a single DateTime property, called Date.

    Like this

    Code:
    class MyDate
    {
    	private DateTime _date;
    
    	public MyDate(DateTime defaultValue)
    	{
    		_date = defaultValue;
    	}
    
    	public DateTime Date
    	{
    		get{return _date;}
    		set{_date = value;}
    	}
    }
    I have also created another class that has an Instance of MyDate stored inside

    Code:
    public class MyOtherClass
    {
    	private MyDate _myDate;
    	
    	public MyOtherClass
    	{
    		_myDate = new MyDate(DateTime.Now);
    	}
    
    	public MyDate MyDate
    	{
    		get{return _myDate;}
    		set{_myDate = value;}
    	}
    }
    Its all quite simple... What i want to do is bind the property MyDate in the MyOtherClass to the Text property of a TextBox

    Code:
    this.TextBox1.DataBindings.Add("Text",anInstanceOfMyOtherObject,"MyDate")
    This is fine, when the code executes the ToString method of MyDate is called and the current date is displayed... Oh... the ToString method is not shown above...

    Now when i change the value in the text box i want to update the value in anInstanceOfMyOtherObject.MyDate.Date

    I have tried writting implicit conversions for converting string to MyDate objects but this isn't working. Any suggestions?

    Thanks Rohan
    You dont need eyes to see, you need vision.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Implicit operation! Help please

    The reason you're getting an implicit conversion is because you're attempting to bind it to a reference type property instead of a data type. If you want to use that second layer down the nest, you need to do something like this:
    Code:
    		private MyOtherClass anInstanceOfMyOtherObject;
    		
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			anInstanceOfMyOtherObject = new MyOtherClass();
    			this.textBox1.DataBindings.Add("Text", anInstanceOfMyOtherObject.MyDateProperty, "Date");
    		}
    
    		public class MyDate
    		{
    			private DateTime _date;
    
    			public MyDate(DateTime defaultValue)
    			{
    				_date = defaultValue;
    			}
    
    			public DateTime Date
    			{
    				get{return _date;}
    				set{_date = value;}
    			}
    		}
    
    		public class MyOtherClass
    		{
    			private MyDate _myDate;
    	
    			public MyOtherClass()
    			{
    				_myDate = new MyDate(DateTime.Now);
    			}
    
    			public MyDate MyDateProperty
    			{
    				get{return _myDate;}
    				set{_myDate = value;}
    			}
    		}

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Re: Implicit operation! Help please

    This is exactly what I was trying to not to do. I dont want to specify the Date property in the data bindings, i want the text in the textbox to be implicitly converted to MyDate object
    You dont need eyes to see, you need vision.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Implicit operation! Help please

    Then either let your MyDate property be a string which you validate whenever it is assigned, or inherit the textbox, make your own and let its text property/custom property return a date.

    OR

    Use a datetime picker.

    OR

    Create your own custom control for this purpose, such as three comboboxes.

Posting Permissions

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



Click Here to Expand Forum to Full Width