• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

where i m wrong???

Public Class Form1
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        getval(mystr)
        MsgBox(mystr(2))
    End Sub
 
End Class


on module

Module Module1
    Public mystr() As String
    Function getval(ByVal mystr() As String) As String
        ReDim mystr(3)
        mystr(1) = "hkh"
        mystr(2) = "hkh"
        mystr(3) = "hkh"
    End Function
End Module
0
vsvb
Asked:
vsvb
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Corey ScheichDeveloperCommented:
Module Module1
    Public mystr() As String
    Public Function getval(ByVal mystr() As String) As String '<here must be public
        ReDim mystr(3)
        mystr(1) = "hkh"
        mystr(2) = "hkh"
        mystr(3) = "hkh"
    End Function
End Module


Corey2
0
 
Corey ScheichDeveloperCommented:
Actually after a closer look.......

Public Class Form1
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        getval(mystr)
        MsgBox(mystr(2))
    End Sub
 
End Class


on module

Module Module1
    Public mystr() As String
    Public Sub getval(ByRef mystr() As String)
        ReDim mystr(3)
        mystr(1) = "hkh"
        mystr(2) = "hkh"
        mystr(3) = "hkh"
    End Sub
End Module

ByVal passes just the value.  ByRef passes a reference so that you can change the value without passing it back through a function.  I changed the function to a sub because you don't have to pass a string back in my perception of your intention.

Corey2
0
 
Erick37Commented:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Create a local string to pass to your Sub    
    Dim str() As String
    getval(str)
    MsgBox(str(1))

End Sub


'======================

Module Module1

    'Subroutine accepts a string array ByRef
    'and modifies the original array
    Public Sub getval(ByRef mystr() As String)
        ReDim mystr(3)
        mystr(0) = "Zero"
        mystr(1) = "One"
        mystr(2) = "Two"
        mystr(3) = "Three"
    End Sub


End Module
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
RonaldBiemansCommented:
Never use byref if you don't need to (it is very unsafe)

this would be enough

Module Module1
    Public mystr() As String
    Public Sub getval()
        ReDim mystr(3)
        mystr(1) = "hkh"
        mystr(2) = "hkh"
        mystr(3) = "hkh"
    End Sub
End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        getval()
        MsgBox(mystr(2))
    End Sub
0
 
Erick37Commented:
I can see how ByRef can be unsafe, but using Public variables is another no-no unless you have to.
Try to encapsulate and protect your data!
0
 
RonaldBiemansCommented:
true so he could do this instead

Public Function getval(ByVal str() As String) As String()
        ReDim str(3)
        str(1) = "hkh"
        str(2) = "hkh"
        str(3) = "hkh"
        Return str
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mystr() As String
        mystr = getval(mystr)
        MsgBox(mystr(2))
    end sub
0
 
Corey ScheichDeveloperCommented:
What if instead of a public variable the OP used a Friend Variable  this would be safer than a public variable.

Ronald when does passing ByRef become unsafe?  Is it when multi-threading.  If you are running solely in a single thread wouldn't it be pretty safe.  Could you point me to a link describing this?
0
 
S-TwilleyCommented:
What's the use in passing an array when you use a redim on it (it's going to lose all its contents straight away):

    Public Function getval() As String()
        Dim str(3) As String
        str(1) = "hkh"
        str(2) = "hkh"
        str(3) = "hkh"
        Return str
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mystr() As String
        mystr = getval()
        MsgBox(mystr(2))
    end sub
0
 
S-TwilleyCommented:
Strange how such a simple bit of code has got many experts squabbling over it :P
0
 
RonaldBiemansCommented:
Also true S-twlilley :-)
0
 
Erick37Commented:
For that matter why not use this:

MsgBox("hkh")

:)
0
 
Corey ScheichDeveloperCommented:
Or he could use the old set of code and use
Redim Preserve

"Strange...."
I thought exactly that

VSVB what is your intention on this bit of code?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now