Muna

Hi

I want to convert this code segment to VB.Net but none of the converters worked.

using System;

using System.Windows.Forms;

using System.Threading;

using System.Reflection;

private static Thread m_SplashThread = null;
 private static Form m_SplashForm = null;

Dim splashFormType as Form ' actually splashFormType is refrence to form already added to application


 m_SplashThread = new Thread(new ThreadStart(delegate(){createForm(splashFormType); Application.Run(m_SplashForm);}));

Private static voic createForm()
{
1 Create a new instance of form
2 set properties
3 assign new instance to form m_SplashForm
}

How it can be converted any way

Please comments

Thanks 




Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Post an actual working C# code sample and I'll convert it.

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

I would be greatly thankfull for the favour

I cant attach code sample to this post...dont know if can be attached some rar or zip file here

can you tell me your email address or how to attach code if possible

Thanks






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Just isolate enough code to demonstrate the issue and post it here.






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

Actual C# code

using System;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace SplashScreen
{
public class Splasher
{
private static Form m_SplashForm = null;
private static ISplashForm m_SplashInterface = null;
private static Thread m_SplashThread = null;
private static string m_TempStatus = string.Empty;


public static void Show(Type splashFormType)
{
if (m_SplashThread != null)
return;
if (splashFormType == null)
{
throw (new Exception("splashFormType is null"));
}

m_SplashThread = new Thread(new ThreadStart(delegate(){CreateInstance(splashFormType); Application.Run(m_SplashForm);}));


m_SplashThread.IsBackground = true;
m_SplashThread.SetApartmentState(ApartmentState.STA);
m_SplashThread.Start();
}

public static string Status
{
set
{
if (m_SplashInterface == null || m_SplashForm == null)
{
m_TempStatus = value;
return;
}
m_SplashForm.Invoke(
new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),
new object[] { value }
);
}

}

public static void Close()
{
if (m_SplashThread == null || m_SplashForm == null) return;

try
{
m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));
}
catch (Exception)
{
}
m_SplashThread = null;
m_SplashForm = null;
}

private static void CreateInstance(Type FormType)
{

object obj = FormType.InvokeMember(null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
m_SplashForm = obj as Form;
m_SplashInterface = obj as ISplashForm;
if (m_SplashForm == null)
{
throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));
}
if (m_SplashInterface == null)
{
throw (new Exception("must implement interface ISplashForm"));
}

if (!string.IsNullOrEmpty(m_TempStatus))
m_SplashInterface.SetStatusInfo(m_TempStatus);
}


private delegate void SplashStatusChangedHandle(string NewStatusInfo);

}
}

VB.Net code

Imports Microsoft.VisualBasic

Imports System

Imports System.Windows.Forms

Imports System.Threading

Imports System.Reflection

Namespace SplashScreen

Public Class Splasher

Private Shared m_SplashForm As Form = Nothing

Private Shared m_SplashInterface As ISplashForm = Nothing

Private Shared m_SplashThread As Thread = Nothing

Private Shared m_TempStatus As String = String.Empty

Private Delegate Sub SplashStatusChangedHandle(ByVal NewStatusInfo As String)

Public Shared WriteOnly Property Status() As String

Set(ByVal value As String)

If ((Splasher.m_SplashInterface Is Nothing) OrElse (Splasher.m_SplashForm Is Nothing)) Then

Splasher.m_TempStatus = value

Else

Splasher.m_SplashForm.Invoke(New SplashStatusChangedHandle(AddressOf AnonymousMethodDelegate), New Object() {value})

End If

End Set

End Property

Public Shared Sub Show(ByVal splashFormType As Type)

If Not m_SplashThread Is Nothing Then

Return

End If

If splashFormType Is Nothing Then

Throw (New Exception("splashFormType is null"))

End If

