Gary,
As I suspect you are figuring out.
Functions cannot be the target of an assignment! Period!
Although Mid looks like a function that is a target of an assignment, its
not, its a Statement!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmMid.asp
Statements: such as Mid, If, For are translated into specific IL
instructions by the compiler.
IMPORTANT: Do not confuse the above Mid Statement with the Mid function.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctMid.asp
My first thought was using a Shared Default property to update the string:
Public Class Entry
Public Default Shared Property Item(ByVal index As Integer, ByRef
input As String) As String
Get
...
End Get
Set(ByVal value As String)
...
End Set
End Property
End Class
| dim sStr as string
| sStr = Entry(2,"a,b,c,d") ' Sets sStr = "b"
| Entry(2,sStr) = "B" ' Sets sStr = "a,B,c,d"
Unfortunately Default properties cannot Shared, so you would need to
explicitly give the property name as in:
| dim sStr as string
| sStr = Entry.Item(2,"a,b,c,d") ' Sets sStr = "b"
| Entry.Item(2,sStr) = "B" ' Sets sStr = "a,B,c,d"
Further you cannot have a ByRef parameter on a property (as logically it
doesn't make sense). If String was mutable class then the property
Hack/trick might be an option...
In which case I would consider a Get & Set method, something like:
Public Class Entry
Public Shared Function [Get](ByVal index As Integer, ByVal input As
String) As String
Dim values() As String = input.Split(","c)
Return values(index - 1)
End Function
Public Shared Sub [Set](ByVal index As Integer, ByRef input As
String, ByVal value As String)
Dim values() As String = input.Split(","c)
values(index - 1) = value
input = String.Join(","c, values)
End Sub
End Class
Dim sStr, value As String
sStr = "a,b,c,d"
value = Entry.Get(2, sStr) ' Sets value = "b"
Entry.Set(2, sStr, "B") ' Sets sStr = "a,B,c,d"
Alternatively I would consider making sStr itself a class.
Public Class Entry
Dim values() As String
Public Sub New(ByVal input As String)
values = input.Split("c"c)
End Sub
Default Public Property Item(ByVal index As Integer) As String
Get
Return values(index - 1)
End Get
Set(ByVal value As String)
values(index - 1) = value
End Set
End Property
Public Overrides Function ToString() As String
Return String.Join(","c, values)
End Function
End Class
Dim sStr As New Entry("a,b,c,d")
Dim value As String
value = sStr(2) ' Sets sStr = "b"
sStr(2) = "B" ' Sets sStr = "a,B,c,d"
Especially if there was other behavior that could be assigned to the Entry
class.
Hope this helps
Jay
|I want to create a function and can only get half way to my goal.
|
| dim sStr as string
| sStr = Entry(2,"a,b,c,d") ' Sets sStr = "b"
| Entry(2,sStr) = "B" ' Sets sStr = "a,B,c,d"
|
|
| I tried create a public property. However, the Set does not allow
| ByRef. How do I work around this?
|
|
| -----------------------------------------------------------
| Gary Kahrau
| (e-mail address removed)
| -----------------------------------------------------------