Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

where i m wrong???

Posted on 2005-04-18
12
Medium Priority
?
224 Views
Last Modified: 2010-04-23
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
Comment
Question by:vsvb
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 13806543
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
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 13806633
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
 
LVL 32

Expert Comment

by:Erick37
ID: 13806637
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13806721
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
 
LVL 32

Expert Comment

by:Erick37
ID: 13806776
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
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 375 total points
ID: 13806835
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
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 13806905
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13806976
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13806989
Strange how such a simple bit of code has got many experts squabbling over it :P
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13807022
Also true S-twlilley :-)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13807028
For that matter why not use this:

MsgBox("hkh")

:)
0
 
LVL 13

Expert Comment

by:Corey Scheich
ID: 13807031
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question