GabrielVa
GabrielVa

Reputation: 2388

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?

Upvotes: 2

Views: 190

Answers (7)

Richard Fawcett
Richard Fawcett

Reputation: 2809

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

Upvotes: 1

Ramesh
Ramesh

Reputation: 13266

All method calls should have () e.g ToString()

All Indexers should have [] w.g .Columns[...]

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

Upvotes: 2

Grzenio
Grzenio

Reputation: 36679

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

Upvotes: 3

Andy Evans
Andy Evans

Reputation: 7176

.ToString should have parenthesis after it.

foo.ToString()

Upvotes: 2

Vinay B R
Vinay B R

Reputation: 8421

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

Upvotes: 2

Joel Martinez
Joel Martinez

Reputation: 47789

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

Upvotes: 2

leppie
leppie

Reputation: 117280

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

Upvotes: 0

Related Questions