简体   繁体   中英

C# Static Class vs VB.NET Module usage

I'm having difficulty calling a function from a C# Class in a similar way that I call a function from a VB.NET Module.

I have a C# class General.cs

  using System;

  namespace XYZ.Classes
     {
     public static class General
     {
         //Object Null to Empty Function
         public static string NullToEmpty(object obj)
         {
             var returnString = "";
             if (obj != null)
             {
                 returnString = obj.ToString();
             }
             return returnString;
         }

     }
  }

This type of function can be called in VB.NET from anywhere in the project without declaring the module or prefixing the call - just using

  dim x as String = NullToEmpty(obj) - VB.NET

  var x = NullToEmpty(obj) - C#

From my googling, it seems a public static class with public static methods may be able to accomplish this in c#.

C# Example:

class foo.cs

  namespace XYZ.Classes
  {
      public class Foo
      {

          public string doFoo()
          {
               var obj = null;
               var foolish = NullToEmpty(obj);
               return foolish;
          }
       }
   }

The function shows up in intellisense (using ReSharper) - but it's not valid(red), so something is not referenced correctly - I'm just guessing...

The point is being able to simply use common 'User Defined' utility functions for null trapping, formatting, etc... - so as not to have to wrap all kinds of stuff in ugly C# code like this:

  obj.FooField = dr["Foo"] == null ? "" : dr["Foo"];

Would prefer:

  obj.FooField = NullToEmpty(dr["Foo"]);

This becomes even more useful for DateTime applications:

  obj.ActivityStartDate = dr["ActivityStartDate"] == null ? "" : Convert.ToDateTime(dr["ActivityStartDate"]).ToString("yyyy-MM-dd HH:mm:ss");

vs:

  obj.ActivityStartDate = GetDate(dr["ActivityStartDate"]);

Or Integer Conversion:

  cmd.Parameters["@BirthdayDay"].Value = String.IsNullOrEmpty(obj.BirthdayDay) ? 01 : Convert.ToInt32(obj.BirthdayDay);

vs:

  cmd.Parameters["@BirthdayDay"].Value = NullToZero(dr["obj.BirthdayDay"]);

Some C# guru must know this :-)

Thanks

在类中访问静态方法要求您包括所有者类

obj.FooField = General.NullToEmpty(dr["Foo"])

My experience has been that you have a few options:

1) Embed the utility function in a base class that all of your other classes inherit from. This is not really a practical solution because you will probably have classes that inherit from third party or .Net framework classes, which would be very difficult to modify.

2) Use Extension methods to extend your functionality to existing base classes. I think that this would end up being more work than it's worth.

3) Make a very simple-named global method holder class (ie Util) and just prefix your methods with this hold name.

We used option 3 to convert a large VB application to C# and it worked quite well. Although it isn't quite a convenient as the VB syntax, once you get used to it, it becomes very easy to work with.

VB.NET adds methods in a Module to the global (unnamed) namespace. This is mostly a back-compat feature, if the team could have removed module support then they would probably have done so. But they couldn't, it would have made it too difficult to port VB6 code to VB.NET. The practice is iffy and doesn't scale at all, you tend to run into trouble when the project becomes large. A problem know as "global namespace pollution".

Programmers tend to work around it by giving method names a prefix. Which works but is pretty awkward since you have to cough up that prefix every time you want to call the method, there is no analogue of the Imports directive for that. IntelliSense suffers greatly as well.

C# just doesn't permit doing this at all. Closest you could get is with extension methods. But don't, get used to the C# Way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM