[.net] [C#]- accesing methods of objects held in a array

Started by
5 comments, last by TheTroll 13 years ago
Hi folks,

this is propably a pretty dumb question but still, I need help here.

Let's say I have 20 "Dog" objects(dog_0, dog_1, dog_2...). The Dog-Class has a method called "bark". Further I have an array where I put all my dogs in. I know that dog_0 f.e. is at dogArray[0].

Nopw want dog_3 to bark. I can't call dogArray[2].bark();

Why is that? and how to solve this problem?

Help would be nice thanks in advance!
Advertisement

Hi folks,

this is propably a pretty dumb question but still, I need help here.

Let's say I have 20 "Dog" objects(dog_0, dog_1, dog_2...). The Dog-Class has a method called "bark". Further I have an array where I put all my dogs in. I know that dog_0 f.e. is at dogArray[0].

Nopw want dog_3 to bark. I can't call dogArray[2].bark();

Why is that? and how to solve this problem?

Help would be nice thanks in advance!



This should work as is. Is the type of your array correct? (i.e. Dog[] dogArray = new Dog[...]). Is the bark() method public and non-static? (Note that the .Net naming convention is to uppercase the first letter: Bark() rather than bark()).

Do you get a compiler error? If so, please post it.


Otherwise explain what you mean by "it doesn't work". What did you expect it to do and what does it do instead?

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

Thx for the reply,

the problem was I didn't put the type of the array to dog. Just created an array and filled it with dogs. Thanks for the help.


PS: No compiler-error. it was just not possible to add ".bark();"




(Note that the .Net naming convention is to uppercase the first letter: Bark() rather than bark()).



Do you mean function-names are named with an uppercase? I tend to uppercase Class-names and keep my variables and methods in lowercase.
Naming convention doesn't really matter.

For me a lot of it depends on what language I am using. In C#. I use Caps for Classes, Public Methods and Public Properties. I use lower case for my private members, methods and properties. It is just so it reminds me what the level of access is.

As for the array. Remember that array is just a class with object members. So it had methods you could use just like any other class. Only when you declare the type in the array can you use the specific members methods that you created.

[quote name='Fiddler' timestamp='1297856179' post='4774921']
(Note that the .Net naming convention is to uppercase the first letter: Bark() rather than bark()).



Do you mean function-names are named with an uppercase? I tend to uppercase Class-names and keep my variables and methods in lowercase.
[/quote]
You can find the official guidelines on MSDN. They are simple: Use CamelCase everywhere except for private fields. Interfaces should start with an "I". Exceptions should end with "Exception". That's it! (There's also a full guide for library developers but you don't need that unless you build redistributable components).

If you follow guidelines you'll be able to reuse 3rd-party code without issue, since it will fit right in. Visual Studio Professional comes with a tool that will analyze your code and warn you if you have anything that doesn't follow the guidelines.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

Hi I know this is a late reply but I had some trouble getting registered. What you need to do in this case is explicitly cast the object types being held in the Array. This should work for you:

((Dog)dogArray[2]).bark();

I hope this helps!

Hi I know this is a late reply but I had some trouble getting registered. What you need to do in this case is explicitly cast the object types being held in the Array. This should work for you:

((Dog)dogArray[2]).bark();

I hope this helps!


Shouldn't need to do that at all.

As long as you use dog[20] and not the array class then it will always know it is a dog.

If you use the array class then you will have to objects and not dogs. Then you have to (Dog)dogs[2].Bark(). I recommend against that.

This topic is closed to new replies.

Advertisement