from net471 to .NET Standard noob questions [close

2019-09-21 04:45发布

问题:

I have a very old VB project I am looking to modernize to which I created a new solution (will convert code to C# later on), am re-adding the libraries and web projects to the new solution to eliminate the old project having old .publishproj, references to mscorlib 2.0 (despite best efforts to resolve through re-adding references) and several other issues that will likely go away.

In the process, I figured to try to go to .NET Standard for the standardized PCL that will allow for future use with Xamarin, Mono, etc. I am not fully versed in .NET Standard so need some input (attempting 2.0 based on the scaling effect of 2.0 down from what I read)

The problems I am running into are:

1) I have several basic CORE functions from the .NET Framework that are not recognized in .NET Standard:

IsNumeric, IsNothing, IsDBNull, IIF

Any suggestions as to why this is?

(re-edit to remove Hold) Thank you to jmcilhinney for answering :-)

回答1:

All four of IsNumeric, IsNothing, IsDBNull and IIf are VB-specific. They can't be part of .NET Standard if they've never been accessible to other languages without referencing the Microsoft.VisualBasic assembly. You really shouldn't have been using any of them previously anyway as they are holdovers from VB6.

In the case of IsNumeric, it uses Double.TryParse internally anyway. In fact, Double.TryParse was written specifically to improve the performance of IsNumeric. You should be using the TryParse method of the appropriate numeric type yourself if you want to know whether a String can be converted to that type.

In the case of IsNothing, you should simply be comparing your reference to Nothing, e.g. instead of:

If IsNothing(myThing) Then

you should be using:

If myThing Is Nothing then

In the case of IsDBNull, you should be doing much as above, e.g. instead of:

If IsDBNull(myThing) Then 

you should be using:

If myThing Is DBNull.Value Then

That said, both a DataRow and a data reader have their own dedicated methods to tell you whether one of their fields is NULL.

In the case of IIf, it always had it's issues because it is a method that people tried to treat like an operator in many cases. I think it was VB 2008 that actually did introduce an If operator that works much like the C# ternary operator, so you should have been using that since then anyway, e.g. instead of:

myVar = IIf(someBooleanExpression, someValue, someOtherValue)

you should have been using:

myVar = If(someBooleanExpression, someValue, someOtherValue)

There are some subtle differences between IIf and If but I'll leave you to read about how If works for yourself.