C# code errors out in vb.net translation

P

Peter Proost

Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 
K

Ken Tucker [MVP]

Hi,

Since you are not actually using x for anything I would use nothing
instead

pi.SetValue(testForm, newValue, nothing)


Ken
 
P

Peter Proost

Hi Ken that did it, thanks. I still have got an other problem that I thought
would go away if my first problem was fixed but I'll first try to solve it
myself and if I can't solve it I'll post it. Thanks again

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

Ken Tucker said:
Hi,

Since you are not actually using x for anything I would use nothing
instead

pi.SetValue(testForm, newValue, nothing)


Ken
------------------
Peter Proost said:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 
P

Peter Proost

Here I am again,

this is the complete code I translated but it keeps crashins when I call
this:
SetControlPropertyValue("txtFrac", "Text", "5") and that is caused by this:

Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)

because I can't get t1.GetField(controlName, flags) to return something it
always returns nothing

Any help would be much appreciated, I've included the .net code and the
original C# code

thanks in advance

Greetz Peter



it says object reference not set
Imports System.Threading
Imports System.windows.Forms
Imports System.Reflection

Module Module1

Private testForm As Form = Nothing
Private flags As BindingFlags = (BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance)
Private delay As Integer = 1500


Sub Main()
Dim exePath As String
Try
Console.WriteLine("Starting UItester")

Console.WriteLine("Launching WinApp under test")
exePath = "C:\Documents and Settings\pproost\Mijn
documenten\Visual Studio Projects\access\bin\access.exe"
testForm = LaunchApp(exePath, "access.Form2")

Console.WriteLine("Moving form")
SetFormPropertyValue("Location", New System.Drawing.Point(200,
200))
SetFormPropertyValue("Location", New System.Drawing.Point(500,
500))

Console.WriteLine("Resizing form")
SetFormPropertyValue("Size", New System.Drawing.Size(600, 600))
SetFormPropertyValue("Size", New System.Drawing.Size(300, 320))

Console.WriteLine("Setting txtFrac to 5")
SetControlPropertyValue("txtFrac", "Text", "5")

Console.WriteLine("Clicking btnFrac")
InvokeMethod("btnFrac_Click", New Object() {Nothing, New
EventArgs})

Console.WriteLine("Checking for result:")
Dim pass As Boolean = True
If (CStr(GetControlPropertyValue("txtResult", "Text")) <> "120")
Then pass = False

If (pass) Then
Console.WriteLine("UI test scenario result: PASS")
Else
Console.WriteLine("\nUI test scenario result: *FAIL*")
End If
Console.WriteLine("Clicking menu File->Exit in 5 seconds . . .
")
Thread.Sleep(3500)
InvokeMethod("MenuItem4_Click", New Object() {Nothing, New
EventArgs})
Console.ReadLine()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub SetControlPropertyValue(ByVal controlName As String, ByVal
propertyName As String, ByVal newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetControlPropertyValueHandler(AddressOf
SetControlPropertyValue), New Object() {controlName, propertyName,
newValue})
'Return
End If
Dim t1 As Type = testForm.GetType
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
pi.SetValue(ctrl, newValue, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub

Delegate Sub SetControlPropertyValueHandler(ByVal controlName As String,
ByVal propertyname As String, ByVal newValue As Object)

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() {propertyName, newValue})
'Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)

pi.SetValue(testForm, newValue, Nothing)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Delegate Sub SetFormPropertyValueHandler(ByVal propertyName As String,
ByVal newValue As Object)


Private Function GetControlPropertyValue(ByVal controlName As String,
ByVal propertyName As String)
Try
If (testForm.InvokeRequired) Then

Thread.Sleep(delay)
Return testForm.Invoke(New
GetControlPropertyValueHandler(AddressOf GetControlPropertyValue), New
Object() {controlName, propertyName})

End If

Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As New Object
ctrl = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
Return pi.GetValue(ctrl, New Object)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function


Delegate Function GetControlPropertyValueHandler(ByVal controlName As
String, ByVal propertyName As String) As Object

Private Sub InvokeMethod(ByVal methodName As String, ByVal parms As
Object()) '{parms})
Try
If (testForm.InvokeRequired) Then
Thread.Sleep(delay)
testForm.Invoke(New InvokeMethodHandler(AddressOf
InvokeMethod), New Object() {methodName, parms})

End If

Dim t As Type = testForm.GetType()
Dim mi As MethodInfo = t.GetMethod(methodName, flags)
mi.Invoke(testForm, parms)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub


Delegate Sub InvokeMethodHandler(ByVal methodName As String, ByVal parms
As Object())


Private Function LaunchApp(ByVal exePath As String, ByVal formName As
String) As Form
Try
Thread.Sleep(delay)
Dim a As [Assembly] = [Assembly].LoadFrom(exePath)
Dim formType As Type = a.GetType(formName)
'Dim resultForm As Form =
DirectCast(a.CreateInstance(formType.FullName),a.GetType(formName))
Dim resultForm As Form =
CType(a.CreateInstance(formType.FullName), Form)
Dim t As Thread = New Thread(New ThreadStart(AddressOf New
AppState(resultForm).RunApp))
t.ApartmentState = ApartmentState.STA
t.IsBackground = True
t.Start()
Return resultForm
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function

Private Class AppState

Public Sub New(ByVal f As Form)
FormToRun = f
End Sub

Public ReadOnly FormToRun As Form

Public Sub RunApp()
Application.Run(FormToRun)
End Sub

End Class

End Module



using System;

using System.Windows.Forms; // Add Project Reference to
System.Windows.Forms.dll
using System.Reflection;
using System.Threading;

namespace RunScenario // Must change this
{
class Program
{
static Form testForm = null;
static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance;
static int delay = 1500;

[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("\nStarting UI test scenario");

Console.WriteLine("\nLaunching WinApp under test");
string exePath = @"..\\..\\..\WinApp\bin\Debug\WinApp.exe";
testForm = LaunchApp(exePath, "WinApp.Form1");

Console.WriteLine("Moving form");
SetFormPropertyValue("Location", new System.Drawing.Point(200,200));
SetFormPropertyValue("Location", new System.Drawing.Point(500,500));

Console.WriteLine("Resizing form");
SetFormPropertyValue("Size", new System.Drawing.Size(600,600) );
SetFormPropertyValue("Size", new System.Drawing.Size(300,320) );

Console.WriteLine("\nSetting textBox1 to 'yellow'");
SetControlPropertyValue("textBox1", "Text", "yellow");

Console.WriteLine("Setting textBox1 to 'red'");
SetControlPropertyValue("textBox1", "Text", "red");

Console.WriteLine("Selecting comboBox1 to 'green'");
SetControlPropertyValue("comboBox1", "SelectedItem", "green");

Console.WriteLine("Selecting comboBox1 to 'blue'");
SetControlPropertyValue("comboBox1", "SelectedItem", "blue");

Console.WriteLine("Clicking button1");
InvokeMethod("button1_Click", new object[]{null, new EventArgs()} );

Console.WriteLine("\nChecking for state:\n 'red', 'blue', 'Result is
purple'");
bool pass = true;
if ((string)GetControlPropertyValue("textBox1", "Text") != "red") pass =
false;
if ((string)GetControlPropertyValue("comboBox1", "SelectedItem") !=
"blue") pass = false;
if ( !((ListBox.ObjectCollection)GetControlPropertyValue("listBox1",
"Items")).Contains("Result is purple")) pass = false;

if (pass) Console.WriteLine("\nUI test scenario result: PASS");
else Console.WriteLine("\nUI test scenario result: *FAIL*");

Console.WriteLine("\nClicking menu File->Exit in 5 seconds . . . ");
Thread.Sleep(3500);
InvokeMethod("menuItem4_Click", new object[] {null, new EventArgs()} );

Console.ReadLine();
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadLine();
}

} // Main()

static void SetControlPropertyValue(
string controlName, string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new
SetControlPropertyValueHandler(SetControlPropertyValue), new
object[]{controlName, propertyName, newValue});
return;
}
Type t1 = testForm.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(testForm);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName, flags);
pi.SetValue(ctrl, newValue, new object[0]);
}
delegate void SetControlPropertyValueHandler(string controlName, string
propertyName, object newValue);


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}
delegate void SetFormPropertyValueHandler(string propertyName, object
newValue);


static object GetControlPropertyValue(
string controlName, string propertyName)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
return testForm.Invoke(new
GetControlPropertyValueHandler(GetControlPropertyValue),new
object[]{controlName, propertyName});
}
Type t1 = testForm.GetType();
FieldInfo fi = t1.GetField(controlName, flags);
object ctrl = fi.GetValue(testForm);
Type t2 = ctrl.GetType();
PropertyInfo pi = t2.GetProperty(propertyName, flags);
return pi.GetValue(ctrl, new object[0]);
}
delegate object GetControlPropertyValueHandler(string controlName, string
propertyName);


static void InvokeMethod(string methodName, params object[] parms)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new InvokeMethodHandler(InvokeMethod), new
object[]{methodName, parms});
return;
}
Type t = testForm.GetType();
MethodInfo mi = t.GetMethod(methodName, flags);
mi.Invoke(testForm, parms);
}
delegate void InvokeMethodHandler(string methodName, params object []
parms);


static Form LaunchApp(string exePath, string formName)
{
Thread.Sleep(delay);
Assembly a = Assembly.LoadFrom(exePath);
Type formType = a.GetType(formName);
Form resultForm = (Form)a.CreateInstance(formType.FullName);
Thread t = new Thread(new ThreadStart(new AppState(resultForm).RunApp));
t.ApartmentState = ApartmentState.STA;
t.IsBackground = true;
t.Start();
return resultForm;
}

private class AppState
{
public AppState(Form f) { FormToRun = f; }
public readonly Form FormToRun;
public void RunApp()
{
Application.Run(FormToRun);
}
}

} // class
} // ns




--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

Peter Proost said:
Hi Ken that did it, thanks. I still have got an other problem that I thought
would go away if my first problem was fixed but I'll first try to solve it
myself and if I can't solve it I'll post it. Thanks again

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

Ken Tucker said:
Hi,

Since you are not actually using x for anything I would use nothing
instead

pi.SetValue(testForm, newValue, nothing)


Ken
------------------
Peter Proost said:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 
G

Guest

First of all, you need to start with a proper translation. From your longer
C# to VB sample, there were many points missed in the VB translation
(parameter arrays for instance).

Our Instant VB C# to VB converter produced the following VB equivalent:

Imports System
Imports System.Windows.Forms ' Add Project Reference to
System.Windows.Forms.dll
Imports System.Reflection
Imports System.Threading

Namespace RunScenario ' Must change this
Friend Class Program
Private Shared testForm As Form = Nothing
Private Shared flags As BindingFlags = BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance

Private Shared delay As Integer = 1500

<STAThread> _
Shared Sub Main(ByVal args As String())
Try
Console.WriteLine(Constants.vbLf & "Starting UI test scenario")

Console.WriteLine(Constants.vbLf & "Launching WinApp under test")
Dim exePath As String = "..\\..\\..\WinApp\bin\Debug\WinApp.exe"
testForm = LaunchApp(exePath, "WinApp.Form1")

Console.WriteLine("Moving form")
SetFormPropertyValue("Location", New System.Drawing.Point(200,200))
SetFormPropertyValue("Location", New System.Drawing.Point(500,500))

Console.WriteLine("Resizing form")
SetFormPropertyValue("Size", New System.Drawing.Size(600,600))
SetFormPropertyValue("Size", New System.Drawing.Size(300,320))

Console.WriteLine(Constants.vbLf & "Setting textBox1 to 'yellow'")
SetControlPropertyValue("textBox1", "Text", "yellow")

Console.WriteLine("Setting textBox1 to 'red'")
SetControlPropertyValue("textBox1", "Text", "red")

Console.WriteLine("Selecting comboBox1 to 'green'")
SetControlPropertyValue("comboBox1", "SelectedItem", "green")

Console.WriteLine("Selecting comboBox1 to 'blue'")
SetControlPropertyValue("comboBox1", "SelectedItem", "blue")

Console.WriteLine("Clicking button1")
InvokeMethod("button1_Click", New Object(){Nothing, New EventArgs()})

Console.WriteLine(Constants.vbLf & "Checking for state:" &
Constants.vbLf & " 'red', 'blue', 'Result is purple'")

Dim pass As Boolean = True
If CStr(GetControlPropertyValue("textBox1", "Text")) <> "red" Then
pass = False
End If
If CStr(GetControlPropertyValue("comboBox1", "SelectedItem")) <>
"blue" Then
pass = False
End If
If Not(CType(GetControlPropertyValue("listBox1", "Items"),
ListBox.ObjectCollection)).Contains("Result is purple") Then

pass = False
End If

