|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Override the propertyto null. I use it for data I get from my database to tell me if I need to update or reset the data to nulls. I am making changes to it and want to be able to do the same thing that you can do with nullable types. If you have int? and you want to change the value you can do: int? something; something.value = x; or something = x; You don't have to specify the property. In my code I do something.Data = x. Where the property is set as: public object Data { get { return _objCurrent; } set { if (value == DBNull.Value) value = null; if (value != null) { _ValidateType(value); } _objCurrent = value; _fChanged = true; } } This handles the data or nulls. How can I change this to allow me to call set the variable by either calling the property or not calling it? The code itself (with some removed as it is pretty large) is: ************************************************** using System; using System.IO; namespace FtsData { abstract class DataType { protected object _objInitial; protected object _objCurrent; private bool _fChanged; public bool IsNull { get { return _objCurrent == null; } } public bool IsFirstNull { get { return _objInitial == null; } } // Reset _objInitial to _objCurrent and changed flag to false to track // when this variable changes again. This would be necessary if were to // write out data to a database record and need to track when it changes again public void Reset() { _objInitial = _objCurrent; _fChanged = false; } public object First { get { return _objInitial; } } public object Data { get { return _objCurrent; } set { if (value == DBNull.Value) value = null; if (value != null) { _ValidateType(value); } _objCurrent = value; _fChanged = true; } } // Likewise, I don't think Changed should include a setter public bool Changed { get { return _fChanged; } } // This is what deriving classes will define so the type can be checked protected abstract Type _TypeRequired { get; } private void _ValidateType(object obj) { Type typeRequired = _TypeRequired; // Depending on how you're using this class, you may instead prefer // to check for exact type equality. The below simply requires that // the passed-in object has the required type in its inheritance chain. if(obj == DBNull.Value) { _fChanged = _fChanged; } if (!typeRequired.IsInstanceOfType(obj)) { throw new ArgumentException("assigned value type of " + obj.GetType().Name + " is incompatible with required type of " + typeRequired.Name); } } } class BoolType : DataType { public BoolType() { } public BoolType(bool initial) { Type type = Type.GetType("initial"); Console.WriteLine("Type = {0}",type); _objInitial= initial; _objCurrent = initial; } // Each class defines this so that the base type can validate the data's type protected override Type _TypeRequired { get { return typeof(bool); } } // A convenience method so that no casting is needed when you already have // a fully typed object public bool TypedData { get { return (bool)Data; } set { Data = value; } } } class StringType : DataType { public StringType() { } public StringType(string initial) { Type type = initial.GetType(); Console.WriteLine("Type = {0}",type); _objInitial= initial; _objCurrent = initial; } protected override Type _TypeRequired { get { return typeof(string); } } public string TypedData { get { return (string)Data; } set { Data = value; } } } } ************************************************** Thanks, Tom
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message The feature you are looking for is the implicit operator. It converts the news:erbqCMhXJHA.4596@TK2MSFTNGP06.phx.gbl... >I have a class that I have that tracks when an object has changed or is set >to null. > > I use it for data I get from my database to tell me if I need to update or > reset the data to nulls. I am making changes to it and want to be able to > do the same thing that you can do with nullable types. > > If you have int? and you want to change the value you can do: > > int? something; > > something.value = x; > > or > > something = x; > > You don't have to specify the property. > > In my code I do > > something.Data = x. > > Where the property is set as: > > public object Data > { > get { return _objCurrent; } > set > { > if (value == DBNull.Value) value = null; > if (value != null) > { > _ValidateType(value); > } > _objCurrent = value; > _fChanged = true; > } > } > > This handles the data or nulls. > > How can I change this to allow me to call set the variable by either > calling the property or not calling it? > type on the RHS to the type of the LHS. It sounds like you want to be able to assign the value of the Data property of an existing object without specifying the Data property. This isn't possible. Here is what the theoretical implicit operator signature would look like (using string as the basis for a concrete type) :- public static implicit operator StringType(string value) { ... } Note that the return value of the operator method is the type being converted to, but the signature doesn't take any reference to the value that might already exist on the LHS, hence you have no way to modify it. Of course you might be satisfied with this:- public static implicit operator StringType(string value) { return new StringType(value); } This simple returns a new instance of StringType initialised using the value on the RHS. But is that what you are really after? -- Anthony Jones - MVP ASP/ASP.NET
Show quote
Hide quote
"Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message What is RHS and LHS?news:%237CsgviXJHA.4632@TK2MSFTNGP04.phx.gbl... > > "tshad" <t**@dslextreme.com> wrote in message > news:erbqCMhXJHA.4596@TK2MSFTNGP06.phx.gbl... >>I have a class that I have that tracks when an object has changed or is >>set to null. >> >> I use it for data I get from my database to tell me if I need to update >> or reset the data to nulls. I am making changes to it and want to be >> able to do the same thing that you can do with nullable types. >> >> If you have int? and you want to change the value you can do: >> >> int? something; >> >> something.value = x; >> >> or >> >> something = x; >> >> You don't have to specify the property. >> >> In my code I do >> >> something.Data = x. >> >> Where the property is set as: >> >> public object Data >> { >> get { return _objCurrent; } >> set >> { >> if (value == DBNull.Value) value = null; >> if (value != null) >> { >> _ValidateType(value); >> } >> _objCurrent = value; >> _fChanged = true; >> } >> } >> >> This handles the data or nulls. >> >> How can I change this to allow me to call set the variable by either >> calling the property or not calling it? >> > > The feature you are looking for is the implicit operator. It converts the > type on the RHS to the type of the LHS. > It sounds like you want to be able to assign the value of the Data > property of an existing object without specifying the Data property. This > isn't possible. > > Here is what the theoretical implicit operator signature would look like Might work.> (using string as the basis for a concrete type) :- > > public static implicit operator StringType(string value) { ... } > > Note that the return value of the operator method is the type being > converted to, but the signature doesn't take any reference to the value > that might already exist on the LHS, hence you have no way to modify it. > > Of course you might be satisfied with this:- > > public static implicit operator StringType(string value) { return new > StringType(value); } But I am not only concerned with a string type but also a Boolean or Int or decimal etc. Really what I want to have happen is to be able to do: something = "a test"; or BoolType theBool; theBool = true; and have it set: something.Data = "the test" or theBool.Data = true or null if that is passed. That is what int? does. May not be possible, but thought that would be neat to be able to do. > Not sure.> This simple returns a new instance of StringType initialised using the > value on the RHS. But is that what you are really after? Thanks, Tom Show quoteHide quote > > > > > > -- > Anthony Jones - MVP ASP/ASP.NET >
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message Yes I had grasped that. The above is an example of a method you would add news:%23Jj4p%23iXJHA.3808@TK2MSFTNGP05.phx.gbl... > >> >> Of course you might be satisfied with this:- >> >> public static implicit operator StringType(string value) { return new >> StringType(value); } > > Might work. > > But I am not only concerned with a string type but also a Boolean or Int > or decimal etc. > to your 'StringType' class you included in your code. I thought it fairly obvious that you would be able to extrapolate how to add a similar method to your other classes as well. Show quoteHide quote > Really what I want to have happen is to be able to do: No it doesn't. There is a very important difference. int? is short for > > something = "a test"; > > or > > BoolType theBool; > theBool = true; > > and have it set: > > something.Data = "the test" > > or theBool.Data = true > > or null if that is passed. > > That is what int? does. > Nullable<int>, Nullable<int> as with all other specialisations of Nullable<T> are structs, IOW are value types. This code:- int? x; x = 5; Fundemantally this code is :- Nullable<int> x; x = new Nullable<int>(5); Note that the value property of the existing content of x is not being implicitly set, rather a new structure is temporarily created and is content is copies over the current content of x. > May not be possible, but thought that would be neat to be able to do. It is possible to do something similar with a Class, a reference type but it > would be illadvised. >> Consider this:->> This simple returns a new instance of StringType initialised using the >> value on the RHS. But is that what you are really after? > > Not sure. > IntType x = new IntType(1); IntType y = x; x.Data = 2; Both x and y point to the same object hence both x.Data and y.Data would return 2. Now what if you had an implicit conversion operator method added to IntType we could now do this:- x = 3; However x would now point to different instance of IntType than y. y.Data would still return 2 and x.Data would return 3. I really doubt that you would want assignment of this sort to replace the current object being referenced since it would seem you want to track whether the value has changed. -- Anthony Jones - MVP ASP/ASP.NET
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message It sounds like you want to indicate that "Data" is the default property for news:erbqCMhXJHA.4596@TK2MSFTNGP06.phx.gbl... >I have a class that I have that tracks when an object has changed or is set >to null. > > I use it for data I get from my database to tell me if I need to update or > reset the data to nulls. I am making changes to it and want to be able to > do the same thing that you can do with nullable types. > > If you have int? and you want to change the value you can do: > > int? something; > > something.value = x; > > or > > something = x; > > You don't have to specify the property. > > In my code I do > > something.Data = x. > > Where the property is set as: > > public object Data > { > get { return _objCurrent; } > set > { > if (value == DBNull.Value) value = null; > if (value != null) > { > _ValidateType(value); > } > _objCurrent = value; > _fChanged = true; > } > } > > This handles the data or nulls. > > How can I change this to allow me to call set the variable by either > calling the property or not calling it? > > The code itself (with some removed as it is pretty large) is: > > ************************************************** > using System; > using System.IO; > > namespace FtsData > { > abstract class DataType > { > protected object _objInitial; > protected object _objCurrent; > private bool _fChanged; > > public bool IsNull > { > get { return _objCurrent == null; } > } > > public bool IsFirstNull > { > get { return _objInitial == null; } > } > > // Reset _objInitial to _objCurrent and changed flag to false to > track > // when this variable changes again. This would be necessary if > were to > // write out data to a database record and need to track when it > changes again > > public void Reset() > { > _objInitial = _objCurrent; > _fChanged = false; > } > > public object First > { > get { return _objInitial; } > } > > public object Data > { > get { return _objCurrent; } > set > { > if (value == DBNull.Value) value = null; > if (value != null) > { > _ValidateType(value); > } > _objCurrent = value; > _fChanged = true; > } > } > > // Likewise, I don't think Changed should include a setter > public bool Changed > { > get { return _fChanged; } > } > > // This is what deriving classes will define so the type can be > checked > protected abstract Type _TypeRequired { get; } > > private void _ValidateType(object obj) > { > Type typeRequired = _TypeRequired; > > // Depending on how you're using this class, you may instead > prefer > // to check for exact type equality. The below simply > requires that > // the passed-in object has the required type in its > inheritance chain. > > if(obj == DBNull.Value) > { > _fChanged = _fChanged; > } > if (!typeRequired.IsInstanceOfType(obj)) > { > throw new ArgumentException("assigned value type of " + > obj.GetType().Name + " is incompatible with required > type of " + > typeRequired.Name); > } > } > } > > class BoolType : DataType > { > public BoolType() > { > } > > public BoolType(bool initial) > { > > Type type = Type.GetType("initial"); > Console.WriteLine("Type = {0}",type); > _objInitial= initial; > _objCurrent = initial; > } > > // Each class defines this so that the base type can validate the > data's type > protected override Type _TypeRequired > { > get { return typeof(bool); } > } > > // A convenience method so that no casting is needed when you > already have > // a fully typed object > public bool TypedData > { > get { return (bool)Data; } > set { Data = value; } > } > } > > class StringType : DataType > { > public StringType() > { > } > > public StringType(string initial) > { > Type type = initial.GetType(); > Console.WriteLine("Type = {0}",type); > _objInitial= initial; > _objCurrent = initial; > } > > protected override Type _TypeRequired > { > get { return typeof(string); } > } > > public string TypedData > { > get { return (string)Data; } > set { Data = value; } > } > } > } > ************************************************** > > Thanks, > > Tom > your class. This is not available to the best of my knowledge in C# (nor VB either). -- Mike
Show quote
Hide quote
"Family Tree Mike" <F**@ThisOldHouse.com> wrote in message I guess that was what I wanted to do.news:B94DE901-90AF-47DE-82A9-0C9164CCA6C4@microsoft.com... > "tshad" <t**@dslextreme.com> wrote in message > news:erbqCMhXJHA.4596@TK2MSFTNGP06.phx.gbl... >>I have a class that I have that tracks when an object has changed or is >>set to null. >> >> I use it for data I get from my database to tell me if I need to update >> or reset the data to nulls. I am making changes to it and want to be >> able to do the same thing that you can do with nullable types. >> >> If you have int? and you want to change the value you can do: >> >> int? something; >> >> something.value = x; >> >> or >> >> something = x; >> >> You don't have to specify the property. >> >> In my code I do >> >> something.Data = x. >> >> Where the property is set as: >> >> public object Data >> { >> get { return _objCurrent; } >> set >> { >> if (value == DBNull.Value) value = null; >> if (value != null) >> { >> _ValidateType(value); >> } >> _objCurrent = value; >> _fChanged = true; >> } >> } >> >> This handles the data or nulls. >> >> How can I change this to allow me to call set the variable by either >> calling the property or not calling it? >> >> The code itself (with some removed as it is pretty large) is: >> >> ************************************************** >> using System; >> using System.IO; >> >> namespace FtsData >> { >> abstract class DataType >> { >> protected object _objInitial; >> protected object _objCurrent; >> private bool _fChanged; >> >> public bool IsNull >> { >> get { return _objCurrent == null; } >> } >> >> public bool IsFirstNull >> { >> get { return _objInitial == null; } >> } >> >> // Reset _objInitial to _objCurrent and changed flag to false to >> track >> // when this variable changes again. This would be necessary if >> were to >> // write out data to a database record and need to track when it >> changes again >> >> public void Reset() >> { >> _objInitial = _objCurrent; >> _fChanged = false; >> } >> >> public object First >> { >> get { return _objInitial; } >> } >> >> public object Data >> { >> get { return _objCurrent; } >> set >> { >> if (value == DBNull.Value) value = null; >> if (value != null) >> { >> _ValidateType(value); >> } >> _objCurrent = value; >> _fChanged = true; >> } >> } >> >> // Likewise, I don't think Changed should include a setter >> public bool Changed >> { >> get { return _fChanged; } >> } >> >> // This is what deriving classes will define so the type can be >> checked >> protected abstract Type _TypeRequired { get; } >> >> private void _ValidateType(object obj) >> { >> Type typeRequired = _TypeRequired; >> >> // Depending on how you're using this class, you may instead >> prefer >> // to check for exact type equality. The below simply >> requires that >> // the passed-in object has the required type in its >> inheritance chain. >> >> if(obj == DBNull.Value) >> { >> _fChanged = _fChanged; >> } >> if (!typeRequired.IsInstanceOfType(obj)) >> { >> throw new ArgumentException("assigned value type of " + >> obj.GetType().Name + " is incompatible with required >> type of " + >> typeRequired.Name); >> } >> } >> } >> >> class BoolType : DataType >> { >> public BoolType() >> { >> } >> >> public BoolType(bool initial) >> { >> >> Type type = Type.GetType("initial"); >> Console.WriteLine("Type = {0}",type); >> _objInitial= initial; >> _objCurrent = initial; >> } >> >> // Each class defines this so that the base type can validate the >> data's type >> protected override Type _TypeRequired >> { >> get { return typeof(bool); } >> } >> >> // A convenience method so that no casting is needed when you >> already have >> // a fully typed object >> public bool TypedData >> { >> get { return (bool)Data; } >> set { Data = value; } >> } >> } >> >> class StringType : DataType >> { >> public StringType() >> { >> } >> >> public StringType(string initial) >> { >> Type type = initial.GetType(); >> Console.WriteLine("Type = {0}",type); >> _objInitial= initial; >> _objCurrent = initial; >> } >> >> protected override Type _TypeRequired >> { >> get { return typeof(string); } >> } >> >> public string TypedData >> { >> get { return (string)Data; } >> set { Data = value; } >> } >> } >> } >> ************************************************** >> >> Thanks, >> >> Tom >> > > > It sounds like you want to indicate that "Data" is the default property > for your class. This is not available to the best of my knowledge in C# > (nor VB either). > Since you could do that with the int?, I thought you may be able to a similar type thing with another object. Thanks, Tom Show quoteHide quote > -- > Mike >
Other interesting topics
Array - Does it exist?
How to close a StreamWriter class when the program exits Bitmpa.Save & GZipStream - error Compare and Null Value equality linq --> changing a value? Connection string/Sql Server 2005/Windows authentication/ but not on domain Extract Text (string) from doc, xls, pdf ... I will pay for Null dynamic data in .net 3.5 SP1 -- performance, multi-tier |
|||||||||||||||||||||||