现在要用vb.net,不知道里面有没有C#中的装箱出箱概念啊?vb.net版面没有搜到啊。
如果没有,是不是有什么其他类似功能啊?dim a as new myClass
dim b as new myClass
dim o as object
o = a
b = o
这样做能够把a的值传递给b吗
如果没有,是不是有什么其他类似功能啊?dim a as new myClass
dim b as new myClass
dim o as object
o = a
b = o
这样做能够把a的值传递给b吗
拆箱是引用型->數值型;
裝箱/拆箱是.NET框架的概念,不不是C#或是VB.NET哪一個具本語言的概念.
例子應該是:
Dim a as int32
Dim b as int32
Dim tmp as object
Tmp=a ‘裝箱
B=Tmp ‘拆箱
b = (int)tmp 这样的用法我担心的是,vb.net不用强制类型转换的话,会不会造成丢失数据或不识别的现象?
我还真不知道vb.net强制类型转换怎么实现,能告诉一下吗?
-----------------------------------------------------------
Public Class BoxSample
Inherits System.Windows.Forms.Form#Region " Windows フォーム デザイナで生成されたコード " Public Sub New()
MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub ' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更してください。
' コード エディタを使って変更しないでください。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(48, 64)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(176, 19)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(48, 136)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(176, 19)
Me.TextBox2.TabIndex = 1
Me.TextBox2.Text = "TextBox2"
'
'BoxSample
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Name = "BoxSample"
Me.Text = "BoxSample"
Me.ResumeLayout(False) End Sub#End Region Private Sub BoxSample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a As New myClass1
Dim b As New myClass1 Dim o As Object
o = a
b = o TextBox1.Text = a.intCount
TextBox2.Text = b.intCount End Sub
End ClassPublic Class myClass1 Private m_intCount As Integer Sub New()
m_intCount = 1
End Sub Public ReadOnly Property intCount() As Integer
Get
intCount = m_intCount
End Get
End PropertyEnd Class
把这个ctype一下,也可以。
b = (int)tmp 这样的用法我担心的是,vb.net不用强制类型转换的话,会不会造成丢失数据或不识别的现象?
我还真不知道vb.net强制类型转换怎么实现,能告诉一下吗?
b = (int)tmp 这样的用法(b,tmp你都沒說分別是什麼類型,就不好分析了).
另外: 强制类型转换的話,C#,VB.NET都是一樣的,都可是可轉換才行(不過可能會造成數據精度有少,比如雙精度轉單精度.)
vb.net不用强制类型转换的话,会不会造成丢失数据或不识别的现象?
除了精度上可能會有少的話,不會造成成數據丟失,或不識別的現象,因為要強制轉換,也要有轉換的基礎呀,比如無論如何,不會將FORM類型轉換成StringBuilder類型的.
vb.net强制类型转换怎么实现
1. 用CTYPE實現(只要運算式與型別之間的轉換定義有效就可成功轉換)
2. 用DirectCast實現(要求物件變數的 Run-Teim 型別必須與指定的型別一樣。)
dim a as new myClass
dim b as new myClass
dim o as object
o = a
//也不用什么具体类型,你告诉我这样做就可以啊,我等会试试directcast,呵呵
b = CTYPE(o, myClass) '或 b=(myClass)o ||另外: 强制类型转换的話,C#,VB.NET都是一樣的
'先揭帖