Click here to Skip to main content
15,890,995 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Code Project Blocking Add Blockers Pin
ZurdoDev19-Feb-19 8:09
professionalZurdoDev19-Feb-19 8:09 
GeneralRe: Code Project Blocking Add Blockers Pin
Pete O'Hanlon19-Feb-19 7:36
mvePete O'Hanlon19-Feb-19 7:36 
GeneralRe: Code Project Blocking Add Blockers Pin
Mycroft Holmes19-Feb-19 11:27
professionalMycroft Holmes19-Feb-19 11:27 
GeneralRe: Code Project Blocking Add Blockers Pin
Mark_Wallace19-Feb-19 7:50
Mark_Wallace19-Feb-19 7:50 
GeneralMessage Closed Pin
18-Feb-19 14:39
doobiester 18-Feb-19 14:39 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
PIEBALDconsult18-Feb-19 15:12
mvePIEBALDconsult18-Feb-19 15:12 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff18-Feb-19 22:14
professionalBillWoodruff18-Feb-19 22:14 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Richard Deeming25-Feb-19 8:10
mveRichard Deeming25-Feb-19 8:10 
Using static variables to store state between method calls could be problematic. I'd be inclined to replace them with either out parameters or value tuple[^] returns. Eg:
C#
public static class StringExtensions
{
    public static StringState GetStringState(this string str, out double dbl)
    {
        dbl = double.NaN;
        
        if (str is null) return StringState.IsNull;
        if (str.Length == 0) return StringState.IsEmpty;
        if (string.IsNullOrWhiteSpace(str)) return StringState.IsWhiteSpace;
        if (!double.TryParse(str, out var d)) return StringState.IsStandard;
        
        dbl = d;
        return StringState.IsNumeric;
    }
    
    public static StringState GetStringState(this string str) => GetStringState(str, out _);
    
    public static NumberState GetStringNumberState(this string str, out double dbl)
    {
        if (GetStringState(str, out dbl) != StringState.IsNumeric) return NumberState.NotNumber;
        if (Math.Abs(dbl % 1) < double.Epsilon) return NumberState.IsInt;
        return NumberState.IsDouble;
    }
    
    public static NumberState GetStringNumberState(this string str) => GetStringNumberState(str, out _);
    
    public static bool StringIsHex(this string str, out int i)
    {
        if (str.StartsWith("0x")) str = str.Remove(0, 2);
        return int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out i);
    }
    
    public static bool StringIsHex(this string str) => StringIsHex(str, out _);
}
Or:
C#
public static class StringExtensions
{
    public static (StringState state, double dbl) GetStringState(this string str)
    {
        if (str is null) return (StringState.IsNull, double.NaN);
        if (str.Length == 0) return (StringState.IsEmpty, double.NaN);
        if (string.IsNullOrWhiteSpace(str)) return (StringState.IsWhiteSpace, double.NaN);
        if (!double.TryParse(str, out var d)) return (StringState.IsStandard, double.NaN);
        return (StringState.IsNumeric, d);
    }
    
    public static (NumberState state, double dbl) GetStringNumberState(this string str)
    {
        switch (GetStringState(str))
        {
            case var n when n.state != StringState.IsNumeric:
            {
                return (NumberState.NotNumber, double.NaN);
            }
            case var n when Math.Abs(n.dbl % 1) < double.Epsilon:
            {
                return (NumberState.IsInt, n.dbl);
            }
            case var n:
            {
                return (NumberState.IsDouble, n.dbl);
            }
        }
    }
    
    public static (bool isHex, int value) StringIsHex(this string str)
    {
        if (str.StartsWith("0x")) str = str.Remove(0, 2);
        return (int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var i), i);
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff25-Feb-19 20:40
professionalBillWoodruff25-Feb-19 20:40 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Super Lloyd18-Feb-19 16:19
Super Lloyd18-Feb-19 16:19 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
the goat in your machine18-Feb-19 17:15
the goat in your machine18-Feb-19 17:15 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
doobiester 19-Feb-19 2:11
doobiester 19-Feb-19 2:11 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
OriginalGriff18-Feb-19 20:53
mveOriginalGriff18-Feb-19 20:53 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Nelek18-Feb-19 23:51
protectorNelek18-Feb-19 23:51 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
kalberts18-Feb-19 21:02
kalberts18-Feb-19 21:02 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
RickZeeland18-Feb-19 21:49
mveRickZeeland18-Feb-19 21:49 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 0:08
professionalBillWoodruff19-Feb-19 0:08 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
RickZeeland19-Feb-19 1:10
mveRickZeeland19-Feb-19 1:10 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 15:38
professionalBillWoodruff19-Feb-19 15:38 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Eddy Vluggen19-Feb-19 3:11
professionalEddy Vluggen19-Feb-19 3:11 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
  Forogar  19-Feb-19 3:41
professional  Forogar  19-Feb-19 3:41 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 15:35
professionalBillWoodruff19-Feb-19 15:35 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
  Forogar  21-Feb-19 2:32
professional  Forogar  21-Feb-19 2:32 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
#realJSOP19-Feb-19 8:10
mve#realJSOP19-Feb-19 8:10 
GeneralHow to swear in Latin Pin
Brisingr Aerowing18-Feb-19 13:14
professionalBrisingr Aerowing18-Feb-19 13:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.