简体   繁体   中英

C# Question regarding rewriting from VB

I'm a VB guy learning C#. I seem to be getting the hang of it but I've got a couple questions regarding some code I'm writing.

In the first one here I'm getting an error when I write my code like so:

Irowindex = sF1411BindingSource.Find(sF1411DataSet.SF1411.Columns(groupBox4.Tag.ToString).ToString, textBox1.Text);
        if (Irowindex == -1)

Error 1 'System.Data.DataTable.Columns' is a 'property' but is used like a 'method' C:\\11180_APPLICATION\\11180_APPLICATION\\Edit.cs 186 71 11180_APPLICATION

My other issue is that in VB I use Tags but in C# it doesnt seem to like them:

//Set the find label to display the new find column
groupBox4.Text = "Find - " + sender.Tag.ToString + ":";
//Store the sort column name in lblFind's Tag property
groupBox4.Tag = sender.Tag.ToString;

Error 4 'object' does not contain a definition for 'Tag' C:\\11180_APPLICATION\\11180_APPLICATION\\Edit.cs 211 36 11180_APPLICATION

Any ideas here?

Try Columns[groupBox4.Tag.ToString()] for the first error.

Regarding tags, in C# the sender is of type Object, and it doesn't have a tag property. Try casting it to Control first:

((Control)sender).Tag

You need to replace an index accessor (in VB .Columns(...)) with the C# syntax of using square brackets.

.Columns[...]

As far as the other error. It sounds like your controls are typed as Object (to be late bound in VB). You need to either change the control definition to use the full type (or at minimum, Control) ... or cast to Control

use ToString() instead of ToString everywhere. its a method.

also Columns is a collection and access is via index in which case you need to use [] instead of ()

.ToString后面应该有括号。

foo.ToString()

All method calls should have () eg ToString()

All Indexers should have [] wg .Columns[...]

And also, you may have to type case the sender like ((TextBox)sender).Text

For your second problem, sender is presumably a parameter of some event handler...

protected void HandlerName(object sender, EventArgs e) { ... }

If this is the case, and you want to use sender as some other class, then you need to cast it like this:

YourClass mySender = sender as YourClass;
if (mySender != null) {
  // Do your handling here
}

Following @gabrielVa's comment below, revised code is:

private void radioButton1_CheckedChanged(object sender, EventArgs e){
    RadioButton radioSender = sender as RadioButton;
    if (radioSender != null){
        sF1411BindingSource.Sort = radioSender.Tag.ToString();
        sF1411BindingSource.MoveFirst();
        //Set the find label to display the new find column
        groupBox4.Text = "Find - " + radioSender.Tag.ToString() + ":";
        //Store the sort column name in lblFind's Tag property
        groupBox4.Tag = radioSender.Tag.ToString();
        textBox1.ReadOnly = false;
    }
}

You need square braces there (the indexer). IOW [ ] and not ( ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM