title: System.Text.Encoding value
body: <p>I'm getting an error with the optional argument, it says "Constant expression is required"</p>
  
  <p>How I can resolve the problem?</p>
  
  <pre><code>Private Function Write_Text_To_File(ByVal Text_File As String, _
                                ByVal Text As String, _
                                ByVal Append_Text As Boolean, _
                                Optional Encoding As System.Text.Encoding = System.Text.Encoding.Default) As Boolean
  
      Try : Using TextFile As New IO.StreamWriter(Text_File, Append_Text, Encoding) : TextFile.WriteLine(Text) : End Using
          Return True
      Catch ex As Exception
          'Return False
          Throw New Exception(ex.Message)
      End Try
  
  End Function
  </code></pre>
  
  <blockquote>
    <p>UPDATE:</p>
  </blockquote>
  
  <p>Solution</p>
  
  <pre><code>#Region " Write Text to File "
  
  ' [ Write Text to File Function ]
  '
  ' // By Elektro H@cker
  '
  ' Examples :
  '
  ' Write_Text_To_File("C:\Test.txt", "Text", False)
  ' Write_Text_To_File("C:\Test.txt", "Text", True, System.Text.Encoding.UTF8)
  
  Private Function Write_Text_To_File(ByVal Text_File As String, _
                                ByVal Text As String, _
                                ByVal Append_Text As Boolean, _
                                Optional Encoding As System.Text.Encoding = Nothing) As Boolean
      Try
          If Encoding Is Nothing Then Encoding = System.Text.Encoding.Default
          Using TextFile As New IO.StreamWriter(Text_File, Append_Text, Encoding) : TextFile.WriteLine(Text) : End Using
          Return True
      Catch ex As Exception
          'Return False
          Throw New Exception(ex.Message)
      End Try
  
  End Function
  
  #End Region
  </code></pre>