2016-06-29 9 views
0

So hatte ich diesen alten VB-Code in WinForms, die ich mit Telerik Online Converter in C# konvertieren wollte.Fehler Konvertieren von VB-Code in C#

Nach der Konvertierung habe ich ein Problem, das nicht behoben wird.

Der VB-Code ist hier

Private Sub PlotLensProfileThreadFunction() 
    Dim ErrorFlag As ErrorFlagType = InitErrorFlag() 
    Dim InMedia As Graphics = Me.PicLensPlot.CreateGraphics 
    Dim PlotRef As New PlotLensProfileThread() 
    Try 
     PlotRef.ThreadInGrphRef = InMedia 
     PlotRef.ThreadInConcavePaths = ConcavePaths 
     PlotRef.ThreadInConvexPaths = ConvexPaths 
     PlotRef.ThreadPlotOptions = PlotOptions.ProfileView 
     PlotRef.ThreadStepSixData = JobData.StepSixData 
     PlotRef.ThreadStepFiveData = JobData.StepFiveData 
     PlotRef.ThreadStepFourData = JobData.StepFourData 
     PlotRef.ThreadStepThreeData = JobData.StepThreeData 
     PlotRef.ThreadStepTwoData = JobData.StepTwoData 
     PlotRef.ErrorFlag = ErrorFlag 
     PlotRef.MeridianConcave = MeridianConcave 
     PlotRef.MeridianEdge = MeridianEdge 
     PlotRef.MeridianConvex = MeridianConvex 
     ZedGraphControl1.GraphPane.CurveList.Clear() 
     PlotRef.zedGraphType = ZedGraphControl1 
     PlotRef.PlotLensProfile() 
    Catch e As System.Threading.ThreadAbortException 
     System.Threading.Thread.ResetAbort() 
    End Try 
End Sub 

Der konvertierte C# -Code ist hier

private void PlotLensProfileThreadFunction() 
{ 
    Mold_Power_Suite.Model.FrontEndStructures.ErrorFlagType ErrorFlag =FrontEndStructures. InitErrorFlag(); 
    Graphics InMedia = this.PicLensPlot.CreateGraphics(); 



    PlotLensProfileThread PlotRef = new PlotLensProfileThread(); 
    // var PlotRef = PlotLensProfileThread; 
    try 
    { 
     PlotRef.ThreadInGrphRef = InMedia; 
     PlotRef.ThreadInConcavePaths = ConcavePaths; 
     PlotRef.ThreadInConvexPaths = ConvexPaths; 
     PlotRef.ThreadPlotOptions = PlotOptions.ProfileView; 
     PlotRef.ThreadStepSixData = JobData.StepSixData; 
     PlotRef.ThreadStepFiveData = JobData.StepFiveData; 
     PlotRef.ThreadStepFourData = JobData.StepFourData; 
     PlotRef.ThreadStepThreeData = JobData.StepThreeData; 
     PlotRef.ThreadStepTwoData = JobData.StepTwoData; 
     PlotRef.ErrorFlag = ErrorFlag; 
     PlotRef.MeridianConcave = MeridianConcave; 
     PlotRef.MeridianEdge = MeridianEdge; 
     PlotRef.MeridianConvex = MeridianConvex; 
     ZedGraphControl1.GraphPane.CurveList.Clear(); 
     PlotRef.zedGraphType = ZedGraphControl1; 
     PlotRef.PlotLensProfile(); 
    } 
    catch (System.Threading.ThreadAbortException e) 
    { 
     System.Threading.Thread.ResetAbort(); 
    } 
} 

Die VB-Klasse die Definition einiger Threads hat, die in der Funktion verwendet werden

Public Class FrmSoftJobProcess 
    Dim PlotLensProfileThreadDelegate As New ThreadStart(AddressOf PlotLensProfileThreadFunction) 
    Dim PlotLensProfileThread As New Thread(PlotLensProfileThreadDelegate) 
    Dim PlotLensPlanThreadDelegate As New ThreadStart(AddressOf PlotLensPlanThreadFunction) 
    Dim PlotLensPlanThread As New Thread(PlotLensPlanThreadDelegate) 

    Dim MiniFilename As String 

    Dim JobData As SoftJobDataType 
    Dim MeridianConvex As Integer 
    Dim MeridianConcave As Integer 
    Dim MeridianEdge As Integer 
    Dim NumberOfTabs As Integer 

    Dim ConcavePaths As PSMG.Minifile.MinifileDocument 
    Dim ConvexPaths As PSMG.Minifile.MinifileDocument 

    Dim DisplayPlot As FrontEndStructures.DisplayPlotType 
    Dim PlotOptions As PlotOptionsType 

    Dim WithEvents MaterialFrm As FrmAddMaterial 
    Dim WithEvents ConcaveToricDesignFrm As frmBCMulticurveToricdesign 
    Dim WithEvents ConcaveSphereDesignFrm As frmBCMulticurveSphereDesign 
    Dim WithEvents DesignFrm As frmSoftConvexdesign 
    Dim WithEvents NewFrm As FrmMarkerDefinition 

    Dim WithEvents TabPageChangeTracker As New TrackChanges 

