[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();
}
}
}
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.
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
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.
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
Re: c# to vb.net code conversion : task with parameter
Quote:
Originally Posted by
bugsi
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.
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.
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
Re: c# to vb.net code conversion : task with parameter
Quote:
Originally Posted by
bugsi
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.
Re: c# to vb.net code conversion : task with parameter
Quote:
Originally Posted by
jmcilhinney
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 ! :)