locked
Difference between ByVal and ByRef RRS feed

  • Question

  • Hi, Everyone

    This Snippet of code doesn't work , Could i know why ???

     Sub CheckF14Pic(ByVal TableName As String,
                      ByVal ColumnName As String,
                      ByVal pic1 As Image,
                      ByVal coloredpic As Image,
                      ByVal graypic As Image,
                      Optional chx1 As Boolean = False,
                      Optional txt As String = "") 
            Using conn As New SqlConnection(cs)
                conn.Open()
                Using cmd As New SqlCommand()
                    cmd.Connection = conn
                    cmd.CommandText = String.Format("select {0}
                                                     from {1} 
                                                     where {0}=1", ColumnName, TableName)
                    Using rdr As SqlDataReader = cmd.ExecuteReader()
                        If rdr.Read Then
                            
                            MessageBox.Show("This flat is Booked, Choose another one", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                            If Not rdr Is Nothing Then
                                rdr.Close()
                            End If
    
                        Else
                            Select Case True
                                Case pic1 IsNot coloredpic      'picbox44
                                    pic1 = coloredpic           'picbox44
                                    chx1 = True
                                    txt = Val(txt) + 1
                                    Exit Sub
                            End Select
                            Select Case True
                                Case pic1 IsNot graypic
                                    pic1 = graypic  'picbox59
                                    chx1 = False
                                    txt = Val(txt) - 1
                            End Select
                        End If
    
                    End Using
                End Using
            End Using
    
        End Sub
    Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
            CheckF14Pic("UnitA", "F4",
                      PictureBox4.BackgroundImage,
                      PictureBox44.BackgroundImage,
                      PictureBox59.BackgroundImage,
                      CheckBox4.Checked,
                      txtUNA.Text)
        End Sub

    But if i put it like this 

     Private Sub PictureBox4_Click(sender As Object, e As EventArgs) Handles PictureBox4.Click
            Using conn As New SqlConnection(cs)
                conn.Open()
                Using cmd As New SqlCommand()
                    cmd.Connection = conn
                    cmd.CommandText = "select F4
                                       from UnitA 
                                       where F4=1"
                    Using rdr As SqlDataReader = cmd.ExecuteReader()
                        If rdr.Read Then
                            MessageBox.Show("This Flat is Booked ,Choose another one", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                            If Not rdr Is Nothing Then
                                rdr.Close()
                            End If
    
                        Else
                            Select Case True
                                Case PictureBox4.BackgroundImage IsNot PictureBox44.BackgroundImage 'Colored
                                    PictureBox4.BackgroundImage = PictureBox44.BackgroundImage 
                                    CheckBox4.Checked = True
                                    txtUNA.Text = Val(txtUNA.Text) + 1
                                    Exit Sub
                            End Select
                            Select Case True
                                Case PictureBox4.BackgroundImage IsNot PictureBox59.BackgroundImage
                                    PictureBox4.BackgroundImage = PictureBox59.BackgroundImage       'Gray
                                    CheckBox4.Checked = False
                                    txtUNA.Text = Val(txtUNA.Text) - 1
                            End Select
    
                        End If
    
                    End Using
                End Using
            End Using
        End Sub

    It works , Could i find any guide ,,, 

    Also in a Sub of loading from database it looks like this 


    Regards From Egypt


    • Edited by Amr_Aly Saturday, January 4, 2020 12:39 PM
    Saturday, January 4, 2020 10:34 AM

Answers

  • What do you mean "doesn't work" ?

    It does not return a value from the sub? ie txt ? Maybe you need to use ByRef instead of ByVal.

    • Marked as answer by Amr_Aly Saturday, January 4, 2020 12:40 PM
    Saturday, January 4, 2020 11:01 AM

All replies

  • What do you mean "doesn't work" ?

    It does not return a value from the sub? ie txt ? Maybe you need to use ByRef instead of ByVal.

    • Marked as answer by Amr_Aly Saturday, January 4, 2020 12:40 PM
    Saturday, January 4, 2020 11:01 AM
  • What do you mean "doesn't work" ?

    It does not return a value from the sub? ie txt ? Maybe you need to use ByRef instead of ByVal.

    Thanks Tommy,

    It is working fine Now.... I don't know the difference between ByVal and ByRef  , So i was use ByVal or don't use any of the ,, Many Subs were not work as expected but now it's working fine

    Thanks again Tommy


    Regards From Egypt

    Saturday, January 4, 2020 11:37 AM

  • Thanks Tommy,

    It is working fine Now.... I don't know the difference between ByVal and ByRef  , So i was use ByVal or don't use any of the ,, Many Subs were not work as expected but now it's working fine

    Thanks again Tommy


    Regards From Egypt

    We have all done that... still do that!

    When you use ByVal a copy of the original value before entering the sub is passed into the sub routine. Then, if you change that local value it does not change the original value in the calling sub. When you use ByRef a reference to the original value is passed to the sub and then when you change that value it follows the reference back to the original value in the calling routine and changes the original in the calling sub. So ByVal does not change the original value and ByRef Does.

    If your question is answered please mark the post using the Mark as Answer link at the bottom of the correct post(s).

    Saturday, January 4, 2020 12:23 PM
  • Thanks Tommy 

    Could you please take a look at this 

    another thread  


    Regards From Egypt

    Saturday, January 4, 2020 12:43 PM
  • Thanks Tommy 

    Could you please take a look at this 

    another thread  


    Regards From Egypt

    I looked... but sorry not my specialty. I am sure Julie or others will help if you get it down to a few simple well defined questions.

    PS When you post code with the code block tool be sure to select the language at the top of the box where you paste the code. Select vb.net. That is why your posts have green formatting and other formatting problems in that thread. You need to select the proper language in the code block editor.

    Saturday, January 4, 2020 12:58 PM
  • Thanks Again Tommy ,

    I missed to do what you said about choosing the language, 

    I fixed it thanks


    Regards From Egypt

    Saturday, January 4, 2020 2:04 PM