0

I have written the following code to implement this in VB.NET:

Public Class TBdata

    Public txtBox() As TextBox = {Form1.TextBox1, Form1.TextBox2, Form1.TextBox3, Form1.TextBox4}
    Public aTextBoxes(3) As String

    Public Sub DataToArray()
         For i As Integer = 0 To 3
             aTextBoxes(i) = txtBox(i).Text
         Next
    End Sub

End Class


'On the Form, to capture all entries in the text boxes

   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim d As TBdata = New TBdata()
        d.DataToArray()

    Dim msg As String = ""
        For i As Integer = 0 To 3
            msg = msg & d.aTextBoxes(i) & " : "
        Next

        MessageBox.Show(msg)
    End Sub

Now, why this can't be done in C#?

class TBdata
{
    public string[] aTextBoxes = new string[3];
    public TextBox[] txtBox = new TextBox[] { Form1.textBox1, Form1.textBox2, Form1.textBox3, Form1.textBox4 };

    public void DataToArray()
    {
        for (int i = 0; i < 4; i++)
        {
            aTextBoxes(i) = txtBox(i).Text;

        }
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Abel
  • 9
  • 1

2 Answers2

3

The problem is here:

public TextBox[] txtBox = new TextBox[] { Form1.textBox1, Form1.textBox2, Form1.textBox3, Form1.textBox4 };

Visual Basic will automatically create a default instance of Form1 for a Windows Form if you refer to it by the class. C# doesn't create this "automatic" instance, so you need to explicitly pass an instance to your constructor for the form, and load the textboxes from that instance.

class TextBoxData
{
    public string[] aTextBoxes = new string[3];
    public TextBox[] TextBoxes { get; private set;}

    public TextBoxData(Form1 form)
    {
        this.TextBoxes = new TextBox[] { form.textBox1, form.textBox2, form.textBox3, form.textBox4 };
    }

    public void DataToArray()
    {
        for (int i = 0; i < TextBoxes.Length; i++)
        {
            aTextBoxes[i] = TextBoxes[i].Text;
        }
    }

    // ...
Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thank you Reed, I'm still digesting this "non-automatic" default instance in C#. I added _"using System.Windows.Forms;"_ to the class TextBoxData and on the form I changed the textBoxes Modifiers property from _private_ to _public_. With this the compiler does not complain anymore. Now, since the class constructor takes Form1 as argument. How I create an instance of the class TextBoxData? – Abel Jun 22 '13 at 05:26
0

It seems like your class TBData is private in your c# code. The visibility keyword is absent and c# defaults to private, therefore even if you add the proper using statement to include TBData namespace or if you code something else in a different file or in the same namespace you will never be able to instantiate an object of type TBData.

Also in your code example in c# you use parentheses to access an object at a certain index in the array, which is the notation for VB. In c# indexes are accessed with square brackets [], parentheses are reserved for method calls.

Louis
  • 593
  • 4
  • 13