|
-
Mar 2nd, 2006, 01:40 PM
#1
Thread Starter
Addicted Member
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.
-
Mar 2nd, 2006, 02:45 PM
#2
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;}
}
}
-
Mar 3rd, 2006, 04:12 AM
#3
Thread Starter
Addicted Member
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.
-
Mar 4th, 2006, 07:13 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|