dcsimg
Results 1 to 10 of 10
  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    59

    Resolved [RESOLVED] c# to vb.net code conversion : task with parameter

    I need the this code in vb.net :

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            string x = "hey";
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Action<object> action;
                action = doWorkWithObject;
                object parameterData = 5;
                Task task = new Task(action, parameterData);
                
                task.Start();
                task.Wait(); //take out this line and x will be "hey" in the textbox.
                textBox1.Text = x;
            }
            private void doWorkWithObject(object o)
            {     // The task runs this code when it is started     ... }
                MessageBox.Show("xyz");
                x = o.ToString();
            }
        }
    }

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,621

    Re: c# to vb.net code conversion : task with parameter

    So what have you tried and where are you stuck? There are plenty of code converters on the web. This site isn't one of them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    59

    Re: c# to vb.net code conversion : task with parameter

    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports System.Windows.Forms
    
    Namespace WindowsFormsApp1
    
        Public Partial Class Form1
            Inherits Form
    
            Private x As String = "hey"
    
            Public Sub New()
                InitializeComponent()
            End Sub
    
            Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
                Dim action As Action(Of Object)
                action = AddressOf doWorkWithObject
                Dim parameterData As Object = 5
                Dim task As Task = New Task(action, parameterData)
                task.Start()
                task.Wait()
                textBox1.Text = x
            End Sub
    
            Private Sub doWorkWithObject(ByVal o As Object)
                MessageBox.Show("xyz")
                x = o.ToString()
            End Sub
        End Class
    End Namespace
    I get the error at :
    action = AddressOf doWorkWithObject

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,621

    Re: c# to vb.net code conversion : task with parameter

    What error? Please provide ALL the relevant information. That said, most likely you just have to create an Action(Of Object) explicitly rather than assuming that the result of the AddressOf statement will be implicitly converted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    59

    Re: c# to vb.net code conversion : task with parameter

    now it doesn't do anything.
    I just want to run a task of a sub with parameters, just a simple working example. I can use global variables but I want to know how to use parameters, the c# code works, but I can't translate it to vb.net

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,621

    Re: c# to vb.net code conversion : task with parameter

    Quote Originally Posted by bugsi View Post
    now it doesn't do anything.
    Firstly, if the behaviour has changed then the code must have changed. We have no idea how you changed the code so we can only speculate over what you have done wrong. Stop assuming that we have psychic powers and show us the code you have now.

    Secondly, it doesn't do nothing. The fact that you don't see the behaviour you expect does not mean that nothing happens. Have you debugged the code to see what actually does happen? If not, do that now. Place a breakpoint at the top of the Click event handler and step through that to see what happens. Place a breakpoint on the 'doWorkWithObject' method and see whether that gets hit. Don't sit on your hands and ask us to do everything for you. Do what you can for yourself first, which ALWAYS includes debugging the code you have, and then, if you're still stuck, post ALL the relevant information here. The current code is ALWAYS relevant information. If we make assumptions about the code you're using then we can make incorrect assumptions and that means we're wasting our time and yours providing solutions to problems that you don't have.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,575

    Re: c# to vb.net code conversion : task with parameter

    I just tried your code, I thought perhaps Action<T> wanted you to use New in this case. But as-is, your code snippet works for me.

    So I'm with JMC: if you just say "some error" and don't tell us what the error is, we can't help you.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    59

    Re: c# to vb.net code conversion : task with parameter

    Code:
    Public Class Form1
        Private x As String = "hey"
    
    
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim action As Action(Of Object)
            action = AddressOf doWorkWithObject
            Dim parameterData As Object = 5
            Dim task As Task = New Task(action, parameterData)
            task.Start()
            task.Wait()
            TextBox1.Text = x
        End Sub
    
        Private Sub doWorkWithObject(ByVal o As Object)
            MessageBox.Show("xyz")
            x = o.ToString()
        End Sub
    
    End Class
    i'm clicking the button and nothing happens

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,621

    Re: c# to vb.net code conversion : task with parameter

    Quote Originally Posted by bugsi View Post
    Code:
    Public Class Form1
        Private x As String = "hey"
    
    
        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim action As Action(Of Object)
            action = AddressOf doWorkWithObject
            Dim parameterData As Object = 5
            Dim task As Task = New Task(action, parameterData)
            task.Start()
            task.Wait()
            TextBox1.Text = x
        End Sub
    
        Private Sub doWorkWithObject(ByVal o As Object)
            MessageBox.Show("xyz")
            x = o.ToString()
        End Sub
    
    End Class
    i'm clicking the button and nothing happens
    That is exactly the same code as you in post #3. How could you be getting an error then and now you're not without changing the code? Also, I already said that "nothing happens" is simply false. Are you refusing to debug your code or do you not now how to but have chosen not to tell us that, or have you already debugged your code but have chosen not to tell us what happened?

    Ah, I think I see what the issue is now. There's no Handles clause on that button1_Click method so it's not actually handling the event. If you had created the event handler the normal way then that wouldn't have been the case. If you had debugged your code as you should have in the first place and I told you to hours ago then you could have actually told us that that method wasn't executed and that would have been one of the first things we looked for. Please learn a lesson from this and debug your code in future. It will prevent everyone wasting their time like this.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    59

    Re: c# to vb.net code conversion : task with parameter

    Quote Originally Posted by jmcilhinney View Post
    That is exactly the same code as you in post #3. How could you be getting an error then and now you're not without changing the code? Also, I already said that "nothing happens" is simply false. Are you refusing to debug your code or do you not now how to but have chosen not to tell us that, or have you already debugged your code but have chosen not to tell us what happened?

    Ah, I think I see what the issue is now. There's no Handles clause on that button1_Click method so it's not actually handling the event. If you had created the event handler the normal way then that wouldn't have been the case. If you had debugged your code as you should have in the first place and I told you to hours ago then you could have actually told us that that method wasn't executed and that would have been one of the first things we looked for. Please learn a lesson from this and debug your code in future. It will prevent everyone wasting their time like this.
    thank you so much !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width