Hi guys! Just wanted to let you all know that I created a Variant structure, inspired by the old VB6 days. This is written in C#, but you can build a CLR/.NET class library assembly and reference it to your liking. '' Here is an example of use: '' start with a string of "1"Dim da As Variant = new Variant("1") '' add an integer of 2, becomes 3da = da + 2 '' add a float of 1.3, becomes 4.3da = da + 1.3F '' add a string, becomes "4.3wow"da = da + "wow" '' write "4.3wow"Console.WriteLine(da)I might actually use this puppy. :) Anyway, here it is, source code and all, take a look and be sure to give it 5 stars if you find it useful. http://www.planet-source-code.com/vb...2854&lngWId=10 Regards,Jon Davis 解决方案 Jon: You obviously put a lot of work into the code - but between an evil and ideaand cross posting - you''re just asking for flames ;-) --W.G. Ryan MVP (Windows Embedded) TiBA Solutions www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com"Jon Davis" <jo*@REMOVE.ME.jondavis.net> wrote in messagenews:eb**************@TK2MSFTNGP10.phx.gbl...Hi guys! Just wanted to let you all know that I created a Variant structure, inspiredby the old VB6 days. This is written in C#, but you can build a CLR/.NETclass library assembly and reference it to your liking. '' Here is an example of use: '' start with a string of "1"Dim da As Variant = new Variant("1") '' add an integer of 2, becomes 3da = da + 2 '' add a float of 1.3, becomes 4.3da = da + 1.3F '' add a string, becomes "4.3wow"da = da + "wow" '' write "4.3wow"Console.WriteLine(da)I might actually use this puppy. :) Anyway, here it is, source code and all, take a look and be sure to give it5 stars if you find it useful. http://www.planet-source-code.com/vb...2854&lngWId=10Regards,Jon DavisJon,Check out VB.NET 2005, as it adds support for the Overloaded Operators toVB.NET. http://lab.msdn.microsoft.com/vs2005/ VB.NET 2002 & 2003 do not support Overloaded Operators so your code doesn''twork per se, unless you call op_Addition, you may want to add "operator"methods similar to what System.Decimal has, such as Decimal.Add in additionto the overloaded operators. I normally implement overloaded operators interms of the "operator" methods. You do know that you can use System.Convert.ChangeType to convert an objectfrom one type to another based on a System.Type variable? In fact you maywant your structure to implement System.IConvertible, and make use of theSystem.Convert class to convert types for you... Also I would seriously consider implementing a Strategy Pattern instead ofall the If statements in each method. (note Convert.ChangeType will reduce anumber of if statements, the Strategy should element almost all the rest). Otherwise a very impressive piece of work! Hope this helpsJay "Jon Davis" <jo*@REMOVE.ME.jondavis.net> wrote in messagenews:eb**************@TK2MSFTNGP10.phx.gbl...Hi guys! Just wanted to let you all know that I created a Variant structure, inspiredby the old VB6 days. This is written in C#, but you can build a CLR/.NETclass library assembly and reference it to your liking. '' Here is an example of use: '' start with a string of "1"Dim da As Variant = new Variant("1") '' add an integer of 2, becomes 3da = da + 2 '' add a float of 1.3, becomes 4.3da = da + 1.3F '' add a string, becomes "4.3wow"da = da + "wow" '' write "4.3wow"Console.WriteLine(da)I might actually use this puppy. :) Anyway, here it is, source code and all, take a look and be sure to give it5 stars if you find it useful. http://www.planet-source-code.com/vb...2854&lngWId=10Regards,Jon DavisThanks for the feedback, Jay! I''ll look into modifying ConvertTo() to makethe call you suggest. I''ll also look at IConvertable. As for all the Ifstatements, you might be right about a Strategy Pattern; consider, though,that it was previously a select...case situation, comparing the string valueof the type''s Namespace + "." + its Name properties! Yikes... hehe. :) Thanks again,Jon"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in messagenews:#1**************@TK2MSFTNGP10.phx.gbl... Jon, Check out VB.NET 2005, as it adds support for the Overloaded Operators to VB.NET. http://lab.msdn.microsoft.com/vs2005/ VB.NET 2002 & 2003 do not support Overloaded Operators so your codedoesn''t work per se, unless you call op_Addition, you may want to add "operator" methods similar to what System.Decimal has, such as Decimal.Add inaddition to the overloaded operators. I normally implement overloaded operators in terms of the "operator" methods. You do know that you can use System.Convert.ChangeType to convert anobject from one type to another based on a System.Type variable? In fact you may want your structure to implement System.IConvertible, and make use of the System.Convert class to convert types for you... Also I would seriously consider implementing a Strategy Pattern instead of all the If statements in each method. (note Convert.ChangeType will reducea number of if statements, the Strategy should element almost all the rest). Otherwise a very impressive piece of work! Hope this helps Jay "Jon Davis" <jo*@REMOVE.ME.jondavis.net> wrote in message news:eb**************@TK2MSFTNGP10.phx.gbl... Hi guys! Just wanted to let you all know that I created a Variant structure,inspired by the old VB6 days. This is written in C#, but you can build a CLR/.NET class library assembly and reference it to your liking. '' Here is an example of use: '' start with a string of "1" Dim da As Variant = new Variant("1") '' add an integer of 2, becomes 3 da = da + 2 '' add a float of 1.3, becomes 4.3 da = da + 1.3F '' add a string, becomes "4.3wow" da = da + "wow" '' write "4.3wow" Console.WriteLine(da) I might actually use this puppy. :) Anyway, here it is, source code and all, take a look and be sure to giveit 5 stars if you find it useful. http://www.planet-source-code.com/vb...2854&lngWId=10 Regards, Jon Davis 这篇关于Variant又回来了!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 06:46