Click here to Skip to main content
15,887,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to get a bool value from one class to another class.
I have webform page.

I have a Click void that gets data from SQL.
I then call a class for Luhn validation, and I need to get the answer from that class back to the click void.

This is the luhn code.

Hope someone can point me in the right direction.

What I have tried:

ASP.NET
public static class Personnummer
{
    private static readonly Regex regex;
    private static readonly CultureInfo cultureInfo;

    static Personnummer()
    {
        cultureInfo = new CultureInfo("sv-SE");
        regex = new Regex(@"^(\d{2}){0,1}
        (\d{2})(\d{2})(\d{2})([-|+]{0,1})?(\d{3})(\d{0,1})$");
    }

    /// <summary>
    /// Calculates the checksum value of a given
    /// digit-sequence as string by using the luhn/mod10 algorithm.
    /// </summary>
    /// <param name="value">Sequence of digits
    /// as a string.</param>
    /// <returns>Resulting checksum value.</returns>
    public static int Luhn(string taxIdentifier)
    {
        // Luhm algorithm doubles every other number in the value.
        // To get the correct checksum digit,
        // we ought to append a 0 on the sequence.
        // If the result becomes a two digit number,
        // subtract 9 from the value.
        // If the total sum is not a 0, the last checksum
        // value should be subtracted from 10.
        // The resulting value is the check value that
        // we use as control number.

        // The value passed is a string, so we ought to
        // get the actual integer value from each char
        // (i.e., subtract '0' which is 48).
        int[] t = taxIdentifier.ToCharArray().Select
                  (d => d - 48).ToArray();
        int sum = 0;
        int temp;
        for (int i = t.Length; i-- > 0;)
        {
            temp = t[i];
            sum += (i % 2 == t.Length % 2)
                ? ((temp * 2) % 10) + temp / 5
                : temp;
        }

        return sum % 10;
    }

    /// <summary>
    /// Function to make sure that the passed year,
    /// month and day is parseable to a date.
    /// </summary>
    /// <param name="year">Years as string.</param>
    /// <param name="month">Month as int.</param>
    /// <param name="day">Day as int.</param>
    /// <returns>Result.</returns>
    public static bool TestDate(string year, int month, int day)
    {
        try
        {
            DateTime dt = new DateTime
            (cultureInfo.Calendar.ToFourDigitYear
            (int.Parse(year)), month, day);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// Validate Swedish social security number.
    /// </summary>
    /// <param name="value">Value as string.</param>
    /// <returns>Result.</returns>
    public static bool Valid(string value)
    {
        MatchCollection matches = regex.Matches(value);

        if (matches.Count < 1 || matches[0].Groups.Count < 7)
        {
            return false;
        }

        GroupCollection groups = matches[0].Groups;
        int month, day, check;
        string yStr;
        try
        {
            yStr = (groups[2].Value.Length == 4) ?
                    groups[2].Value.Substring(2) : groups[2].Value;
            month = int.Parse(groups[3].Value);
            day = int.Parse(groups[4].Value);
            check = int.Parse(groups[7].Value);
        }
        catch
        {
            // Could not parse. So invalid.
            return false;
        }

        bool valid = Luhn($"{yStr}{groups[3].Value}
             {groups[4].Value}{groups[6].Value}{check}") == 0;

        return valid && (TestDate(yStr, month, day) ||
                                 TestDate(yStr, month, day - 60));

    //global: string test = bool valid;
    }

    ///// <summary>
    ///// Validate Swedish social security number.
    ///// </summary>
    ///// <param name="value">Value as long.</param>
    ///// <returns>Result.</returns>
    public static bool Valid(long value)
    {
        return Valid(value.ToString());
    }
}
Posted
Updated 6-Sep-23 8:22am
v3
Comments
Richard MacCutchan 6-Sep-23 3:13am    
Instantiate an object of the class, then you can call the method of the class using the object as reference. This is basic OOP, so I don't quite understand what the problem is.
Member 16086150 6-Sep-23 3:15am    
The problem is that im new to this
Could you give an example please
Richard MacCutchan 6-Sep-23 3:18am    
You can find examples in any of the C# tutorials that are freely available.
Richard MacCutchan 6-Sep-23 3:26am    
See my (very simple) example below.

1 solution

C#
class A
{
    public bool ReturnBool(int value)
    {
        if (value > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

class B
{
    public SomeMethod()
    {
        int number = get(); // get a value from somewhere
        A a = new A();
        if a.ReturnBool(number)
        {
            // do something when it is true
        }
    // ... etc.
    }
}

The code running in class B creates an A object and calls the ReturnBool method to check the validity of some data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900