You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中使用过程为结构体变量赋值失败问题求助

问题原因与解决方案

你的问题出在VB.NET中结构体是值类型的特性上:默认情况下,过程的参数是按值传递(ByVal,可省略),调用defineInst时,你传递的是snare、hiHat等变量的副本,过程里修改的只是这个副本,原变量完全没被改动,所以运行后所有乐器变量的数值都没变化。

解决方案1:按引用传递结构体参数

给defineInst过程的inst参数加上ByRef关键字,让它按引用传递,这样过程里操作的就是原变量本身,而非副本:

Sub defineInst(ByRef inst As instInfo, vol As Double, cost As Double, amt As Integer, bpm As Double, costmult As Double, indivVol As Double)
    inst.vol = vol
    inst.cost = cost
    inst.amt = amt
    inst.bpm = bpm
    inst.costMult = costmult
    inst.indivVol = indivVol
End Sub

解决方案2:将结构体改为类(引用类型)

如果你的场景更适合使用引用类型,可以把Structure改成Class。类是引用类型,默认传递的是对象的引用,过程中修改的就是原对象:

Class instInfo
    Public vol As Double
    Public cost As Double
    Public amt As Integer
    Public bpm As Double
    Public costMult As Double
    Public indivVol As Double
End Class

注意:类的成员默认访问级别是Private,所以需要改成Public才能在外部赋值,或者使用属性封装。

内容的提问来源于stack exchange,提问作者Connor Schnurr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:50:21