Der konvertierte C# -Code ist

public partial class FrmSoftJobProcess : Form 
{ 

    public FrmSoftJobProcess() 
    { 
     InitializeComponent(); 
     Load += FrmJobProcess_Load; 
     FormClosing += FrmJobProcess_FormClosing; 
    } 

    frmMain mainForm = new frmMain(); 
    ThreadStart PlotLensProfileThreadDelegate = new ThreadStart(new FrmSoftJobProcess(). PlotLensProfileThreadFunction); //This has to be revisited again 
    Thread PlotLensProfileThread = new Thread(new FrmSoftJobProcess(). PlotLensProfileThreadDelegate); 
    ThreadStart PlotLensPlanThreadDelegate = new ThreadStart(new FrmSoftJobProcess().PlotLensPlanThreadFunction); 

    Thread PlotLensPlanThread = new Thread(new FrmSoftJobProcess().PlotLensPlanThreadDelegate); 

    string MiniFilename; 
    Mold_Power_Suite.Model.FrontEndStructures.SoftJobDataType JobData; 
    int MeridianConvex; 
    int MeridianConcave; 
    int MeridianEdge; 

    int NumberOfTabs; 
    PSMG.Minifile.MinifileDocument ConcavePaths; 

    PSMG.Minifile.MinifileDocument ConvexPaths; 
    FrontEndStructures.DisplayPlotType DisplayPlot; 

Das Problem ist jetzt, dass ich nicht in der Lage bin PlotLensProfileThread und InRef auf dieser Aussage PlotLensProfileThread PlotRef = new PlotLensProfileThread() zuzugreifen;. Der Compiler löst den Fehler PlotLensProfileThread 'ist ein' Feld 'aber wird wie ein' Typ 'verwendet

Kann jemand helfen?

+0

in VB.net ist der Code, ein Verfahren, mit dem ‚AddressOf‘ aufrufen, die ein Zeiger auf die Startposition von die Funktion. Es ist nicht dasselbe wie PlotLensProfileThread(), wo der Compiler in C# einen Aufruf der Methode erstellt. Siehe Webseite: http://www.codeproject.com/Articles/19911/Dynamic-Invoke-A-Method-Given-Strings-with-Met – jdweng

+0

PlotLensProfileThread ist keine Funktion. So fett, wie ich es verstehe, ist es eine Variable, die in der ThreadStart-Klasse verwendet wird, um ein Objekt zu erzeugen. Thread PlotLensPlanThread = new Thread (new FrmSoftJobProcess(). PlotLensPlanThreadDelegate); – Apoorv

+0

Der Vb-Code sagt: "AddressOf PlotLensProfileThreadFunction" – jdweng

Antwort

0

Im Gegensatz zu VB in C# kann ein Instanzfeld nicht auf andere Instanzmitglieder im Initialisierer der Deklaration verweisen. Sie haben 4 Felder in Ihrer Klasse, die genau das tun und die Work-arounds, die Sie versucht haben, graben Sie tiefer in Schwierigkeiten. Die übliche korrekte Umgehung besteht darin, eine Initialisierungsmethode von Ihren Konstruktoren aufzurufen. Der konvertierte Code (nur die ersten 4 Felder Ihrer Klasse das Problem zu lokalisieren) ist:

public class FrmSoftJobProcess 
{ 
    //use a flag to prevent duplicate initialization for the case of chained constructor calls: 
    private bool InstanceFieldsInitialized = false; 

    public FrmSoftJobProcess() 
    { 
     if (!InstanceFieldsInitialized) 
     { 
      InitializeInstanceFields(); 
      InstanceFieldsInitialized = true; 
     } 
    } 

    private void InitializeInstanceFields() 
    { 
     PlotLensProfileThreadDelegate = new ThreadStart(PlotLensProfileThreadFunction); 
     PlotLensProfileThread = new Thread(PlotLensProfileThreadDelegate); 
     PlotLensPlanThreadDelegate = new ThreadStart(PlotLensPlanThreadFunction); 
     PlotLensPlanThread = new Thread(PlotLensPlanThreadDelegate); 
    } 

    private ThreadStart PlotLensProfileThreadDelegate; 
    private Thread PlotLensProfileThread; 
    private ThreadStart PlotLensPlanThreadDelegate; 
    private Thread PlotLensPlanThread; 
} 
+0

Ich habe meinen Code aktualisiert. Ich hoffe auf diese Weise sollte es für mich funktionieren. Ich habe noch ein Problem, würde gerne in Ihrer Hilfe suchen – Apoorv

+0

http://stackoverflow.com/questions/38095919/error-accessing-threadstart-object-inside-a-funktion können Sie hier nachsehen? – Apoorv

+0

@Apoorv: Entschuldigung, aber ich habe keine Lösung dafür. –