foreach variable scope

G

Guest

I am trying to use the below to return different values of an int variable,
but I seem to be running into scope issues as I cannot use the iSize variable
outside of the foreach block. Any help is appreciated.

public int CalcDiskSpace(string strComputer)
{
ManagementScope oMS = new ManagementScope("\\\\" + strComputer);
ObjectQuery oQuery = new ObjectQuery("Select Size From
Win32_DiskDrive");
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMS, oQuery);
ManagementObjectCollection colSearch = oSearcher.Get();

foreach ( ManagementObject oReturn in colSearch )
{
if ( (System.Int32.Parse(oReturn.ToString())/100) < 1048576 )
{
int iSize = 1048576;

}
if ((System.Int32.Parse(oReturn.ToString()) / 100) > 4194304)
{
int iSize = 4194304;

}
else
{
int iSize = System.Int32.Parse(oReturn.ToString());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@"c:\");
int iFolders =
Int32.Parse(odirInfo.GetFileSystemInfos().Length.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;


}
 
N

Nicholas Paldino [.NET/C# MVP]

Primera,

I'm a little confused by what you are trying to do here. First, there
is no way that this code can compile, as you arent returning anything.

What are you trying to do?

From what I can see, you have other problems. The iSize variable is
declared in the if block, which is nested in a foreach block. You should
declare the iSize variable outside of the foreach, and then you can access
it. The other problem is that iSize is only going to be equal to the last
result in this query (if you have multiple drives, then it is an issue).

This is why I asked what you are trying to do.
 
G

Guest

I apologize for the mess, but I'm just teaching myself c#. I'm trying to get
multiple pieces of information from a hard drive to compute space
requirements prior to running convert.exe to convert a FAT32 drive to NTFS.
This link will show the article that outlines how to compute the space
requirements ( http://support.microsoft.com/kb/156560/EN-US/ ). Thanks for
your help.

Nicholas Paldino said:
Primera,

I'm a little confused by what you are trying to do here. First, there
is no way that this code can compile, as you arent returning anything.

What are you trying to do?

From what I can see, you have other problems. The iSize variable is
declared in the if block, which is nested in a foreach block. You should
declare the iSize variable outside of the foreach, and then you can access
it. The other problem is that iSize is only going to be equal to the last
result in this query (if you have multiple drives, then it is an issue).

This is why I asked what you are trying to do.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Primera said:
I am trying to use the below to return different values of an int variable,
but I seem to be running into scope issues as I cannot use the iSize
variable
outside of the foreach block. Any help is appreciated.

public int CalcDiskSpace(string strComputer)
{
ManagementScope oMS = new ManagementScope("\\\\" +
strComputer);
ObjectQuery oQuery = new ObjectQuery("Select Size From
Win32_DiskDrive");
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMS, oQuery);
ManagementObjectCollection colSearch = oSearcher.Get();

foreach ( ManagementObject oReturn in colSearch )
{
if ( (System.Int32.Parse(oReturn.ToString())/100) <
1048576 )
{
int iSize = 1048576;

}
if ((System.Int32.Parse(oReturn.ToString()) / 100) >
4194304)
{
int iSize = 4194304;

}
else
{
int iSize = System.Int32.Parse(oReturn.ToString());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@"c:\");
int iFolders =
Int32.Parse(odirInfo.GetFileSystemInfos().Length.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;


}
 
M

Mike Hofer

Primera said:
I am trying to use the below to return different values of an int variable,
but I seem to be running into scope issues as I cannot use the iSize variable
outside of the foreach block. Any help is appreciated.

public int CalcDiskSpace(string strComputer)
{
ManagementScope oMS = new ManagementScope("\\\\" + strComputer);
ObjectQuery oQuery = new ObjectQuery("Select Size From
Win32_DiskDrive");
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMS, oQuery);
ManagementObjectCollection colSearch = oSearcher.Get();

foreach ( ManagementObject oReturn in colSearch )
{
if ( (System.Int32.Parse(oReturn.ToString())/100) < 1048576 )
{
int iSize = 1048576;

}
if ((System.Int32.Parse(oReturn.ToString()) / 100) > 4194304)
{
int iSize = 4194304;

}
else
{
int iSize = System.Int32.Parse(oReturn.ToString());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@"c:\");
int iFolders =
Int32.Parse(odirInfo.GetFileSystemInfos().Length.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;


}

The scope of the iSize variable is limited to the curly braces in which
it is defined. You have three separate variables, with three separate
scopes, that just happen to have the same name.

Move the declaration of the variable out of the curly braces, and leave
the assignments within, like this:

int iSize = 0;
foreach ( ManagementObject oReturn in colSearch )
{
if ( (System.Int32.Parse(oReturn.ToString())/100) < 1048576 )
iSize = 1048576;
if ((System.Int32.Parse(oReturn.ToString()) / 100) > 4194304)
iSize = 4194304;
else
iSize = System.Int32.Parse(oReturn.ToString());
}
}
int iBytes = iSize + (iSize / 803);

P.S. You might be missing an ELSE in there. If the first IF statement
executes, but then the second one enters the ELSE block, iSize will be
equal to Int32.Parse(oReturn.ToString()) instead of the fixed value of
1048576.
 
Top