m_SplashThread = New Thread(New ThreadStart(AddressOf AnonymousMethodDelegate()

CreateInstance(splashFormType)

Application.Run(m_SplashForm)

)

m_SplashThread.IsBackground = True

m_SplashThread.SetApartmentState(ApartmentState.STA)

m_SplashThread.Start()

End Sub

Shared Sub AnonymousMethodDelegate(ByVal NewStatusInfo As String)

m_SplashInterface.SetStatusInfo(NewStatusInfo)

End Sub

Public Shared Sub Close()

If m_SplashThread Is Nothing OrElse m_SplashForm Is Nothing Then

Return

End If

Try

m_SplashForm.Invoke(New MethodInvoker(AddressOf m_SplashForm.Close))

Catch e1 As Exception

End Try

m_SplashThread = Nothing

m_SplashForm = Nothing

End Sub

Private Shared Sub CreateInstance(ByVal FormType As Type)

Dim obj As Object = FormType.InvokeMember(Nothing, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.CreateInstance, Nothing, Nothing, Nothing)

m_SplashForm = TryCast(obj, Form)

m_SplashInterface = TryCast(obj, ISplashForm)

If m_SplashForm Is Nothing Then

Throw (New Exception("Splash Screen must inherit from System.Windows.Forms.Form"))

End If

If m_SplashInterface Is Nothing Then

Throw (New Exception("must implement interface ISplashForm"))

End If

If (Not String.IsNullOrEmpty(m_TempStatus)) Then

m_SplashInterface.SetStatusInfo(m_TempStatus)

End If

End Sub

End Class

End Namespace

The bold red marked code is creating problem.

It says deletegate method has not specified argument But when i pass argue then says Threadstart accespts only delegate without parentheses

I dont know how to convert this C# line of code to VB.NEt

m_SplashThread = new Thread(new ThreadStart(delegate(){CreateInstance(splashFormType); Application.Run(m_SplashForm);}));

Hopes it will give some idea

Thanks






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Sorry, I should have spotted the anonymous method (...delegate()...) from your first post.

Instant VB will not convert anonymous methods if they reference 'outer' variables or parameters. The best Instant VB can do is the following:

Imports System
Imports System.Windows.Forms
Imports System.Threading
Imports System.Reflection

Namespace SplashScreen
Public Class Splasher
Private Shared m_SplashForm As Form = Nothing
Private Shared m_SplashInterface As ISplashForm = Nothing
Private Shared m_SplashThread As Thread = Nothing
Private Shared m_TempStatus As String = String.Empty


Public Shared Sub Show(ByVal splashFormType As Type)
If Not m_SplashThread Is Nothing Then
Return
End If
If splashFormType Is Nothing Then
Throw (New Exception("splashFormType is null"))
End If

'TODO: INSTANT VB TODO TASK: Anonymous methods are not converted by Instant VB if the parameter list is omitted or local variables of the outer method are referenced within the anonymous method:
m_SplashThread = New Thread(New ThreadStart(delegate()
CreateInstance(splashFormType)
Application.Run(m_SplashForm)
))


m_SplashThread.IsBackground = True
m_SplashThread.SetApartmentState(ApartmentState.STA)
m_SplashThread.Start()
End Sub

Public Shared WriteOnly Property Status() As String
Set
If m_SplashInterface Is Nothing OrElse m_SplashForm Is Nothing Then
m_TempStatus = Value
Return
End If
m_SplashForm.Invoke(New SplashStatusChangedHandle(AddressOf AnonymousMethod1), New Object() { Value }
)
End Set

End Property
Private Sub AnonymousMethod1(ByVal str As String)
m_SplashInterface.SetStatusInfo(str)
End Sub

Public Shared Sub Close()
If m_SplashThread Is Nothing OrElse m_SplashForm Is Nothing Then
Return
End If

Try
m_SplashForm.Invoke(New MethodInvoker(AddressOf m_SplashForm.Close))
Catch e1 As Exception
End Try
m_SplashThread = Nothing
m_SplashForm = Nothing
End Sub

Private Shared Sub CreateInstance(ByVal FormType As Type)

Dim obj As Object = FormType.InvokeMember(Nothing, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
m_SplashForm = TryCast(obj, Form)
m_SplashInterface = TryCast(obj, ISplashForm)
If m_SplashForm Is Nothing Then
Throw (New Exception("Splash Screen must inherit from System.Windows.Forms.Form"))
End If
If m_SplashInterface Is Nothing Then
Throw (New Exception("must implement interface ISplashForm"))
End If

If (Not String.IsNullOrEmpty(m_TempStatus)) Then
m_SplashInterface.SetStatusInfo(m_TempStatus)
End If
End Sub


Private Delegate Sub SplashStatusChangedHandle(ByVal NewStatusInfo As String)

End Class
End Namespace

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

any idea for converting this one line

Thanks






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Yes, if you change your original code so that you set a new private variable m_splashFormType and reference this new variable in the anonymous method instead of the local variable, Instant VB converts it to the following:

Imports System
Imports System.Windows.Forms
Imports System.Threading
Imports System.Reflection

Namespace SplashScreen
Public Class Splasher
Private Shared m_SplashForm As Form = Nothing
Private Shared m_SplashInterface As ISplashForm = Nothing
Private Shared m_SplashThread As Thread = Nothing
Private Shared m_TempStatus As String = String.Empty
Private Shared m_splashFormType As Type = Nothing

Public Shared Sub Show(ByVal splashFormType As Type)
If Not m_SplashThread Is Nothing Then
Return
End If
If splashFormType Is Nothing Then
Throw (New Exception("splashFormType is null"))
End If

m_splashFormType = splashFormType
m_SplashThread = New Thread(New ThreadStart(AddressOf AnonymousMethod1))

m_SplashThread.IsBackground = True
m_SplashThread.SetApartmentState(ApartmentState.STA)
m_SplashThread.Start()
End Sub
Private Sub AnonymousMethod1()
CreateInstance(m_splashFormType)
Application.Run(m_SplashForm)
End Sub

Public Shared WriteOnly Property Status() As String
Set
If m_SplashInterface Is Nothing OrElse m_SplashForm Is Nothing Then
m_TempStatus = Value
Return
End If
m_SplashForm.Invoke(New SplashStatusChangedHandle(AddressOf AnonymousMethod2), New Object() { Value }
)
End Set

End Property
Private Sub AnonymousMethod2(ByVal str As String)
m_SplashInterface.SetStatusInfo(str)
End Sub

Public Shared Sub Close()
If m_SplashThread Is Nothing OrElse m_SplashForm Is Nothing Then
Return
End If

Try
m_SplashForm.Invoke(New MethodInvoker(AddressOf m_SplashForm.Close))
Catch e1 As Exception
End Try
m_SplashThread = Nothing
m_SplashForm = Nothing
End Sub

Private Shared Sub CreateInstance(ByVal FormType As Type)

Dim obj As Object = FormType.InvokeMember(Nothing, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
m_SplashForm = TryCast(obj, Form)
m_SplashInterface = TryCast(obj, ISplashForm)
If m_SplashForm Is Nothing Then
Throw (New Exception("Splash Screen must inherit from System.Windows.Forms.Form"))
End If
If m_SplashInterface Is Nothing Then
Throw (New Exception("must implement interface ISplashForm"))
End If

If (Not String.IsNullOrEmpty(m_TempStatus)) Then
m_SplashInterface.SetStatusInfo(m_TempStatus)
End If
End Sub

Private Delegate Sub SplashStatusChangedHandle(ByVal NewStatusInfo As String)

End Class
End Namespace

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

Idea seems feasible .. so far I got this message

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class

I m still checking to make it work .. please can you check it further .. if you want here is the actual code example project

http://www.codeproject.com/useritems/usesplashscreen.asp

The author has converted some of the code but did not this issue so far.

Thanks






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Add a "Shared" modifier to "AnonymousMethod1" and "AnonymousMethod2".






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

Hi David Anton

Thank You , It is working . .. Great Job...






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

Thank you for helping us find that bug (Instant VB should be adding the "Shared" modifier on our methods we add to substitute for anonymous methods when the containing method is Shared).

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

Muna

Instant VB , no doubt , is very good tool but at this time It just was not able to help me out.

Hopes it would be updated to deal in such situations.

Thanks for being so helpful..






Re: Visual C# Language How to convert Parameterized Delegate from C# to VB.Net?

David Anton

It did provide that last translation which worked (except for the 'Shared' modifier - this is fixed in the latest build). However, it did require modifying the original C# anonymous method, before conversion, to not access the local variable. This condition for manual adjustment will remain until VB has anonymous methods (possibly in VB9 ). It is possible to include logic to handle the case with 'captured variables', but the converted code would have to be restructured too drastically.

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter