簡體   English   中英

將VB代碼轉換為C#時出錯

[英]Error Converting VB code to C#

所以我在WinForms中有這個舊的VB代碼,我想使用Telerik Online Converter將其轉換為C#。

轉換后,我遇到的問題尚未得到解決。

VB代碼在這里

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

轉換后的C#代碼在這里

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();
    }
}

VB類具有函數中正在使用的一些線程的定義

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

轉換后的C#代碼為

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;

現在的問題是,我無法在此語句上訪問PlotLensProfileThreadInRef PlotLensProfileThread PlotRef = new PlotLensProfileThread(); 編譯器拋出錯誤PlotLensProfileThread'是一個'field',但其用法類似於'type'

有人可以幫忙嗎?

與VB不同,在C#中,實例字段不能在其聲明的初始化程序中引用其他實例成員。 您的班級中有4個字段完全可以做到這一點,而您嘗試的變通方法只是在進一步加深您的麻煩。 通常正確的解決方法是從構造函數中調用初始化方法。 轉換后的代碼(正是您的類的前4個字段可以查明問題)是:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM