【发布时间】:2016-08-29 06:33:43
【问题描述】:
嗨,我只是想在 vb 中使用 Byref 和 ByVal 做一些事情显然。下面是我试图更改 arr List 值的方法,但它给出了 Collection 已修改的异常;枚举操作可能无法执行,请帮忙理解Byval和Byref的这个逻辑。
Public Function arrayReturn() As List(Of Object)
Dim arr As List(Of Object)
arr = New List(Of Object)
For index As Integer = 1 To 5
arr.Add(index)
Debug.Write(index.ToString & " ")
Next
For Each i As Object In arr
arr.Add(intReturn(i))
Next
Return arr
End Function
Public Function intReturn(ByRef i As Object) As Integer
i += i
Return i
End Function
Sub Main()
Dim obj As List(Of Object)
obj = New List(Of Object)
obj = arrayReturn()
For Each obj2 As Object In obj
Console.WriteLine(obj2)
Next
End Sub
我的问题是这个方法在这里我试图检索一个事件对象并将其放入事件列表中,但我不知道我是否正确
Public Function GetIcalForAppointments(nxtMonth As Integer, prevMonth As Integer, strStaffAPIKey As String) As Ical.NET.Calendar
Dim objDTOIcallApt As DTOICallAppt
objDTOIcallApt = New DTOICallAppt
Dim lst As List(Of DOAppointment)
objDTOIcallApt = GetAllAppointmentsForIcal(nxtMonth, prevMonth, strStaffAPIKey)
lst = objDTOIcallApt.Appointments
Dim timeZoneName As String = objDTOIcallApt.UserTimezone
Dim calendar As Ical.NET.Calendar
calendar = New Ical.NET.Calendar()
Dim timeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)
calendar.AddTimeZone(New Ical.NET.VTimeZone(timeZoneName))
Dim evt As Ical.NET.Event
For Each appt As DOAppointment In lst
GetIcalForAppointment(appt, objDTOIcallApt, calendar, evt)
Next
Return calendar
End Function
Public Function GetIcalForAppointment(app As DOAppointment, objResponse As DTOICallAppt, calendar As Ical.NET.Calendar, ByRef evt As Ical.NET.Event) As Ical.NET.Event
evt = calendar.Create(Of Ical.NET.Event)()
evt.Start = getCalDateTime(app.StartDate, calendar)
evt.End = getCalDateTime(app.EndDate, calendar)
evt.Description = app.FullSubject
evt.Location = app.AppointmentInOrgName
evt.IsAllDay = False
evt.Created = getCalDateTime(app.CreatedDatetime, calendar)
If objResponse.UserEmail <> "" AndAlso objResponse.UserEmail.Contains("@") = True Then
evt.Organizer = New Organizer("MAILTO:" + objResponse.UserEmail)
End If
Select Case app.AppointmentStatus
Case "Cancelled"
evt.Status = Ical.NET.EventStatus.Cancelled
Exit Select
Case "NoShow"
evt.Status = Ical.NET.EventStatus.Cancelled
Exit Select
Case "Scheduled"
evt.Status = Ical.NET.EventStatus.Tentative
Exit Select
Case Else
evt.Status = Ical.NET.EventStatus.Confirmed
Exit Select
End Select
evt.Categories.Add("Work")
evt.Comments.Add(app.CreatedDetails)
evt.Resources.Add(app.Resource1Name)
evt.Resources.Add(app.Resource2Name)
Dim strSummary As String = app.FullSubject
If app.IsPatConsentRequired = True Then
If app.IsPatConsentGiven = True Then
strSummary = strSummary & Convert.ToString(" | Consent given")
Else
strSummary = strSummary & Convert.ToString(" | Consent Pending")
End If
End If
If app.PatMedicalHistoryCasesheetID <> "" Then
If app.IsMedicalHistoryFilled = True Then
strSummary = strSummary & Convert.ToString(" | Medical History filled")
Else
strSummary = strSummary & Convert.ToString(" | Medical History pending")
End If
End If
evt.Summary = strSummary
evt.Uid = app.ID
Dim alarm As New Ical.NET.Alarm
alarm.Action = Ical.NET.AlarmAction.Display
alarm.Summary = "Appointment with " + app.AppointmentWithFullName + " at " + app.AppointmentInOrgName
alarm.Trigger = New Trigger(TimeSpan.FromMinutes(-30))
evt.Alarms.Add(alarm)
Return evt
End Function
所以在这里你可以看到我每次都在检索单个事件对象并尝试将其推送到事件列表中
【问题讨论】:
-
在您的第二个 for 循环中,您将添加新元素而不是更改现有元素。
arr(i) = intReturn(i)应该可以工作。 -
以及如何处理在其他方法中填充的对象,但对于 foreach 循环...@turamarth
-
如果唯一的问题是异常,@Turamarth 是对的。如果您需要更多帮助,您需要重写您的答案以说明具体问题或为每个问题编写多个问题。如果您需要解释什么是 byref 和 byval 的手册,here 它是
标签: asp.net vb.net pass-by-reference pass-by-value