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