将VB代码转换为C#时出错

时间:2016-06-29 10:10:47

标签: c# vb.net winforms

所以我在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;

现在问题是我无法在此声明 PlotLensProfileThread PlotRef = new PlotLensProfileThread(); 上访问 PlotLensProfileThread InRef 。编译器抛出错误 PlotLensProfileThread'是'字段',但用作'类型'

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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