Vb.net在c#中填充一个循环结构

时间:2014-03-31 11:25:40

标签: c# vb.net struct

你如何将其翻译成VB.Net?我真的不确定Structs如何在那里工作,我找不到合适的一个。 你们有没有人能告诉我它是如何在那里完成的?

struct Questions
{
    public int temakor;
    public int nKerdes;
    public string kerdes;
    public string kerdes_a;
    public string kerdes_b;
    public string kerdes_c;
    public string kerdes_d;
    public char valasz;
 }

然后是Main()

    List<Questions> test_questions = new List<Questions>();
    using (StreamReader sr = new StreamReader(@"teszt_.txt"))
    {
        for (int i = 0; i < 130; i++)
        {
            string s = sr.ReadLine();
            string[] fields = s.Split('_'); // adatok feldarabolása
            Kerdesek uj_kerdes;
            uj_kerdes.temakor = int.Parse(fields[0]);
            uj_kerdes.nKerdes = int.Parse(fields[1]);
            uj_kerdes.kerdes = fields[2];
            uj_kerdes.kerdes_a = fields[3];
            uj_kerdes.kerdes_b = fields[4];
            uj_kerdes.kerdes_c = fields[5];
            uj_kerdes.kerdes_d = fields[6];
            uj_kerdes.valasz = char.Parse(fields[7]);
            test_questions.Add(uj_kerdes);
        }
    }

    Console.WriteLine("Kedves vizsgázó, jó tanulást a következő vizsgára");

3 个答案:

答案 0 :(得分:3)

嗯,根据反射器,C#

Questions q = new Questions {
    temakor = 1,
    nKerdes = 2,
    kerdes = "",
    kerdes_a = "a",
    kerdes_b = "b",
    kerdes_c = "c",
    kerdes_d = "d",
    valasz = 'v'
};

转换为:

Dim q As New Questions With { _
    .temakor = 1, _
    .nKerdes = 2, _
    .kerdes = "", _
    .kerdes_a = "a", _
    .kerdes_b = "b", _
    .kerdes_c = "c", _
    .kerdes_d = "d", _
    .valasz = "v"c _
}

从那里它应该很简单。但是:几乎可以肯定是对struct 的不当使用,它会对你产生积极的伤害。我强烈建议:

class Question
{
    public int Temakor {get;set;}
    public int KerdesInt32 {get;set;} // probably needs a better name
    public string Kerdes {get;set;}
    public string KerdesA {get;set;}
    public string KerdesB {get;set;}
    public string KerdesC {get;set;}
    public string KerdesD {get;set;}
    public char Valasz {get;set;}
 }

或者作为一个绝对最坏的情况(这是可怕的,需要证明理由):

struct Question
{
    public readonly int Temakor;
    public readonly int KerdesInt32;
    public readonly string Kerdes, KerdesA, KerdesB, KerdesC, KerdesD
    public readonly char Valasz;
    public Question(int temakor, int kerdesInt32, ..., char valasz)
    {
        Temakor = temakor;
        KerdesInt32 = kerdesInt32;
        // ...
        Valasz = valasz;
    }
 }

答案 1 :(得分:0)

Kerdesek也是问题。当我写这个主题时,我很着急,所以我只翻译了它最重要的部分,以便更容易理解。 无论如何。我想要做的是使用这些变量创建一个结构,导入一个txt文件,并填充这些变量。但是!我不想只使用一次结构,但更像是例如:大约10倍左右。

  • Temakor持有主题编号
  • nKerdes持有问题编号
  • kerdes自己持有这个问题
  • kerdes_a持有答案&#34; a&#34;对于这个问题
  • kerdes_b ...
  • kerdes_c ...
  • kerdes_d ...
  • valasz持有答案。

因此,当我使用streamreader读取我的txt文件时,它的行会保存这些变量的数据,当一个问题被读入时,我会读到另一个,直到我读完整个txt文件。 我为此使用结构的原因是C#非常简单,我想这是最简单的方法(使用LINQ)。

答案 2 :(得分:0)

我必须弄明白,这根本不是什么大问题。

 Imports System
 Imports System.IO
 Imports System.Text

Public Class Form1
Public Structure Kerdesek
    Public hanyadik As Integer
    Public kerdes As String
    Public valaszA As String
    Public valaszB As String
    Public valaszC As String
    Public valaszD As String
    Public helyesValasz As Char
End Structure

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub

Private Sub BetöltésToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BetöltésToolStripMenuItem.Click
    OpenFile.ShowDialog()

End Sub

Private Sub OpenFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFile.FileOk
    Dim sr As StreamReader = New StreamReader(OpenFile.OpenFile())
    Dim firstLine As Boolean = True
    Dim row As Integer
    Dim lst As New List(Of Kerdesek)

    While Not (sr.EndOfStream)
        If (firstLine = True) Then
            row = sr.ReadLine()
            firstLine = False
        ElseIf (firstLine = False) Then
            For i = 1 To row
                Dim ujKerdes As New Kerdesek
                ujKerdes.hanyadik = sr.ReadLine()
                ujKerdes.kerdes = sr.ReadLine()
                ujKerdes.valaszA = sr.ReadLine()
                ujKerdes.valaszB = sr.ReadLine()
                ujKerdes.valaszC = sr.ReadLine()
                ujKerdes.valaszD = sr.ReadLine()
                ujKerdes.helyesValasz = sr.ReadLine()
                lst.Add(ujKerdes)
            Next i
        End If
    End While
    sr.Close()


    For Each item As Kerdesek In lst
        LabelHanyadikKerdes.Text = item.hanyadik
        LabelKerdes.Text = item.kerdes
        LabelA.Text = item.valaszA
        LabelB.Text = item.valaszB
        LabelC.Text = item.valaszC
        LabelD.Text = item.valaszD
        MessageBox.Show(item.hanyadik)
        MessageBox.Show(item.kerdes)
    Next
End Sub
End Class