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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:46:06+00:00 2026-05-26T12:46:06+00:00

how can I convert the following code from C# to VB without exposing variable

  • 0

how can I convert the following code from C# to VB without exposing “variable” as global variable.

private void SomeMethod(SomeType variable)
{
    this.SomeEvent+= delegate
    {
        if (variable == 1)
        {
           this.DoSomething();
        }
    }
    //here I have some other code
}
  • 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-26T12:46:07+00:00Added an answer on May 26, 2026 at 12:46 pm

    One possible solution

    Private Sub SomeMethod(ByVal variable As Integer)
        AddHandler Me.SomeEvent,
            Sub()
                If (variable = 1) Then
                    Me.DoSomething()
                End If
            End Sub
        Console.WriteLine("ciao")
    End Sub
    

    I just tried that and it works like a charm, so i don’t know why u say it doesnt 🙁 Visual studio 2010.

    You can also do something like this

    Private Sub SomeMethod(ByVal variable As Integer)
        Me.SomeEvent = DirectCast(Delegate.Combine(Me.SomeEvent, Sub()
            If (variable = 1) Then
                Me.DoSomething
            End If
        End Sub), MyDelegate)
        ...mycode
    End Sub
    

    Delegate.Combine have exactly the same effect as AddHandler.

    I don’t have visual studio 2008 so I don’t know how to write that in VS2008, try the second solution, the first seems to work only on 2010.

    If this doesn’t work you can try this out, more code to write:

    Public Delegate Sub MyDelegate()
    
    Public Class Class1
    
        Public Event SomeEvent As MyDelegate
    
        Private Class MyDelegateClass
    
            Public Owner As Class1
            Public Variable As Integer
    
            Public Sub Method()
                If (Variable = 1) Then
                    Owner.DoSomething()
                End If
            End Sub
    
        End Class
    
        Private Sub SomeMethod(ByVal variable As Integer)
    
            Dim dc As New MyDelegateClass
            dc.Owner = Me
            dc.Variable = variable
    
            AddHandler Me.SomeEvent, AddressOf dc.Method
            Console.WriteLine("ciao")
        End Sub
    
        Public Sub DoSomething()
            Console.WriteLine("hello")
        End Sub
    
    End Class
    

    Visual studio syntactic sugar does something like this with anonymous delegate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some tool that can automatically convert the following c style code A
If I do the following I can convert from a time_struct object to a
I have the following line of code: LPSTR address = T2A((LPTSTR)hostAddress); Can I convert
How can I run a ruby piece of code from a buffer, without actually
Following up from my previous question. Can anyone explain why the following code compiles
I got the following code from this question: What is the best way to
How can I convert the following code to use boost unit test framework: #include
I am using the following code to convert files from php to html. In
Need to convert the following code from Ruby to C#. However I'm kind of
I'm trying to convert the following code from C# to Java, but I get

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.