Wątek przeniesiony 2016-06-20 14:00 z C# i .NET przez ŁF.

Prośba o sprawdzenie kodu.

0
using System;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Globalization;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize = 8000,
     IsByteOrdered = true, ValidationMethodName = "ValidatePesel")]

public struct Pesel : INullable, IBinarySerialize
{
    private bool is_Null;
    private string _s;
    public bool IsNull
    {
        get
        {
            return (is_Null);
        }
    }

    public static Pesel Null
    {
        get
        {
            Pesel p = new Pesel();
            p.is_Null = true;
            return p;
        }
    }

    // Use StringBuilder to provide string representation of UDT.
    public override string ToString()
    {
        if (this.IsNull)
            return "NULL";
        else
        {
            //Pesel p = new Pesel();
            return s.ToString();
        }
    }

    [SqlMethod(OnNullCall = false)]
    public static Pesel Parse(SqlString ss)
    {
        if (ss.IsNull)
        {
            return Null;
        }

        Pesel p = new Pesel();
        p.s = (string)ss;
        return p;
    }

    public string s
    {
        get
        {
            return this._s;
        }
        set
        {
            string temp = _s;
            _s = value;
            if (!ValidatePesel())
            {
                _s = temp;
                throw new ArgumentException("Invalid X coordinate value.");
            }
        }
    }

    public void Read(BinaryReader r)
    {
        _s = r.ReadString();
    }

    public void Write(BinaryWriter w)
    {
        string ss = _s.ToString();
        w.Write(ss);
    }

    public bool ValidatePesel()
    {
        int[] weights = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
        bool result = false;
        if (_s.Length == 11)
        {
            int controlSum = CalculateControlSum(_s.ToString(), weights);
            int controlNum = controlSum % 10;
            controlNum = 10 - controlNum;
            if (controlNum == 10)
            {
                controlNum = 0;
            }
            int lastDigit = int.Parse(_s[_s.Length - 1].ToString());
            result = controlNum == lastDigit;
        }
        return result;
    }

    public static int CalculateControlSum(string input, int[] weights, int offset = 0)
    {
        int controlSum = 0;
        for (int i = 0; i < input.Length - 1; i++)
        {
            controlSum += weights[i + offset] * int.Parse(input[i].ToString());
        }
        return controlSum;
    }

    public static string BirthDate()
    {
        if (ValidatePesel())
        {
            string birth = new SqlString("Dzien: " + _s.Substring(4, 2) + "Miesiąc: " + _s.Substring(2, 2) + "Rok: "_s.Substring(0, 2));
            return birth;
        }
        return null;

    }

}

Kod wyrzuca błąd w Visual Studio 2015. Proszę o pomoc.

0

Kod wyrzuca błąd w Visual Studio 2015

To napisz jaki konkretnie błąd wyrzuca. I wrzuć kod w znaczniki < code >

0
using System;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Globalization;

[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize = 8000,
     IsByteOrdered = true, ValidationMethodName = "ValidatePesel")]

public struct Pesel : INullable, IBinarySerialize
{
    private bool is_Null;
    private string _s;
    public bool IsNull
    {
        get
        {
            return (is_Null);
        }
    }

    public static Pesel Null
    {
        get
        {
            Pesel p = new Pesel();
            p.is_Null = true;
            return p;
        }
    }

    // Use StringBuilder to provide string representation of UDT.
    public override string ToString()
    {
        if (this.IsNull)
            return "NULL";
        else
        {
            //Pesel p = new Pesel();
            return s.ToString();
        }
    }

    [SqlMethod(OnNullCall = false)]
    public static Pesel Parse(SqlString ss)
    {
        if (ss.IsNull)
        {
            return Null;
        }

        Pesel p = new Pesel();
        p.s = (string)ss;
        return p;
    }

    public string s
    {
        get
        {
            return this._s;
        }
        set
        {
            string temp = _s;
            _s = value;
            if (!ValidatePesel())
            {
                _s = temp;
                throw new ArgumentException("Invalid X coordinate value.");
            }
        }
    }

    public void Read(BinaryReader r)
    {
        _s = r.ReadString();
    }

