Sign in to follow this  
plusnoir

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

Recommended Posts

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 [b][size="3"]dogArray[0][/size][/b].

Nopw want dog_3 to bark. I can't call [b][size="3"]dogArray[2].bark();

[/size][/b][size="3"]Why is that? and how to solve this problem?

Help would be nice thanks in advance!
[/size]

Share this post


Link to post
Share on other sites
[quote name='plusnoir' timestamp='1297851047' post='4774897']
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 [b][size="3"]dogArray[0][/size][/b].

Nopw want dog_3 to bark. I can't call [b][size="3"]dogArray[2].bark();

[/size][/b][size="3"]Why is that? and how to solve this problem?

Help would be nice thanks in advance!
[/size]
[/quote]

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?

Share this post


Link to post
Share on other sites
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();"


[quote name='Fiddler' timestamp='1297856179' post='4774921']

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

[/quote]

Do you mean function-names are named with an uppercase? I tend to uppercase Class-names and keep my variables and methods in lowercase.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='plusnoir' timestamp='1297860157' post='4774933']
[quote name='Fiddler' timestamp='1297856179' post='4774921']
(Note that the .Net naming convention is to uppercase the first letter: Bark() rather than bark()).

[/quote]

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 [url="http://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx"]official guidelines on MSDN[/url]. 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 [url="http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx"]full guide for library developers[/url] 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.

Share this post


Link to post
Share on other sites
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:

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

[/b]I hope this helps!

Share this post


Link to post
Share on other sites
[quote name='Verp' timestamp='1300818857' post='4789213']
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:

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

[/b]I hope this helps!
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this