Jump to content
  • Advertisement
Sign in to follow this  
KazenoZ

Overloading operators of existing structures?

This topic is 2246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

Not really sure how to have put this correctly in words for the title, but I hope this does ok enough.

So, my problem is, I need to overload the [] operator of a ListViewItemCollection from .NET, is that even possible? If it is, what would the correct method for it be?

Thanks alot!

Share this post


Link to post
Share on other sites
Advertisement
It is not. The best you can get is an extension method. Or perhaps to make an inheritor that overloads the operator, though that seems dicey.

Share this post


Link to post
Share on other sites
As far as I could see from the examples in MSDN extension methods aren't exactly what I'm looking for.
And is it even possible to make an inheritor like that, considering that the ListViewItemCollection is a member of the ListView class?

Share this post


Link to post
Share on other sites

As far as I could see from the examples in MSDN extension methods aren't exactly what I'm looking for.


No offense, but this is a peculiar thing you're looking for. That generally means you're looking for the wrong thing to solve the problem at hand. What are you trying to solve?


And is it even possible to make an inheritor like that, considering that the ListViewItemCollection is a member of the ListView class?
[/quote]

MSVS2010 lets me. As long as the thing has a public constructor I don't see why you couldn't...

Share this post


Link to post
Share on other sites
None taken.
I probably am looking at a wrong approach here..

My problem is that I have a page of code that does some converting of strings into actions(It takes the string, extracts a part of it and goes through a if...else set of operations until it finds the appropriate function associated with it and calls it).
The code is pretty lengthy due to a mass amount of possible actions(Around 15k lines of code).

The code works well and is not a problem, but at a rather late part of the development a new bug had shown up that I didn't notice at earlier stages, inside the if case for each of these actions there's a call to the previous items in the list(The aforementioned ListView contains the items), but some are skimmed over by the program since they are not important to the taken action, the code then gives you the item number of the first item that does meet the criteria set by the program.
The problem lies in that part, if the unimportant items are going from the selected item all the way through to the first item, the position returned would be -1, since I didn't include a checking state for that back then...

Now, it's about a 100 actions in the code that have this issue, and I was looking for a quick fix for it, ie; overloading the operator to add the check inside it and push it to 0 if it's -1.
Yes, there were many things I could've done to avoid this problem, and I would've designed the code differently today, but this was my first project in .NET and I was not very experienced with OOP either back then, so there's no need to point out at that and hurt my feelings =(


So... Yes, that's the problem. So is there any quickfix solution I could apply here, or do I have to pay for my sins and go manually over it all?

Share this post


Link to post
Share on other sites
How are you determining position and/or the 'call to the previous items in the list'? Depending on how that's done a quick property to return min(position,0) might help, or perhaps a slightly dirty find/replace.

Might be best if you post/link the giant pile of code.

Share this post


Link to post
Share on other sites
The format of a line is
ActionName: (parameter1` parameter2)PriorityNumber

//When we find our result, count would be just a number, but until then it's using the above format, so we'll check for that.
while(count->IndexOf(")") != -1 || count->IndexOf(
"End Case") != -1 || count->IndexOf("//") != -1){
//The various cases that aren't considered as proper results, so we'll push the item we're checking back in the list, and assign the
//new item to the count variable.
if(count->IndexOf("End Case") != -1){
pos--;
count = listView1->Items[pos]->Text;
mod--;
}
else if(count->IndexOf("//") != -1){
pos--;
count = listView1->Items[pos]->Text;
}
else if(count->IndexOf("Condition") != -1){
count = listView1->Items[pos]->Text;
mod++;
count = Convert::ToString(Convert::ToInt32(
count->Substring(count->IndexOf(")")+1))+mod);
}
else{
//When we finally found a proper result, we'll remove the action from it and store the number of the result alone in the count variable
count = Convert::ToString(Convert::ToInt32(
count->Substring(count->IndexOf(")")+1))+mod);
}
}


The error occurs at the positions that say

count = listView1->Items[pos]->Text;

Where the pos is decremented right beforehand.
So if the last item was the first in the list, this will now be -1 and invalid.

Share this post


Link to post
Share on other sites
Right. So simple ><

Thanks alot!


And yea, it was in C++ .NET, since that's the language I was more comfortable at the time. I actually ported the whole thing to C# later for it to be Mono-compatible, but I'm keeping updates on both builds, you know, just to be on the safe side.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!