作者:VB.NET开发
项目:Syste
Module Example
Public Sub Main()
Dim obj As New Object()
Console.WriteLine(obj.ToString())
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports Examples
Namespace Examples
Public Class Object1
End Class
End Namespace
Module Example
Public Sub Main()
Dim obj1 As New Object1()
Console.WriteLine(obj1.ToString())
End Sub
End Module
作者:VB.NET开发
项目:Syste
Public Class Object2
Private value As Object
Public Sub New(value As Object)
Me.value = value
End Sub
Public Overrides Function ToString() As String
Return MyBase.ToString + ": " + value.ToString()
End Function
End Class
Module Example
Public Sub Main()
Dim obj2 As New Object2("a"c)
Console.WriteLine(obj2.ToString())
End Sub
End Module
作者:VB.NET开发
项目:Syste
Public Class Automobile
Private _doors As Integer
Private _cylinders As String
Private _year As Integer
Private _model As String
Public Sub New(model As String, year As Integer, doors As Integer,
cylinders As String)
_model = model
_year = year
_doors = doors
_cylinders = cylinders
End Sub
Public ReadOnly Property Doors As Integer
Get
Return _doors
End Get
End Property
Public ReadOnly Property Model As String
Get
Return _model
End Get
End Property
Public ReadOnly Property Year As Integer
Get
Return _year
End Get
End Property
Public ReadOnly Property Cylinders As String
Get
Return _cylinders
End Get
End Property
Public Overrides Function ToString() As String
Return ToString("G")
End Function
Public Overloads Function ToString(fmt As String) As String
If String.IsNullOrEmpty(fmt) Then fmt = "G"
Select Case fmt.ToUpperInvariant()
Case "G"
Return String.Format("{0} {1}", _year, _model)
Case "D"
Return String.Format("{0} {1}, {2} dr.",
_year, _model, _doors)
Case "C"
Return String.Format("{0} {1}, {2}",
_year, _model, _cylinders)
Case "A"
Return String.Format("{0} {1}, {2} dr. {3}",
_year, _model, _doors, _cylinders)
Case Else
Dim msg As String = String.Format("'{0}' is an invalid format string",
fmt)
Throw New ArgumentException(msg)
End Select
End Function
End Class
Module Example
Public Sub Main()
Dim auto As New Automobile("Lynx", 2016, 4, "V8")
Console.WriteLine(auto.ToString())
Console.WriteLine(auto.ToString("A"))
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR",
"hr-HR", "ja-JP" }
Dim value As Decimal = 1603.49d
For Each cultureName In cultureNames
Dim culture As New CultureInfo(cultureName)
Console.WriteLine("{0}: {1}", culture.Name,
value.ToString("C2", culture))
Next
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim values() As Integer = { 1, 2, 4, 8, 16, 32, 64, 128 }
Console.WriteLine(values.ToString())
Dim list As New List(Of Integer)(values)
Console.WriteLine(list.ToString())
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Collections.Generic
Public Class CList(Of T) : Inherits List(Of T)
Public Sub New(capacity As Integer)
MyBase.New(capacity)
End Sub
Public Sub New(collection As IEnumerable(Of T))
MyBase.New(collection)
End Sub
Public Sub New()
MyBase.New()
End Sub
Public Overrides Function ToString() As String
Dim retVal As String = String.Empty
For Each item As T In Me
If String.IsNullOrEmpty(retval) Then
retVal += item.ToString()
Else
retval += String.Format(", {0}", item)
End If
Next
Return retVal
End Function
End Class
Module Example
Public Sub Main()
Dim list2 As New CList(Of Integer)
list2.Add(1000)
list2.Add(2000)
Console.WriteLine(list2.ToString())
End Sub
End Module
作者:VB.NET开发
项目:Syste
' 导入命名空间
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices
Public Module StringExtensions
<Extension()>
Public Function ToString2(Of T)(l As List(Of T)) As String
Dim retVal As String = ""
For Each item As T In l
retVal += String.Format("{0}{1}", If(String.IsNullOrEmpty(retVal),
"", ", "),
item)
Next
Return If(String.IsNullOrEmpty(retVal), "{}", "{ " + retVal + " }")
End Function
<Extension()>
Public Function ToString(Of T)(l As List(Of T), fmt As String) As String
Dim retVal As String = String.Empty
For Each item In l
Dim ifmt As IFormattable = TryCast(item, IFormattable)
If ifmt IsNot Nothing Then
retVal += String.Format("{0}{1}",
If(String.IsNullOrEmpty(retval),
"", ", "), ifmt.ToString(fmt, Nothing))
Else
retVal += ToString2(l)
End If
Next
Return If(String.IsNullOrEmpty(retVal), "{}", "{ " + retVal + " }")
End Function
End Module
Module Example
Public Sub Main()
Dim list As New List(Of Integer)
list.Add(1000)
list.Add(2000)
Console.WriteLine(list.ToString2())
Console.WriteLine(list.ToString("N0"))
End Sub
End Module