Udostępnij za pośrednictwem


RegionInfo Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the RegionInfo class for a particular country/region based on its full culture name.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub New ( _
    name As String _
)
[SecuritySafeCriticalAttribute]
public RegionInfo(
    string name
)

Parameters

  • name
    Type: System.String
    A string containing the full culture name for a specific culture.

Exceptions

Exception Condition
ArgumentNullException

name is nulla null reference (Nothing in Visual Basic).

ArgumentException

name is a country/region name.

-or-

name is the name of a neutral culture.

-or-

name is not a valid culture name.

Remarks

The specified name must be the name of a specific culture. It cannot be the name of a neutral culture. For example, en-US or en-ZW are valid strings to pass to the name parameter; en is not. Case is not significant. The predefined culture names are listed in the CultureInfo class topic.

Examples

The following code example compares two instances of RegionInfo that were created differently.

Imports System.Globalization


Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Creates a RegionInfo using the es-US culture name.
      Dim myRI1 As New RegionInfo("es-US")

      ' Creates a RegionInfo using a CultureInfo name.
      Dim myRI2 As New RegionInfo(New CultureInfo("en-US").Name)

      ' Compares the two instances.
      If myRI1.Equals(myRI2) Then
         outputBlock.Text &= "The two RegionInfo instances are equal." & vbCrLf
      Else
         outputBlock.Text &= "The two RegionInfo instances are NOT equal." & vbCrLf
      End If
   End Sub
End Class 
' This example produces the following output.
'       The two RegionInfo instances are equal.
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Creates a RegionInfo using the es-US culture name.
      RegionInfo myRI1 = new RegionInfo("es-US");

      // Creates a RegionInfo using a CultureInfo Name property
      RegionInfo myRI2 = new RegionInfo(new CultureInfo("en-US").Name);

      // Compares the two instances.
      if (myRI1.Equals(myRI2))
         outputBlock.Text += "The two RegionInfo instances are equal." + "\n";
      else
         outputBlock.Text += "The two RegionInfo instances are NOT equal." + "\n";
   }
}
/*
This code produces the following output.
   The two RegionInfo instances are equal.
*/

The following code example creates instances of RegionInfo using culture names.

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Creates an array containing culture names.
      Dim myCultures() As String = {"", "ar", "ar-DZ", "en", "en-US"}

      ' Creates a RegionInfo for each of the culture names.
      '    Note that "ar" is the culture name for the neutral culture "Arabic",
      '    but it is also the region name for the country/region "Argentina";
      '    therefore, it does not fail as expected.
      outputBlock.Text &= "Without checks..." & vbCrLf
      For Each cul As String In myCultures
         Try
            Dim myRI As New RegionInfo(cul)
         Catch e As ArgumentException
            outputBlock.Text &= e.ToString() & vbCrLf
         End Try
      Next cul

      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "Checking the culture names first..." & vbCrLf
      For Each cul In myCultures
         If cul = "" Then
            outputBlock.Text &= "The culture is the invariant culture." & vbCrLf
         Else
            Dim myCI As New CultureInfo(cul)
            If myCI.IsNeutralCulture Then
               outputBlock.Text += String.Format("The culture '{0}' is a neutral culture.", cul) & vbCrLf
            Else
               outputBlock.Text += String.Format("The culture '{0}' is a specific culture.", cul) & vbCrLf
               Try
                  Dim myRI As New RegionInfo(cul)
               Catch e As ArgumentException
                  outputBlock.Text &= e.ToString() & vbCrLf
               End Try
            End If
         End If
      Next 
   End Sub  
End Class 
' This example produces the following output.
'    Without checks...
'    System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required.
'    Parameter name: name
'       at System.Globalization.RegionInfo..ctor(String name)
'       at SilverlightApplication.Example.Demo(TextBlock outputBlock)
'    System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required.
'    Parameter name: name
'       at System.Globalization.RegionInfo..ctor(String name)
'       at Silverlight.Application.Example.Demo(TextBlock outputBlock)
'    
'    Checking the culture names first...
'    The culture '' is the invariant culture.
'    The culture 'ar' is a neutral culture.
'    The culture 'ar-DZ' is a specific culture.
'    The culture 'en' is a neutral culture.
'    The culture 'en-US' is a specific culture.
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Creates an array containing culture names.
      String[] myCultures = new String[] { "", "ar", "ar-DZ", "en", "en-US" };

      // Creates a RegionInfo for each of the culture names.
      //    Note that "ar" is the culture name for the neutral culture "Arabic",
      //    but it is also the region name for the country/region "Argentina";
      //    therefore, it does not fail as expected.
      outputBlock.Text += "Without checks..." + "\n";
      foreach (String cul in myCultures)
      {
         try
         {
            RegionInfo myRI = new RegionInfo(cul);
         }
         catch (ArgumentException e)
         {
            outputBlock.Text += e.ToString() + "\n";
         }
      }

      outputBlock.Text += "\n";

      outputBlock.Text += "Checking the culture names first..." + "\n";
      foreach (String cul in myCultures)
      {
         if (cul == "")
         {
            outputBlock.Text += "The culture is the invariant culture." + "\n";
         }
         else
         {
            CultureInfo myCI = new CultureInfo(cul);
            if (myCI.IsNeutralCulture)
               outputBlock.Text += String.Format("The culture '{0}' is a neutral culture.", cul) + "\n";
            else
            {
               outputBlock.Text += String.Format("The culture '{0}' is a specific culture.", cul) + "\n";
               try
               {
                  RegionInfo myRI = new RegionInfo(cul);
               }
               catch (ArgumentException e)
               {
                  outputBlock.Text += e.ToString() + "\n";
               }
            }
         }
      }
   }
}
/*
This example produces the following output.
   Without checks...
   System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required.
   Parameter name: name
      at System.Globalization.RegionInfo..ctor(String name)
      at Example.Demo(TextBlock outputBlock)
   System.ArgumentException: The region name ar should not correspond to neutral culture; a specific culture is required.
   Parameter name: name
      at System.Globalization.RegionInfo..ctor(String name)
      at Example.Demo(TextBlock outputBlock)

   Checking the culture names first...
   The culture '' is the invariant culture.
   The culture 'ar' is a neutral culture.
   The culture 'ar-DZ' is a specific culture.
   The culture 'en' is a neutral culture.
   The culture 'en-US' is a specific culture.
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.