次の方法で共有


UInt16.Parse メソッド (String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の書式による数値の文字列形式を、それと等価な 16 ビット符号なし整数に変換します。

UInt16 型は CLS との互換性がありません。CLS との互換性が必要な場合は、代わりに Int32 を使用してください。Int16 は、元の値が UInt16.MaxValue の半分以下の場合に使用されます。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。

<CLSCompliant(False)>
Overloads Public Shared Function Parse( _   ByVal s As String, _   ByVal style As NumberStyles, _   ByVal provider As IFormatProvider _) As UInt16
[C#]
[CLSCompliant(false)]
public static ushort Parse(strings,NumberStylesstyle,IFormatProviderprovider);
[C++]
[CLSCompliant(false)]
public: static unsigned short Parse(String* s,NumberStylesstyle,IFormatProvider* provider);
[JScript]
public
   CLSCompliant(false)
static function Parse(s : String,style : NumberStyles,provider : IFormatProvider) : UInt16;

パラメータ

  • s
    変換する数値を表す文字列。
  • style
    s の許容形式を示す、1 つ以上の NumberStyles 定数の組み合わせ。
  • provider
    s に関するカルチャに固有の書式情報を提供する IFormatProvider

戻り値

s で指定した数値と等しい 16 ビット符号なし整数。

例外

例外の種類 条件
ArgumentNullException s が null 参照 (Visual Basic では Nothing) です。
ArgumentException style が、 NumberStyles 列挙定数の有効な組み合わせではありません。
FormatException s の書式が、 style に準拠した書式ではありません。
OverflowException sMinValue 未満の数値か、 MaxValue より大きい数値を表しています。

解説

s パラメータには、次の書式の数値を指定します。

[ws][sign]digits[ws]

角かっこ ('[' および ']') で囲まれている項目は省略可能です。その他の項目は次のとおりです。

  • ws
    省略可能な空白。
  • sign
    省略可能な正の符号。
  • digits
    0 から 9 までの一連の数字。

style パラメータには、ビットごとの OR 演算を使用して、1 つ以上の NumberStyles 列挙定数を組み合わせて指定できます。 Any および AllowDecimalPoint は、このメソッドから返される型として無効であるため除きます。

provider パラメータは、 NumberFormatInfo を取得する IFormatProvider です。 NumberFormatInfo は、 s の書式に関するカルチャ固有の情報を提供します。 provider が null 参照 (Visual Basic では Nothing) の場合は、現在のカルチャの NumberFormatInfo が使用されます。

使用例

[C#, C++, JScript] Parse メソッドの例を次に示します。

 
/// <summary>
/// Temperature class stores the value as UInt16
/// and delegates most of the functionality 
/// to the UInt16 implementation.
/// </summary>
public class Temperature : IComparable, IFormattable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public int CompareTo(object obj) {
        if(obj is Temperature) {
            Temperature temp = (Temperature) obj;

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    /// <summary>
    /// IFormattable.ToString implementation.
    /// </summary>
    public string ToString(string format, IFormatProvider provider) {
        if( format != null && format.Equals("F") ) {
            return String.Format("{0}'F", this.Value.ToString());
        }

        return m_value.ToString(format, provider);
    }

    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static Temperature Parse(string s, NumberStyles styles, IFormatProvider provider) {
        Temperature temp = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = UInt16.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else {
            temp.Value = UInt16.Parse(s, styles, provider);
        }

        return temp;
    }

    // The value holder
    protected ushort m_value;

    public ushort Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }
}

[C++] 
/// <summary>
/// Temperature class stores the value as UInt16
/// and delegates most of the functionality
/// to the UInt16 implementation.
/// </summary>

public __gc class Temperature : public IComparable, public IFormattable
{
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
protected:
    short m_value;

public:
    Int32 CompareTo(Object* obj) {
        if(obj->GetType() == __typeof(Temperature)) {
            Temperature* temp = dynamic_cast<Temperature*>(obj);
            return m_value.CompareTo(__box(temp->m_value));
        }

        throw new ArgumentException("object is not a Temperature");
    };

    /// <summary>
    /// IFormattable.ToString implementation.
    /// </summary>

    String* ToString(String* format, IFormatProvider* provider) {
        if( format != NULL ) {
            if( format->Equals("F") ) {
                return String::Format("{0}'F", this->Value.ToString());
            }
            if( format->Equals("C") ) {
                return String::Format("{0}'C", this->Celsius.ToString());
            }
        }
        return m_value.ToString(format, provider);
    };

    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>

    static Temperature* Parse(String* s, NumberStyles styles, IFormatProvider* provider) {
        Temperature* temp = new Temperature();

        if( s->EndsWith("F") ) {
            temp->Value = UInt16::Parse( s->Remove(s->LastIndexOf('\''), 2), styles, provider);
        }
        else {
            if( s->EndsWith("C") ) {
                temp->Celsius = UInt16::Parse( s->Remove(s->LastIndexOf('\''), 2), styles, provider);
            }
            else {
                temp->Value = UInt16::Parse(s, styles, provider);
            }
        }
        return temp;
    };

        // The value holder


    __property short get_Value() {
        return m_value;
    };


    __property void set_Value(short value) {
        m_value = value;
    };

    __property short get_Celsius() {
        return (short)((m_value-32)/2);
    };

    __property void set_Celsius( short value) {
        m_value = (short)(value*2+32);
    };

    __property static short get_MinValue(){
        return UInt16::MinValue;
    };

    __property static short get_MaxValue() {
        return UInt16::MaxValue;
    };
};

[JScript] 
/// <summary>
/// Temperature class stores the value as UInt16
/// and delegates most of the functionality 
/// to the UInt16 implementation.
/// </summary>
public class Temperature implements IComparable, IFormattable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public function CompareTo(obj) : int {
        if(obj.GetType() ==Temperature) {
            var temp : Temperature = Temperature(obj);

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    /// <summary>
    /// IFormattable.ToString implementation.
    /// </summary>
    public function ToString(format : String, provider : IFormatProvider) : String {
        if( format != null && format.Equals("F") ) {
            return String.Format("{0}'F", m_value.ToString());
        }

        return m_value.ToString(format, provider);
    }

    /// <summary>
    /// Parses the temperature from a string in form
    /// [ws][sign]digits['F|'C][ws]
    /// </summary>
    public static function Parse(s : String, styles : NumberStyles, provider : IFormatProvider) : Temperature {
        var temp : Temperature = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = UInt16.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
        }
        else {
            temp.Value = UInt16.Parse(s, styles, provider);
        }

        return temp;
    }

    // The value holder
    protected var m_value : ushort;

    public function get Value() : ushort {
        return m_value;
            }
            
            public function set Value(value : ushort) {                        
        m_value = value;
    }
}

[Visual Basic] Visual Basic のサンプルはありません。C#、C++、および JScript のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

UInt16 構造体 | UInt16 メンバ | System 名前空間 | UInt16.Parse オーバーロードの一覧 | 書式設定の概要 | ToString