vb.net結(jié)構(gòu)數(shù)組 vb中數(shù)組

vb.net 如何在結(jié)構(gòu)體內(nèi)設(shè)定定長(zhǎng)數(shù)組

結(jié)構(gòu)體無(wú)法初始化值,你可以用類(lèi)實(shí)現(xiàn),或者寫(xiě)一個(gè)構(gòu)造函數(shù),把值傳進(jìn)去。

成都創(chuàng)新互聯(lián)公司-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、達(dá)州服務(wù)器托管、等保安全、私有云建設(shè)等企業(yè)級(jí)互聯(lián)網(wǎng)基礎(chǔ)服務(wù),電話聯(lián)系:18982081108

Public Structure wheelmodel

Public ID As Short

Public swapway() As Short

Public start As Short

Public Sub New(ByVal Size As UShort) 'Size就是傳入的數(shù)組的大小

swapway = New Short(Size) {}

End Sub

End Structure

調(diào)用的時(shí)候:

Dim x As wheelmodel = New wheelmodel(10)

vb.net中如何對(duì)結(jié)構(gòu)數(shù)組進(jìn)行new初始化

首先你是怎么重寫(xiě)結(jié)構(gòu)的Sub New的呢?不會(huì)有這個(gè)錯(cuò)誤嗎:“結(jié)構(gòu)無(wú)法聲明沒(méi)有參數(shù)的非共享“Sub New”?

結(jié)構(gòu)是值類(lèi)型,和類(lèi)不一樣,不一定要有構(gòu)造函數(shù)。直接

Dim?B(2)?As?A

如果有一個(gè)含參數(shù)的Sub New(i As Integer)

Dim?B()?As?A={New?A(1),?New?A(2)}

有時(shí)要初始化很多個(gè)的時(shí)候可以用循環(huán)

Dim?c?As?Integer?=?50

Dim?B(c)?As?A

For?i?=?0?To?c

B(i)?=?New?A(i)

Next

不過(guò)這樣是對(duì)變量重新賦值,這種方法用在類(lèi)上比較好。

看看這個(gè)有幫助哦:

在VB.net中如何取變量、結(jié)構(gòu)、數(shù)組、函數(shù)的地址?

當(dāng)然可以的,需要System.Runtime.InteropServices?命名空間中的?Marshal?類(lèi)

Imports?System.Runtime.InteropServices?'這里一定要有?

Public?Class?Form1

Public?Structure?m_Point

Dim?x?As?Integer

Dim?y?As?Integer

End?Structure

Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click

Dim?i?As?Integer?=?50

Dim?ai()?As?Integer?=?{1,?2,?3,?4,?5}

Dim?pi?As?IntPtr?=?GCHandle.Alloc(i,?GCHandleType.Pinned).AddrOfPinnedObject()?'取得整形變量的指針?

Dim?pai?As?IntPtr?=?GCHandle.Alloc(ai,?GCHandleType.Pinned).AddrOfPinnedObject()?'取得整形數(shù)組首地址指針

MsgBox(Marshal.ReadInt32(pi,?0))?'讀回整形變量指針指向的值

MsgBox(Marshal.ReadInt32(pai,?0?*?4))?'讀回?cái)?shù)組的第一個(gè)元素

MsgBox(Marshal.ReadInt32(pai,?1?*?4))?'讀回?cái)?shù)組的第二個(gè)元素

MsgBox(Marshal.ReadInt32(pai,?2?*?4))?'讀回?cái)?shù)組的第三個(gè)元素

'-----下面是結(jié)構(gòu)--------------------------

Dim?m_p?As?New?m_Point

m_p.x?=?100

m_p.y?=?50

Dim?pm_p?As?IntPtr?=?GCHandle.Alloc(m_p,?GCHandleType.Pinned).AddrOfPinnedObject()?'取得結(jié)構(gòu)首地址指針?

MsgBox(Marshal.ReadInt32(pm_p,?0?*?4))?'讀回結(jié)構(gòu)的第一個(gè)值

MsgBox(Marshal.ReadInt32(pm_p,?1?*?4))?'讀回結(jié)構(gòu)的第二個(gè)值

End?Sub

End?Class

VB.net中怎么定義初始化一個(gè)結(jié)構(gòu)體數(shù)組

struct T_ChildStruct

{

int nChildData;

string strChildData;

T_ChildStruct()

{

nChildData = 0;

strChildData = ""; // string可以不用寫(xiě)初始化,本身構(gòu)造中就有

}

};

struct T_FatherStruct

{

int nFatherData;

string strFatherData;

T_ChildStruct arrChild[10];

T_FatherStruct()

{

nFatherData = 0;

strFatherData = "";

}

};

網(wǎng)站標(biāo)題:vb.net結(jié)構(gòu)數(shù)組 vb中數(shù)組
網(wǎng)站地址:http://muchs.cn/article18/hjssdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、電子商務(wù)、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化