本文整理汇总了VB.NET中System.Object.ToString方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Object.ToString方法的具体用法?VB.NET Object.ToString怎么用?VB.NET Object.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Object.ToString方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Module Example
Public Sub Main()
Dim obj As New Object()
Console.WriteLine(obj.ToString())
End Sub
End Module
输出:
System.Object
示例2: Example
' 导入命名空间
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
输出:
Examples.Object1
示例3: Object2
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
输出:
Object2: a
示例4: Automobile
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
输出:
2016 Lynx 2016 Lynx, 4 dr. V8
示例5: Example
' 导入命名空间
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
输出:
en-US: $1,603.49 en-GB: £1,603.49 fr-FR: 1 603,49 € hr-HR: 1.603,49 kn ja-JP: ¥1,603.49
示例6: Example
' 导入命名空间
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
输出:
System.Int32[] System.Collections.Generic.List`1[System.Int32]
示例7: Example
' 导入命名空间
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
输出:
1000, 2000
示例8: StringExtensions
' 导入命名空间
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
输出:
{ 1000, 2000 } { 1,000, 2,000 }