Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Static Class vs VB.NET Module usage
    primarykey
    data
    text
    <p>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.</p> <p>I have a C# class General.cs</p> <pre><code> 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; } } } </code></pre> <p>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 </p> <pre><code> dim x as String = NullToEmpty(obj) - VB.NET var x = NullToEmpty(obj) - C# </code></pre> <p>From my googling, it seems a public static class with public static methods may be able to accomplish this in c#.</p> <p>C# Example: </p> <p>class foo.cs</p> <pre><code> namespace XYZ.Classes { public class Foo { public string doFoo() { var obj = null; var foolish = NullToEmpty(obj); return foolish; } } } </code></pre> <p>The function shows up in intellisense (using ReSharper) - but it's not valid(red), so something is not referenced correctly - I'm just guessing...</p> <p>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:</p> <pre><code> obj.FooField = dr["Foo"] == null ? "" : dr["Foo"]; </code></pre> <p>Would prefer:</p> <pre><code> obj.FooField = NullToEmpty(dr["Foo"]); </code></pre> <p>This becomes even more useful for DateTime applications:</p> <pre><code> obj.ActivityStartDate = dr["ActivityStartDate"] == null ? "" : Convert.ToDateTime(dr["ActivityStartDate"]).ToString("yyyy-MM-dd HH:mm:ss"); </code></pre> <p>vs:</p> <pre><code> obj.ActivityStartDate = GetDate(dr["ActivityStartDate"]); </code></pre> <p>Or Integer Conversion:</p> <pre><code> cmd.Parameters["@BirthdayDay"].Value = String.IsNullOrEmpty(obj.BirthdayDay) ? 01 : Convert.ToInt32(obj.BirthdayDay); </code></pre> <p>vs:</p> <pre><code> cmd.Parameters["@BirthdayDay"].Value = NullToZero(dr["obj.BirthdayDay"]); </code></pre> <p>Some C# guru must know this :-)</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload