473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C# code errors out in vb.net translation

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(Obj ect 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
Nov 21 '05 #1
5 2714
Hi,

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

pi.SetValue(testForm, newValue, nothing)
Ken
------------------
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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(Obj ect 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

Nov 21 '05 #2
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 [MVP]" <vb***@bellsouth.net> schreef in bericht
news:e2**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

pi.SetValue(testForm, newValue, nothing)
Ken
------------------
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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(Obj ect 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


Nov 21 '05 #3
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.G etType(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)GetControlPropertyValu e("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(SetControlPropertyV alue), 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(GetControlPropertyV alue),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" <pp*****@nospam.hotmail.com> schreef in bericht
news:uG**************@TK2MSFTNGP12.phx.gbl...
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 [MVP]" <vb***@bellsouth.net> schreef in bericht
news:e2**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

nothing
instead

pi.SetValue(testForm, newValue, nothing)
Ken
------------------
"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:uL**************@TK2MSFTNGP12.phx.gbl...
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(Obj ect 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



Nov 21 '05 #4
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" wrote:
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(Obj ect 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

Nov 21 '05 #5
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" <Da********@discussions.microsoft.com> schreef in bericht
news:91**********************************@microsof t.com...
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" wrote:
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(Obj ect 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

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave | last post by:
Hello all, I have one method of a class calling another method (both private, though that shouldn't matter). The method and its enclosing class are declared in a .h file and the method is...
3
by: Peng Yu | last post by:
Hi, I'm trying to define inline_test::test() to be inline. But I always got errors. I know that if I define inline_test::test() in inline_test.h, there are no errors. But I still would rather...
30
by: prasanna | last post by:
i will be very thankful if you sent all the errors and warnings regarding to the language C thank you
4
by: DELESTRE Christophe | last post by:
I’m sorry to disturb you but I have a problem on .NET development, and I’m need some help to resolve it if it’s possible. I have an aspx page with src property (no dll for my web...
2
by: JP Ogden | last post by:
I have a some .NET code that I need to translate into VB. Does anyone know how to do this? If so, thanks in advance! Here is the code. I only need the translation code between START...
23
by: Abhi | last post by:
Hi.. I wanted the C source code in machine readable format for the book "Numerical Recipes in C". I got hold of the pdf version of the book somehow. Does anyone have the complete C code of the...
5
by: himadri | last post by:
Hi Everybody, Please have a look at the code snippet below ---- #include <stdio.h> int a; int a; int main()
2
by: Markus Dehmann | last post by:
What to do if an external library is header-files-only (but you have to use it), and you get lots of linker errors? You will necessarily get linker errors "multiple definition of ..." if you try...
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.