Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:13:04+00:00 2026-05-13T20:13:04+00:00

Switching Byref to Byval on method calls I have many warnings raised due to:

  • 0

Switching Byref to Byval on method calls

I have many warnings raised due to:

“Implicit conversion from xxxx to yyyy in copying the value of ‘ByRef’ parameter zzzz back to the matching argument.”

My feeling is that it would be safe to change the function parameters from byref to byval as nothing special is being done with the reference type pointers inside these methods the reference types are simply being used and I think the behaviour would be exactly the same if running with a copy a pointer rather than the original.

Another consideration is that I have two classes which inherit from a base class. The same situation is occuring in that the byref params are causing implicit casting from the base class to the narrower concrete class. Again I can’t see any problems with this code running byval either.

Does anyone have any tips regarding use of parameters in functions when dealing with reference types?

Some of the other things that are currently being passed around byref in my project are database connection objects i.e. OracleConnection and SqlConnection. Is there any good reason for passing these around byref?

Example 1

Implicit conversion from ‘Object’ to ‘Integer’ in copying the value of ‘ByRef’ parameter ‘value’ back to the matching argument.

Calling code:

cmd = New SqlCommand()

cmd.Parameters.Add(CreateParameter("Alpha", SqlDbType.Int,ParameterDirection.Input, -1, AlphaValue))

Function:

Private Function CreateParameter(ByVal parameterName As String, ByVal dbType As SqlDbType, ByVal direction As ParameterDirection, ByVal size As Integer, ByRef value As Object) As SqlParameter
    Dim retParam As SqlParameter
    retParam = New SqlParameter(parameterName, dbType)
    retParam.Direction = direction
    retParam.Size = size
    retParam.Value = value
    Return retParam
End Function

Example 2

Implicit conversion from ‘System.Data.IDataReader’ to ‘System.Data.SqlClient.SqlDataReader’ in copying the value of ‘ByRef’ parameter ‘reader’ back to the matching argument.

Calling code:

Dim reader As new SqlDataReader

ReleaseReader(reader)

Method:

    Public Sub ReleaseReader(ByRef reader As IDataReader)
        If reader IsNot Nothing Then
            If Not reader.IsClosed Then
                reader.Close()
            End If
            reader.Dispose()
        End If
    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:13:05+00:00Added an answer on May 13, 2026 at 8:13 pm

    When defining a method in VB.Net, or C# for that matter, the you should pass parameters by value (ByVal) unless you need to take advantage of ByRef semantics. If you are not resetting the parameter value within the method then you should definitely turn these into ByVal calls.

    If you are resetting the reference but not taking advantage of it from the call site then I would write a helper method which takes the parameter ByVal and calls into the one taking it ByRef. This will remove the warning because the resulting code won’t be subject to narrowing conversion errors.

    For example:

    Public Sub ExampleMethod(ByRef p1 As Object) 
      p1 = "foo"
    End Sub
    
    Public Sub ExampleMethodWrapper(ByVal p1 as Object)
      ExampleMethod(p1)
    End Sub 
    
    Public Sub Test()
      Dim v1 As String = "hello"
      Dim v2 As String = "world" 
      ExampleMethod(v1) ' Warning generated
      ExampleMethodWrapper(v2) ' No warning
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge Web App that's switching from a HTML-rendered-on-the-server-and-pushed-to-the-client approach to a
Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
We are considering switching from MS Visual Studio 2003 to MS Visual Studio 2005
We're considering switching our site from Prototype to jQuery. Being all-too-familiar with Prototype, I'm
My group is thinking about switching our platform for web UI from ASP.net to
Switching from JVM 1.4 to 1.5 has performance benefits as per release notes. http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#performance
Switching from Microsofts Oracle Driver to ODP.NET version 10.2.0.100. After changing the data types
After switching from C++ to C++ w/boost, do you think your OOD skills improved?
When switching back from a local branch, I accidentally checked out a new branch,
After switching from firefox testing to internet explorer testing, some elements couldn't be found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.