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 8780257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:59:41+00:00 2026-06-13T19:59:41+00:00

If x is equal to 1 then we need the SqlDataReader from GetDataReader1 method.

  • 0

If x is equal to 1 then we need the SqlDataReader from GetDataReader1 method. Otherwise, we need SqlDataReader from GetDataReader2. That is the only difference. Other than that, all the code needed (where it says do stuff here) is going to be duplicated. How can I make this more elegant so I don’t have to repeat all the logic within both using statements?

Update: In simplifying the code for this post, I missed an additional factor. GetDataReader1 takes 2 parameters and GetDataReader2 takes 3 parameters. If I create one GetDataReader function, and move the If statement there, will I be forced to pass in the extra parameter that is currently not needed for the GetDataReader1 method?

Dim value1, value2, value3 As String

If x = 1 Then
    Using myDataReader As SqlDataReader = GetDataReader1(value1, value2)
        myDataReader.Read()
        If myDataReader.HasRows Then
            'do stuff here
        End If
     End Using
 Else
     Using myDataReader As SqlDataReader = GetDataReader2(value1, value2, value3)
         myDataReader.Read()
         If myDataReader.HasRows Then
             'do stuff here
         End If
     End Using
 End If
  • 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-06-13T19:59:43+00:00Added an answer on June 13, 2026 at 7:59 pm

    Pass in x to GetDataReader and let it determine what to return.

    Using myDataReader As SqlDataReader = GetDataReader(x)
        myDataReader.Read()
        If myDataReader.HasRows Then
            'do stuff here
        End If
    End Using
    

    This pushes the If into GetDataReader where your shouldn’t have much duplication.


    Update – following update on the question.

    You can still use the same mechanism – pushing the decision to a method – you simply need to pass in all the parameters that may be needed in addition to x:

    Using myDataReader As SqlDataReader = GetDataReader(x, value1, value2, value3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a java method that takes in an integer and then increments it
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Are they equal in safeness? I was informed that using <?=$function_here?> was less safe,
I need to set equal height on a series of divs inside another div
string temp is equal to ZERO:\t.WORD\t1 from my debugger. (the first line of my
I need to check that two variables match fields in a row where the
i need to prove that following algorithm works correctly,i know induction,but don't know how
trying to get last from the tag if data of last line is equal
I need to be able to match a certain string ( '[' then any
I need to delete a character from a character array and re-size the array.

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.