If pass Then
Console.WriteLine(Constants.vbLf & "UI test scenario result: PASS")
Else
Console.WriteLine(Constants.vbLf & "UI test scenario result:
*FAIL*")
End If

Console.WriteLine(Constants.vbLf & "Clicking menu File->Exit in 5
seconds . . . ")
Thread.Sleep(3500)
InvokeMethod("menuItem4_Click", New Object() {Nothing, New
EventArgs()})

Console.ReadLine()
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
End Try

End Sub ' Main()

Private Shared Sub SetControlPropertyValue(ByVal controlName As String,
ByVal propertyName As String, ByVal newValue As Object)

If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetControlPropertyValueHandler(AddressOf
SetControlPropertyValue), New Object(){controlName, propertyName, newValue})

Return
End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
pi.SetValue(ctrl, newValue, New Object(){})
End Sub
Private Delegate Sub SetControlPropertyValueHandler(ByVal controlName As
String, ByVal propertyName As String, ByVal newValue As Object)

Private Shared Sub SetFormPropertyValue(ByVal propertyName As String,
ByVal newValue As Object)
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object(){propertyName, newValue})

Return
End If
Dim t As Type = testForm.GetType()
Dim pi As PropertyInfo = t.GetProperty(propertyName)
pi.SetValue(testForm, newValue, New Object(){})
End Sub
Private Delegate Sub SetFormPropertyValueHandler(ByVal propertyName As
String, ByVal newValue As Object)

Private Shared Function GetControlPropertyValue(ByVal controlName As
String, ByVal propertyName As String) As Object
If testForm.InvokeRequired Then
Thread.Sleep(delay)
Return testForm.Invoke(New GetControlPropertyValueHandler(AddressOf
GetControlPropertyValue),New Object(){controlName, propertyName})

End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
Return pi.GetValue(ctrl, New Object(){})
End Function
Private Delegate Function GetControlPropertyValueHandler(ByVal controlName
As String, ByVal propertyName As String) As Object

Private Shared Sub InvokeMethod(ByVal methodName As String, ParamArray
ByVal parms As Object())
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New InvokeMethodHandler(AddressOf InvokeMethod), New
Object(){methodName, parms})
Return
End If
Dim t As Type = testForm.GetType()
Dim mi As MethodInfo = t.GetMethod(methodName, flags)
mi.Invoke(testForm, parms)
End Sub
Private Delegate Sub InvokeMethodHandler(ByVal methodName As String,
ParamArray ByVal parms As Object())

Private Shared Function LaunchApp(ByVal exePath As String, ByVal formName
As String) As Form
Thread.Sleep(delay)
Dim a As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(exePath)
Dim formType As Type = a.GetType(formName)
Dim resultForm As Form = CType(a.CreateInstance(formType.FullName), Form)
Dim t As Thread = New Thread(New ThreadStart(New
AppState(resultForm).RunApp))
t.ApartmentState = ApartmentState.STA
t.IsBackground = True
t.Start()
Return resultForm
End Function

Private Class AppState
Public Sub New(ByVal f As Form)
FormToRun = f
End Sub
Public ReadOnly FormToRun As Form
Public Sub RunApp()
Application.Run(FormToRun)
End Sub
End Class

End Class ' class
End Namespace ' ns

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter



Peter Proost said:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 
P

Peter Proost

Thanks I'll try it on monday because I haven't got access to my development
machine for the rest of the day.

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

David Anton said:
First of all, you need to start with a proper translation. From your longer
C# to VB sample, there were many points missed in the VB translation
(parameter arrays for instance).

Our Instant VB C# to VB converter produced the following VB equivalent:

Imports System
Imports System.Windows.Forms ' Add Project Reference to
System.Windows.Forms.dll
Imports System.Reflection
Imports System.Threading

Namespace RunScenario ' Must change this
Friend Class Program
Private Shared testForm As Form = Nothing
Private Shared flags As BindingFlags = BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance

Private Shared delay As Integer = 1500

<STAThread> _
Shared Sub Main(ByVal args As String())
Try
Console.WriteLine(Constants.vbLf & "Starting UI test scenario")

Console.WriteLine(Constants.vbLf & "Launching WinApp under test")
Dim exePath As String = "..\\..\\..\WinApp\bin\Debug\WinApp.exe"
testForm = LaunchApp(exePath, "WinApp.Form1")

Console.WriteLine("Moving form")
SetFormPropertyValue("Location", New System.Drawing.Point(200,200))
SetFormPropertyValue("Location", New System.Drawing.Point(500,500))

Console.WriteLine("Resizing form")
SetFormPropertyValue("Size", New System.Drawing.Size(600,600))
SetFormPropertyValue("Size", New System.Drawing.Size(300,320))

Console.WriteLine(Constants.vbLf & "Setting textBox1 to 'yellow'")
SetControlPropertyValue("textBox1", "Text", "yellow")

Console.WriteLine("Setting textBox1 to 'red'")
SetControlPropertyValue("textBox1", "Text", "red")

Console.WriteLine("Selecting comboBox1 to 'green'")
SetControlPropertyValue("comboBox1", "SelectedItem", "green")

Console.WriteLine("Selecting comboBox1 to 'blue'")
SetControlPropertyValue("comboBox1", "SelectedItem", "blue")

Console.WriteLine("Clicking button1")
InvokeMethod("button1_Click", New Object(){Nothing, New EventArgs()})

Console.WriteLine(Constants.vbLf & "Checking for state:" &
Constants.vbLf & " 'red', 'blue', 'Result is purple'")

Dim pass As Boolean = True
If CStr(GetControlPropertyValue("textBox1", "Text")) <> "red" Then
pass = False
End If
If CStr(GetControlPropertyValue("comboBox1", "SelectedItem")) <>
"blue" Then
pass = False
End If
If Not(CType(GetControlPropertyValue("listBox1", "Items"),
ListBox.ObjectCollection)).Contains("Result is purple") Then

pass = False
End If

If pass Then
Console.WriteLine(Constants.vbLf & "UI test scenario result: PASS")
Else
Console.WriteLine(Constants.vbLf & "UI test scenario result:
*FAIL*")
End If

Console.WriteLine(Constants.vbLf & "Clicking menu File->Exit in 5
seconds . . . ")
Thread.Sleep(3500)
InvokeMethod("menuItem4_Click", New Object() {Nothing, New
EventArgs()})

Console.ReadLine()
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
End Try

End Sub ' Main()

Private Shared Sub SetControlPropertyValue(ByVal controlName As String,
ByVal propertyName As String, ByVal newValue As Object)

If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetControlPropertyValueHandler(AddressOf
SetControlPropertyValue), New Object(){controlName, propertyName, newValue})

Return
End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
pi.SetValue(ctrl, newValue, New Object(){})
End Sub
Private Delegate Sub SetControlPropertyValueHandler(ByVal controlName As
String, ByVal propertyName As String, ByVal newValue As Object)

Private Shared Sub SetFormPropertyValue(ByVal propertyName As String,
ByVal newValue As Object)
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object(){propertyName, newValue})

Return
End If
Dim t As Type = testForm.GetType()
Dim pi As PropertyInfo = t.GetProperty(propertyName)
pi.SetValue(testForm, newValue, New Object(){})
End Sub
Private Delegate Sub SetFormPropertyValueHandler(ByVal propertyName As
String, ByVal newValue As Object)

Private Shared Function GetControlPropertyValue(ByVal controlName As
String, ByVal propertyName As String) As Object
If testForm.InvokeRequired Then
Thread.Sleep(delay)
Return testForm.Invoke(New GetControlPropertyValueHandler(AddressOf
GetControlPropertyValue),New Object(){controlName, propertyName})

End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
Return pi.GetValue(ctrl, New Object(){})
End Function
Private Delegate Function GetControlPropertyValueHandler(ByVal controlName
As String, ByVal propertyName As String) As Object

Private Shared Sub InvokeMethod(ByVal methodName As String, ParamArray
ByVal parms As Object())
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New InvokeMethodHandler(AddressOf InvokeMethod), New
Object(){methodName, parms})
Return
End If
Dim t As Type = testForm.GetType()
Dim mi As MethodInfo = t.GetMethod(methodName, flags)
mi.Invoke(testForm, parms)
End Sub
Private Delegate Sub InvokeMethodHandler(ByVal methodName As String,
ParamArray ByVal parms As Object())

Private Shared Function LaunchApp(ByVal exePath As String, ByVal formName
As String) As Form
Thread.Sleep(delay)
Dim a As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(exePath)
Dim formType As Type = a.GetType(formName)
Dim resultForm As Form = CType(a.CreateInstance(formType.FullName), Form)
Dim t As Thread = New Thread(New ThreadStart(New
AppState(resultForm).RunApp))
t.ApartmentState = ApartmentState.STA
t.IsBackground = True
t.Start()
Return resultForm
End Function