    public void Write(BinaryWriter w)
    {
        string ss = _s.ToString();
        w.Write(ss);
    }

    public bool ValidatePesel()
    {
        int[] weights = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
        bool result = false;
        if (_s.Length == 11)
        {
            int controlSum = CalculateControlSum(_s.ToString(), weights);
            int controlNum = controlSum % 10;
            controlNum = 10 - controlNum;
            if (controlNum == 10)
            {
                controlNum = 0;
            }
            int lastDigit = int.Parse(_s[_s.Length - 1].ToString());
            result = controlNum == lastDigit;
        }
        return result;
    }

    public static int CalculateControlSum(string input, int[] weights, int offset = 0)
    {
        int controlSum = 0;
        for (int i = 0; i < input.Length - 1; i++)
        {
            controlSum += weights[i + offset] * int.Parse(input[i].ToString());
        }
        return controlSum;
    }

    public static string BirthDate()
    {
        if (ValidatePesel())
        {
            string birth = new SqlString("Dzien: " + _s.Substring(4, 2) + "Miesiąc: " + _s.Substring(2, 2) + "Rok: "_s.Substring(0, 2));
            return birth;
        }
        return null;

    }

} 

Nie wiem czemu wyświetla błąd, wydaje mi się że wszystko zrobiłam OK.

Błędy:
Error CS1003 Syntax error, ',' expected Projekt 123 Active

Error CS0120 An object reference is required for the non-static field, method, or property 'Pesel._s' Projekt 123 Active

Error CS0120 An object reference is required for the non-static field, method, or property 'Pesel._s' Projekt 123 Active

Error CS0120 An object reference is required for the non-static field, method, or property 'Pesel.ValidatePesel()' Projekt 121 Active

Error CS0120 An object reference is required for the non-static field, method, or property 'Pesel._s' Projekt 123 Active

0

Lista zawiera przecież informację, gdzie są błędy i na czym polegają. Sprawdź, co jest w pliku Class1.cs w liniach 121-123 i popraw.

1

Nie wiem czemu wyświetla błąd, wydaje mi się że wszystko zrobiłam OK.

To prawda, wydaje Ci się.

Wszystkie błędy masz w metodzie BirthDate().

Pierwszy błąd - brak + przed ostatnim Substring.
Reszta - próbujesz odwołać się do niestatycznych pól/metod w metodzie statycznej. Dlaczego w ogóle metoda BirthDate() jest statyczna?

No i dobrze by było jakbyś zapoznała się z konwencją nazewnictwa w C#, automatycznymi właściwościami, inicjalizatorem obiektów i nie zaszkodzi expression-bodied methods/properties.

To:

private bool is_Null;
public bool IsNull
{
    get
    {
        return (is_Null);
    }
}
 

Można uprościć do:

public bool IsNull {get; private set;} 

To:

public static Pesel Null
{
    get
    {
        Pesel p = new Pesel();
        p.is_Null = true;
        return p;
    }
} 

Można tak:

public static Pesel Null => 
    new  Pesel { IsNull = true }; 
0

Cienki ten kod jak fujarka komara połączona z trąbą słonia - część kodu wygląda całkiem nieźle, część jakby stażysta chciał coś dopisać, ale nie wiedział co ani jak. Co to za nazwy - s, _s, is_Null, Null (WTF?!), oczy bolą. ValidatePesel -> IsPeselValidate, BirthDate albo do gettera, albo -> GetBirthDate, BTW nie masz uwzględnionych peseli z nowego tysiąclecia. Static przy BirthDate jest chyba literówką? W każdym razie jest to powód błędu i jednocześnie sugestia, że nie rozumiesz, co robisz.

Na przyszłość - błędy kompilacji mają treść i numer linijki, jedno i drugie ZAWSZE wystarcza do poprawienia błędu. Błędy run-time (czyli wyjątki) też mają numer linijki i treść, a dodatkowo także stos wywołań (często z numerami linijek) i czasem inne dodatkowe dane, które zwykle wystarczają do zlokalizowania i usunięcia błędu. Wystarczy czytać ze zrozumieniem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1