|
-
Oct 10th, 2006, 02:36 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2.0] used like a method?
Code:
foreach (DataRowView invoiceRow in invoicebindingsource)
{
ipasstotal = invoiceRow("totalpayments");
}
I get
'invoiceRow' is a 'variable' but is used like a 'method'
It may be due to the late hour but i am completely stumped.
-
Oct 10th, 2006, 03:20 AM
#2
Re: [2.0] used like a method?
You index using square brackets in C#, not parentheses. Parentheses are for method argument lists, hence the error message.
-
Oct 10th, 2006, 02:17 PM
#3
Thread Starter
Fanatic Member
Re: [2.0] used like a method?
I am also having trouble with this line
userRowIndex = userbindingsource.Find("Name", invoiceRow["Name"]);
if (userRowIndex != -1 && userbindingsource[userRowIndex]["Department"]== Dept)
I am trying to check the department name at a certian index
but i amd get some trouble with the double []
i get the error
Cannot apply indexing with [] to an expression of type 'object'
Which i assume that reqires one or the other to be inclosed in () however i cant figure it out ive gone through a few diffrent varations but dont seem do the trick
in vb
the line is
If userRowIndex <> -1 AndAlso Me.userbindingsource(userRowIndex)("Department") = Dept Then
-
Oct 10th, 2006, 05:30 PM
#4
Re: [2.0] used like a method?
BindingSources can be bound to all sorts of things. For that reason the indexer of the BindingSource, which is the Item property in VB, returns an Object reference. This code:
Code:
userbindingsource[userRowIndex]
is invoking the indexer, thus it returns an Object. As the error message says, you cannot index an Object with square brackets. If you have bound a DataTable to your BindingSource then it's the contents of that table's DefaultView that is exposed via the BindingSource, which is always the case with bound DataTables. That means that the actual objects in the BindingSource will be DataRowView objects. You're trying to index one of those DataRowView objects but you aren't telling the compiler that that's what it is. You have to cast the Object reference returned by the indexer as that type so the compiler knows what it is and what members it has:
Code:
if (userRowIndex != -1 && ((DataRowView)userbindingsource[userRowIndex])["Department"] == Dept)
Had you been working with Option Strict turned On in VB, as I and many others suggest, then you would have had to do the same thing in VB with DirectCast. This is one of the reasons that I suggest having Option Strict turned on: so that you understand what is happening in your code. You should ALWAYS be aware of what type objects your properties and methods are returning. C# is a stricter language than VB by default so you'll find that you have to do this sort of thing all the time, and there's no equivalent to Option Strict that you can turn Off. This is a mild example of the sort of reason that VB has historically had a bad rep with developers who have used C/C++. It can tend to encourage you to ignore issues that could potentially crash your app at run time.
-
Oct 10th, 2006, 06:31 PM
#5
Thread Starter
Fanatic Member
Re: [2.0] used like a method?
thanks that definetly makes much more sense than what i was trying
-
Oct 10th, 2006, 07:05 PM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] [2.0] used like a method?
is it better to use convert.tostring
or use the (String) cast?
-
Oct 10th, 2006, 08:32 PM
#7
Re: [RESOLVED] [2.0] used like a method?
Converting and casting are two different things, so you should use whichever is appropriate for the current circumstances. Converting is for creating a string representation of an object that is not a string, e.g.
Code:
int myInt = 100;
string myString = Convert.ToString(myInt);
Casting is for getting a reference of type String to a String object that was returned via a reference of a type other than String, e.g.
Code:
object myObject = "Hello World";
string myString = (string)myObject;
Times when casting appropriate include retrieving an item from an ArrayList, retrieving a field value from an untyped DataRow, getting the SelectedValue from a ComboBox and using the 'sender' argument in an event handler. Any time an object is returned via a reference of a general type because multiple more specific types can be used is a place you should cast.
It is really, REALLY important to remember that OOP is designed to mimic real life. Let's say that you have a pet and you take it to the vet. The vet knows that your pet is an animal but they don't know what kind of animal. You tell them that it's a dog, so now they know what food to give it, where to house it, etc. The vet had to cast your Pet property, which returns an Animal reference, as type Dog in order to know how to use it. The object was always a Dog, even though it was returned via an Animal reference. Casting didn't convert anything, it merely provided a different way of viewing the same object. Have you ever heard the expression "casting something in a different light"? That's the way in which "cast" is used in programming. One of the dictionary definitions of cast is "The visual appearance of something or someone" and that's what casting is in programming: changing the appearance of something by accessing it via a different type of reference, not changing the type of the object itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|