Private Class AppState
Public Sub New(ByVal f As Form)
FormToRun = f
End Sub
Public ReadOnly FormToRun As Form
Public Sub RunApp()
Application.Run(FormToRun)
End Sub
End Class

End Class ' class
End Namespace ' ns

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter



Peter Proost said:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 
P

Peter Proost

Hi first of all thanks again for your code, I also posted something last
friday but it doesn't show in my newsreader but it does in Google qo I don't
now if you've read that but I finaly got the change to test your code and it
also doesn't work, it errors out at 2 lines of code:

Private Delegate Sub InvokeMethodHandler(ByVal methodName As String,
ParamArray ByVal parms As Object())
==>Event or delegate declaration cannot have Optional or ParamArray
parameters.

Dim t As Thread = New Thread(New ThreadStart(New
AppState(resultForm).RunApp))
==>'System.Threading.ThreadStart' is a delegate type. Delegate construction
permits only a single AddressOf expression as an argument list. Often an
AddressOf expression can be used instead of a delegate construction.

Greetz Peter


--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

David Anton said:
First of all, you need to start with a proper translation. From your longer
C# to VB sample, there were many points missed in the VB translation
(parameter arrays for instance).

Our Instant VB C# to VB converter produced the following VB equivalent:

Imports System
Imports System.Windows.Forms ' Add Project Reference to
System.Windows.Forms.dll
Imports System.Reflection
Imports System.Threading

Namespace RunScenario ' Must change this
Friend Class Program
Private Shared testForm As Form = Nothing
Private Shared flags As BindingFlags = BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance

Private Shared delay As Integer = 1500

<STAThread> _
Shared Sub Main(ByVal args As String())
Try
Console.WriteLine(Constants.vbLf & "Starting UI test scenario")

Console.WriteLine(Constants.vbLf & "Launching WinApp under test")
Dim exePath As String = "..\\..\\..\WinApp\bin\Debug\WinApp.exe"
testForm = LaunchApp(exePath, "WinApp.Form1")

Console.WriteLine("Moving form")
SetFormPropertyValue("Location", New System.Drawing.Point(200,200))
SetFormPropertyValue("Location", New System.Drawing.Point(500,500))

Console.WriteLine("Resizing form")
SetFormPropertyValue("Size", New System.Drawing.Size(600,600))
SetFormPropertyValue("Size", New System.Drawing.Size(300,320))

Console.WriteLine(Constants.vbLf & "Setting textBox1 to 'yellow'")
SetControlPropertyValue("textBox1", "Text", "yellow")

Console.WriteLine("Setting textBox1 to 'red'")
SetControlPropertyValue("textBox1", "Text", "red")

Console.WriteLine("Selecting comboBox1 to 'green'")
SetControlPropertyValue("comboBox1", "SelectedItem", "green")

Console.WriteLine("Selecting comboBox1 to 'blue'")
SetControlPropertyValue("comboBox1", "SelectedItem", "blue")

Console.WriteLine("Clicking button1")
InvokeMethod("button1_Click", New Object(){Nothing, New EventArgs()})

Console.WriteLine(Constants.vbLf & "Checking for state:" &
Constants.vbLf & " 'red', 'blue', 'Result is purple'")

Dim pass As Boolean = True
If CStr(GetControlPropertyValue("textBox1", "Text")) <> "red" Then
pass = False
End If
If CStr(GetControlPropertyValue("comboBox1", "SelectedItem")) <>
"blue" Then
pass = False
End If
If Not(CType(GetControlPropertyValue("listBox1", "Items"),
ListBox.ObjectCollection)).Contains("Result is purple") Then

pass = False
End If

If pass Then
Console.WriteLine(Constants.vbLf & "UI test scenario result: PASS")
Else
Console.WriteLine(Constants.vbLf & "UI test scenario result:
*FAIL*")
End If

Console.WriteLine(Constants.vbLf & "Clicking menu File->Exit in 5
seconds . . . ")
Thread.Sleep(3500)
InvokeMethod("menuItem4_Click", New Object() {Nothing, New
EventArgs()})

Console.ReadLine()
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
Console.ReadLine()
End Try

End Sub ' Main()

Private Shared Sub SetControlPropertyValue(ByVal controlName As String,
ByVal propertyName As String, ByVal newValue As Object)

If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetControlPropertyValueHandler(AddressOf
SetControlPropertyValue), New Object(){controlName, propertyName, newValue})

Return
End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
pi.SetValue(ctrl, newValue, New Object(){})
End Sub
Private Delegate Sub SetControlPropertyValueHandler(ByVal controlName As
String, ByVal propertyName As String, ByVal newValue As Object)

Private Shared Sub SetFormPropertyValue(ByVal propertyName As String,
ByVal newValue As Object)
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object(){propertyName, newValue})

Return
End If
Dim t As Type = testForm.GetType()
Dim pi As PropertyInfo = t.GetProperty(propertyName)
pi.SetValue(testForm, newValue, New Object(){})
End Sub
Private Delegate Sub SetFormPropertyValueHandler(ByVal propertyName As
String, ByVal newValue As Object)

Private Shared Function GetControlPropertyValue(ByVal controlName As
String, ByVal propertyName As String) As Object
If testForm.InvokeRequired Then
Thread.Sleep(delay)
Return testForm.Invoke(New GetControlPropertyValueHandler(AddressOf
GetControlPropertyValue),New Object(){controlName, propertyName})

End If
Dim t1 As Type = testForm.GetType()
Dim fi As FieldInfo = t1.GetField(controlName, flags)
Dim ctrl As Object = fi.GetValue(testForm)
Dim t2 As Type = ctrl.GetType()
Dim pi As PropertyInfo = t2.GetProperty(propertyName, flags)
Return pi.GetValue(ctrl, New Object(){})
End Function
Private Delegate Function GetControlPropertyValueHandler(ByVal controlName
As String, ByVal propertyName As String) As Object

Private Shared Sub InvokeMethod(ByVal methodName As String, ParamArray
ByVal parms As Object())
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New InvokeMethodHandler(AddressOf InvokeMethod), New
Object(){methodName, parms})
Return
End If
Dim t As Type = testForm.GetType()
Dim mi As MethodInfo = t.GetMethod(methodName, flags)
mi.Invoke(testForm, parms)
End Sub
Private Delegate Sub InvokeMethodHandler(ByVal methodName As String,
ParamArray ByVal parms As Object())

Private Shared Function LaunchApp(ByVal exePath As String, ByVal formName
As String) As Form
Thread.Sleep(delay)
Dim a As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFrom(exePath)
Dim formType As Type = a.GetType(formName)
Dim resultForm As Form = CType(a.CreateInstance(formType.FullName), Form)
Dim t As Thread = New Thread(New ThreadStart(New
AppState(resultForm).RunApp))
t.ApartmentState = ApartmentState.STA
t.IsBackground = True
t.Start()
Return resultForm
End Function

Private Class AppState
Public Sub New(ByVal f As Form)
FormToRun = f
End Sub
Public ReadOnly FormToRun As Form
Public Sub RunApp()
Application.Run(FormToRun)
End Sub
End Class

End Class ' class
End Namespace ' ns

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter



Peter Proost said:
Hi I got this piece of c# code which runs ok but now I translated it to
vb.net (see bottom of post for both pieces)
and it keeps erroring out at pi.SetValue(testForm, newValue, x)
with this error:

System.Reflection.TargetParameterCountException: Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object
value, Object[] index)
at uiTester.Module1.SetFormPropertyValue(String propertyName, Object
newValue)

But I can't figure out why it throws an error so some help would be much
appreciated :)

Thanks in advance

Greetz Peter


static void SetFormPropertyValue(string propertyName, object newValue)
{
if (testForm.InvokeRequired)
{
Thread.Sleep(delay);
testForm.Invoke(new SetFormPropertyValueHandler(SetFormPropertyValue),
new object[]{propertyName, newValue});
return;
}
Type t = testForm.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(testForm, newValue, new object[0]);
}

Private Sub SetFormPropertyValue(ByVal propertyName As String, ByVal
newValue As Object)
Try
If testForm.InvokeRequired Then
Thread.Sleep(delay)
testForm.Invoke(New SetFormPropertyValueHandler(AddressOf
SetFormPropertyValue), New Object() _{propertyName, newValue})

Return
End If


Dim t As Type = testForm.GetType
Dim pi As PropertyInfo = t.GetProperty(propertyName)
Dim x(0) As Object

pi.SetValue(testForm, newValue, x)

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top