Jump to content

So I am kinda working on old source files just for fun. The task is to convert VS2003 files to VS2012.

 

Here is some code. The "pos = i" i assume refers to "i" from the loop.

	for( POSTYPE i=0;i<m_pStallGrid->GetCellNum();++i)	{		if( m_pStallGrid->m_pIconGridCell[i].use )		{			pItem = (CExchangeItem*)m_pStallGrid->m_pIconGridCell[i].icon;			if( pItem->GetDBId() == pbase->dwDBIdx )				break;		}	}	pos = i;

Example of how I "fixed" it.

	POSTYPE i;	for(i=0;i<m_pStallGrid->GetCellNum();++i)	{		if( m_pStallGrid->m_pIconGridCell[i].use )		{			pItem = (CExchangeItem*)m_pStallGrid->m_pIconGridCell[i].icon;			if( pItem->GetDBId() == pbase->dwDBIdx )				break;		}	}	pos = i;

Was this considered normal? Also most of for loops look like "for (i=0; i< number; i++)" instead of "for (int i=0; i< number; i++)". 

 

 

Link to comment
https://linustechtips.com/topic/25747-need-help-converting-old-code/
Share on other sites

Link to post
Share on other sites

Is it actually sensful to set pos=i because you already know what i will be after the loop? (I don't know what the break exactly does.)
In my opinion it is more common to declare the variable in the loop and not before, but it also works so why not.

Link to post
Share on other sites

Is it actually sensful to set pos=i because you already know what i will be after the loop? (I don't know what the break exactly does.)

In my opinion it is more common to declare the variable in the loop and not before, but it also works so why not.

Exactly what I thought. But the code I am converting is old, probably before 2003.

I think variables without types used to default to int, thats why original developers did it that way.

However it doesn't answer why for loop variables are used outside the loop.

 

 

Link to post
Share on other sites

yeah if you don't declare a type it should get pissy. 03 probably let you do that for corner cutting because programmers are lazy but it probably ended up being a source of bugs. VS languages are all strongly typed so you 'need' to declare a type

Link to post
Share on other sites

How about using loop variables after the loop itself?

for( POSTYPE i=0;i<m_pStallGrid->GetCellNum();++i)	{		if( m_pStallGrid->m_pIconGridCell[i].use )		{			pItem = (CExchangeItem*)m_pStallGrid->m_pIconGridCell[i].icon;			if( pItem->GetDBId() == pbase->dwDBIdx )				break;		}	}	pos = i;

In vs2003 pos is assigned a value of "i" that was used in for loop. 

 

 

Link to post
Share on other sites

for( POSTYPE i=0;i<m_pStallGrid->GetCellNum();++i)	{		if( m_pStallGrid->m_pIconGridCell[i].use )		{			pItem = (CExchangeItem*)m_pStallGrid->m_pIconGridCell[i].icon;			if( pItem->GetDBId() == pbase->dwDBIdx )				break;		}	}	pos = i;

Isn't 'i' unreachable from outside the loop? Dis language be hacking.

 

I'd make it into it's own method and return the value of i.

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

×