C# Static Class vs VB.NET Module usage -
i'm having difficulty calling function c# class in similar way call function vb.net module.
i have c# class general.cs
using system; namespace xyz.classes { public static class general { //object null empty function public static string nulltoempty(object obj) { var returnstring = ""; if (obj != null) { returnstring = obj.tostring(); } return returnstring; } } }
this type of function can called in vb.net anywhere in project without declaring module or prefixing call - using
dim x string = nulltoempty(obj) - vb.net var x = nulltoempty(obj) - c#
from googling, seems public static class public static methods may able accomplish 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 in intellisense (using resharper) - it's not valid(red), not referenced correctly - i'm guessing...
the point being able use common 'user defined' utility functions null trapping, formatting, etc... - not have wrap kinds of stuff in ugly c# code this:
obj.foofield = dr["foo"] == null ? "" : dr["foo"];
would prefer:
obj.foofield = nulltoempty(dr["foo"]);
this becomes more useful 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 :-)
thanks
my experience has been have few options:
1) embed utility function in base class of other classes inherit from. not practical solution because have classes inherit third party or .net framework classes, difficult modify.
2) use extension methods extend functionality existing base classes. think end being more work it's worth.
3) make simple-named global method holder class (i.e. util) , prefix methods hold name.
we used option 3 convert large vb application c# , worked quite well. although isn't quite convenient vb syntax, once used it, becomes easy work with.
Comments
Post a Comment