1

I am modifying a legacy project that must run in VB6, and I have OOP experience but not VB.

Anyway I thought this would be straightforward, I need to add data to a hashmap.

I can call the retreive data function but not the set data function.

Here is the set data function (or sub I guess).

Sub SetSetting(Name As String, Value)
    Member of MBCSettings.MBCSettings
    Store the named setting in the current settings map

So if I try to set something like this:

  gobjMBCSettings.SetSetting("UserName", "223")

I get: Compiler Error, Expected "="

Is my object broken or am I missing something obvious? Thanks.

Rob
  • 2,363
  • 7
  • 36
  • 54
  • 6
    Try without the parens: `gobjMBCSettings.SetSetting "UserName", "223"` – Alex K. Oct 09 '14 at 16:10
  • That works, thanks! Such an odd language. – Rob Oct 09 '14 at 16:15
  • 1
    See also http://stackoverflow.com/questions/10107259/is-it-possible-to-call-a-vb-function-without-the-parenthesis and a number of other questions all related to this – MarkJ Oct 09 '14 at 16:25
  • Not so odd really, method calls with no return value were intended to look like new language verbs. – Bob77 Oct 09 '14 at 17:08

1 Answers1

3

Ah VB6... yes.

In order to invoke a method the standard way you do not use parentheses:

gobjMBCSettings.SetSetting "UserName", "223"

If you want to use parentheses, tack in the call command:

Call gobjMBCSettings.SetSetting("UserName", "223")

It should be noted that using parentheses around ByRef argument without the Call keyword, the argument will be sent in as ByVal.

Public Sub MySub(ByRef foo As String)
    foo = "some text"
End Sub

Dim bar As String
bar = "myText"
MySub(bar)
' bar is "myText"
Call MySub(bar)
' bar is "some text"

It only complained because you were passing in multiple parameters wrapped with a single set of parentheses. Using () to force ByVal also works in VB.NET.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • `using parentheses around ByRef argument without the Call keyword, the argument will be sent in as ByVal.` ... That is absolutely disgusting. – helrich Oct 09 '14 at 19:38
  • @helrich: It's an expression. If you use `val + 0` instead of `(val)` the expression will be calculated in a temp local var which in turn will be passed `ByRef`. – wqw Oct 10 '14 at 13:16