Class specific method in list of different classes
so i Have a list consisting of different classes, all inheriting from a common class. Now i want to call for a method specific to one of those subclasses but i can't seem to find the right code to do that, could anyone help?
the part of the code where it goes wrong (can't access the enemies list):
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()).GetType() == typeof(StandardRoom))
{
for (int i = 0; i < _floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies.Count; i++)
{
_floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies[i].UpdateBoundingBox();
}
}
and here is part of my StandardRoom Class
class StandardRoom : CommonBaseClass
{
public bool IsCleared { get; set; }
public List<Enemy> ennemies = new List<Enemy>();
...
}
c# list class
New contributor
add a comment |
so i Have a list consisting of different classes, all inheriting from a common class. Now i want to call for a method specific to one of those subclasses but i can't seem to find the right code to do that, could anyone help?
the part of the code where it goes wrong (can't access the enemies list):
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()).GetType() == typeof(StandardRoom))
{
for (int i = 0; i < _floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies.Count; i++)
{
_floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies[i].UpdateBoundingBox();
}
}
and here is part of my StandardRoom Class
class StandardRoom : CommonBaseClass
{
public bool IsCleared { get; set; }
public List<Enemy> ennemies = new List<Enemy>();
...
}
c# list class
New contributor
2
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
2
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using aforeach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use theenemy
object to call whatever you want. Also, you spelledenemies
* incorrectly.
– Chris
Dec 27 '18 at 14:32
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47
add a comment |
so i Have a list consisting of different classes, all inheriting from a common class. Now i want to call for a method specific to one of those subclasses but i can't seem to find the right code to do that, could anyone help?
the part of the code where it goes wrong (can't access the enemies list):
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()).GetType() == typeof(StandardRoom))
{
for (int i = 0; i < _floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies.Count; i++)
{
_floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies[i].UpdateBoundingBox();
}
}
and here is part of my StandardRoom Class
class StandardRoom : CommonBaseClass
{
public bool IsCleared { get; set; }
public List<Enemy> ennemies = new List<Enemy>();
...
}
c# list class
New contributor
so i Have a list consisting of different classes, all inheriting from a common class. Now i want to call for a method specific to one of those subclasses but i can't seem to find the right code to do that, could anyone help?
the part of the code where it goes wrong (can't access the enemies list):
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()).GetType() == typeof(StandardRoom))
{
for (int i = 0; i < _floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies.Count; i++)
{
_floor.GetRoomByIndex(CheckActiveRoomIndex()).enemies[i].UpdateBoundingBox();
}
}
and here is part of my StandardRoom Class
class StandardRoom : CommonBaseClass
{
public bool IsCleared { get; set; }
public List<Enemy> ennemies = new List<Enemy>();
...
}
c# list class
c# list class
New contributor
New contributor
edited Dec 27 '18 at 15:08
Olivier Jacot-Descombes
65.4k885136
65.4k885136
New contributor
asked Dec 27 '18 at 14:28
William Jacobs
43
43
New contributor
New contributor
2
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
2
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using aforeach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use theenemy
object to call whatever you want. Also, you spelledenemies
* incorrectly.
– Chris
Dec 27 '18 at 14:32
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47
add a comment |
2
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
2
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using aforeach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use theenemy
object to call whatever you want. Also, you spelledenemies
* incorrectly.
– Chris
Dec 27 '18 at 14:32
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47
2
2
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
2
2
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using a
foreach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use the enemy
object to call whatever you want. Also, you spelled enemies
* incorrectly.– Chris
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using a
foreach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use the enemy
object to call whatever you want. Also, you spelled enemies
* incorrectly.– Chris
Dec 27 '18 at 14:32
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47
add a comment |
2 Answers
2
active
oldest
votes
William,
The problem is that _floor.GetRoomByIndex(CheckActiveRoomIndex())
is not a StandardRoom, but a Room (or the base class, whatever the name)
1/ Solution closer to existing code
There is cast missing so that the rooms can be treated as StandardRoom : use the as keyword to cast in a safe way.
And Don't Repeat Yourself (DRY principle), declare a variable (stdRoom).
StandardRoom stdRoom = _floor.GetRoomByIndex(CheckActiveRoomIndex()) as StandardRoom;
if ( stdRoom != null )
{
for (int i = 0; i < stdRoom.enemies.Count; i++)
{
stdRoom.enemies[i].UpdateBoundingBox();
}
}
As says Olivier below, with C#7 pattern matching (Visual Studio 2017+), the cast can be shorter :
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
2/ The Linq answer
Enumerable.SelectMany
can be used to get Enemies from a Room
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
{
// projection to get Enemies from StandardRoom
foreach( Enemy e in stdRoom.SelectMany( r => r.enemies ) )
{
e.UpdateBoundingBox()
}
}
Regards
or with the new c# 7.0 syntaxif (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.
– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
add a comment |
LINQ has a tons of available methods. Read the documentation or just google 'LINQ methods'.
The method you are looking for is OfType<T>
(see documentation page). This method will only select the items in the list of the required type T
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
William Jacobs is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946633%2fclass-specific-method-in-list-of-different-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
William,
The problem is that _floor.GetRoomByIndex(CheckActiveRoomIndex())
is not a StandardRoom, but a Room (or the base class, whatever the name)
1/ Solution closer to existing code
There is cast missing so that the rooms can be treated as StandardRoom : use the as keyword to cast in a safe way.
And Don't Repeat Yourself (DRY principle), declare a variable (stdRoom).
StandardRoom stdRoom = _floor.GetRoomByIndex(CheckActiveRoomIndex()) as StandardRoom;
if ( stdRoom != null )
{
for (int i = 0; i < stdRoom.enemies.Count; i++)
{
stdRoom.enemies[i].UpdateBoundingBox();
}
}
As says Olivier below, with C#7 pattern matching (Visual Studio 2017+), the cast can be shorter :
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
2/ The Linq answer
Enumerable.SelectMany
can be used to get Enemies from a Room
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
{
// projection to get Enemies from StandardRoom
foreach( Enemy e in stdRoom.SelectMany( r => r.enemies ) )
{
e.UpdateBoundingBox()
}
}
Regards
or with the new c# 7.0 syntaxif (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.
– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
add a comment |
William,
The problem is that _floor.GetRoomByIndex(CheckActiveRoomIndex())
is not a StandardRoom, but a Room (or the base class, whatever the name)
1/ Solution closer to existing code
There is cast missing so that the rooms can be treated as StandardRoom : use the as keyword to cast in a safe way.
And Don't Repeat Yourself (DRY principle), declare a variable (stdRoom).
StandardRoom stdRoom = _floor.GetRoomByIndex(CheckActiveRoomIndex()) as StandardRoom;
if ( stdRoom != null )
{
for (int i = 0; i < stdRoom.enemies.Count; i++)
{
stdRoom.enemies[i].UpdateBoundingBox();
}
}
As says Olivier below, with C#7 pattern matching (Visual Studio 2017+), the cast can be shorter :
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
2/ The Linq answer
Enumerable.SelectMany
can be used to get Enemies from a Room
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
{
// projection to get Enemies from StandardRoom
foreach( Enemy e in stdRoom.SelectMany( r => r.enemies ) )
{
e.UpdateBoundingBox()
}
}
Regards
or with the new c# 7.0 syntaxif (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.
– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
add a comment |
William,
The problem is that _floor.GetRoomByIndex(CheckActiveRoomIndex())
is not a StandardRoom, but a Room (or the base class, whatever the name)
1/ Solution closer to existing code
There is cast missing so that the rooms can be treated as StandardRoom : use the as keyword to cast in a safe way.
And Don't Repeat Yourself (DRY principle), declare a variable (stdRoom).
StandardRoom stdRoom = _floor.GetRoomByIndex(CheckActiveRoomIndex()) as StandardRoom;
if ( stdRoom != null )
{
for (int i = 0; i < stdRoom.enemies.Count; i++)
{
stdRoom.enemies[i].UpdateBoundingBox();
}
}
As says Olivier below, with C#7 pattern matching (Visual Studio 2017+), the cast can be shorter :
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
2/ The Linq answer
Enumerable.SelectMany
can be used to get Enemies from a Room
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
{
// projection to get Enemies from StandardRoom
foreach( Enemy e in stdRoom.SelectMany( r => r.enemies ) )
{
e.UpdateBoundingBox()
}
}
Regards
William,
The problem is that _floor.GetRoomByIndex(CheckActiveRoomIndex())
is not a StandardRoom, but a Room (or the base class, whatever the name)
1/ Solution closer to existing code
There is cast missing so that the rooms can be treated as StandardRoom : use the as keyword to cast in a safe way.
And Don't Repeat Yourself (DRY principle), declare a variable (stdRoom).
StandardRoom stdRoom = _floor.GetRoomByIndex(CheckActiveRoomIndex()) as StandardRoom;
if ( stdRoom != null )
{
for (int i = 0; i < stdRoom.enemies.Count; i++)
{
stdRoom.enemies[i].UpdateBoundingBox();
}
}
As says Olivier below, with C#7 pattern matching (Visual Studio 2017+), the cast can be shorter :
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
2/ The Linq answer
Enumerable.SelectMany
can be used to get Enemies from a Room
if ( _floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom )
{
// projection to get Enemies from StandardRoom
foreach( Enemy e in stdRoom.SelectMany( r => r.enemies ) )
{
e.UpdateBoundingBox()
}
}
Regards
edited Dec 27 '18 at 16:44
answered Dec 27 '18 at 14:57
Emmanuel DURIN
3,77721435
3,77721435
or with the new c# 7.0 syntaxif (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.
– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
add a comment |
or with the new c# 7.0 syntaxif (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.
– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
or with the new c# 7.0 syntax
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
or with the new c# 7.0 syntax
if (_floor.GetRoomByIndex(CheckActiveRoomIndex()) is StandardRoom stdRoom)
. See: Dissecting the pattern matching in C# 7.– Olivier Jacot-Descombes
Dec 27 '18 at 15:12
add a comment |
LINQ has a tons of available methods. Read the documentation or just google 'LINQ methods'.
The method you are looking for is OfType<T>
(see documentation page). This method will only select the items in the list of the required type T
.
add a comment |
LINQ has a tons of available methods. Read the documentation or just google 'LINQ methods'.
The method you are looking for is OfType<T>
(see documentation page). This method will only select the items in the list of the required type T
.
add a comment |
LINQ has a tons of available methods. Read the documentation or just google 'LINQ methods'.
The method you are looking for is OfType<T>
(see documentation page). This method will only select the items in the list of the required type T
.
LINQ has a tons of available methods. Read the documentation or just google 'LINQ methods'.
The method you are looking for is OfType<T>
(see documentation page). This method will only select the items in the list of the required type T
.
answered Dec 27 '18 at 14:45
Karel Frajták
3,6241632
3,6241632
add a comment |
add a comment |
William Jacobs is a new contributor. Be nice, and check out our Code of Conduct.
William Jacobs is a new contributor. Be nice, and check out our Code of Conduct.
William Jacobs is a new contributor. Be nice, and check out our Code of Conduct.
William Jacobs is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946633%2fclass-specific-method-in-list-of-different-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
We prefer code in the question, preferable as text, not pictures. Your question is impossible to understand if the link goes dead and inaccessible to some people using accessibility aids. Ideally, you create an Minimal, Complete, and Verifiable example.
– Damien_The_Unbeliever
Dec 27 '18 at 14:31
2
William, add some context to your question by adding some code of your class definitions so we can help. Just the calling code is not enough.
– Tonto
Dec 27 '18 at 14:32
For future reference, you should actually post the code instead of posting screenshots of code. It's much easier for people to read/help. Secondly, your question is kind of unclear but why don't you try using a
foreach (var enemy in _floor.GetRoomByIndex(CheckActiveRoomIndex()).ennemies) { //... }
and then use theenemy
object to call whatever you want. Also, you spelledenemies
* incorrectly.– Chris
Dec 27 '18 at 14:32
@Damien_The_Unbeliever thanks for the advise, i addes some code, should be easier to read now.
– William Jacobs
Dec 27 '18 at 14:47
@Chris the problem is that my code doesn't seem like it can read the enemies list, same happens when i try to use a for each instead of a for loop
– William Jacobs
Dec 27 '18 at 14:47