473,809 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

foreach variable scope

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(s tring strComputer)
{
ManagementScope oMS = new ManagementScope ("\\\\" + strComputer);
ObjectQuery oQuery = new ObjectQuery("Se lect Size From
Win32_DiskDrive ");
ManagementObjec tSearcher oSearcher = new
ManagementObjec tSearcher(oMS, oQuery);
ManagementObjec tCollection colSearch = oSearcher.Get() ;

foreach ( ManagementObjec t oReturn in colSearch )
{
if ( (System.Int32.P arse(oReturn.To String())/100) < 1048576 )
{
int iSize = 1048576;

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

}
else
{
int iSize = System.Int32.Pa rse(oReturn.ToS tring());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@ "c:\");
int iFolders =
Int32.Parse(odi rInfo.GetFileSy stemInfos().Len gth.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;
}
Nov 17 '05 #1
3 2441
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]
- mv*@spam.guard. caspershouse.co m

"Primera" <Pr*****@discus sions.microsoft .com> wrote in message
news:B9******** *************** ***********@mic rosoft.com...
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(s tring strComputer)
{
ManagementScope oMS = new ManagementScope ("\\\\" +
strComputer);
ObjectQuery oQuery = new ObjectQuery("Se lect Size From
Win32_DiskDrive ");
ManagementObjec tSearcher oSearcher = new
ManagementObjec tSearcher(oMS, oQuery);
ManagementObjec tCollection colSearch = oSearcher.Get() ;

foreach ( ManagementObjec t oReturn in colSearch )
{
if ( (System.Int32.P arse(oReturn.To String())/100) <
1048576 )
{
int iSize = 1048576;

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

}
else
{
int iSize = System.Int32.Pa rse(oReturn.ToS tring());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@ "c:\");
int iFolders =
Int32.Parse(odi rInfo.GetFileSy stemInfos().Len gth.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;
}

Nov 17 '05 #2
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard. caspershouse.co m

"Primera" <Pr*****@discus sions.microsoft .com> wrote in message
news:B9******** *************** ***********@mic rosoft.com...
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(s tring strComputer)
{
ManagementScope oMS = new ManagementScope ("\\\\" +
strComputer);
ObjectQuery oQuery = new ObjectQuery("Se lect Size From
Win32_DiskDrive ");
ManagementObjec tSearcher oSearcher = new
ManagementObjec tSearcher(oMS, oQuery);
ManagementObjec tCollection colSearch = oSearcher.Get() ;

foreach ( ManagementObjec t oReturn in colSearch )
{
if ( (System.Int32.P arse(oReturn.To String())/100) <
1048576 )
{
int iSize = 1048576;

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

}
else
{
int iSize = System.Int32.Pa rse(oReturn.ToS tring());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@ "c:\");
int iFolders =
Int32.Parse(odi rInfo.GetFileSy stemInfos().Len gth.ToString()) * 1280;
int NeededSize = 196096 + iFolders + iBytes;
}


Nov 17 '05 #3
Primera wrote:
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(s tring strComputer)
{
ManagementScope oMS = new ManagementScope ("\\\\" + strComputer);
ObjectQuery oQuery = new ObjectQuery("Se lect Size From
Win32_DiskDrive ");
ManagementObjec tSearcher oSearcher = new
ManagementObjec tSearcher(oMS, oQuery);
ManagementObjec tCollection colSearch = oSearcher.Get() ;

foreach ( ManagementObjec t oReturn in colSearch )
{
if ( (System.Int32.P arse(oReturn.To String())/100) < 1048576 )
{
int iSize = 1048576;

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

}
else
{
int iSize = System.Int32.Pa rse(oReturn.ToS tring());

}
}
int iBytes = iSize + (iSize / 803);
DirectoryInfo odirInfo = new DirectoryInfo(@ "c:\");
int iFolders =
Int32.Parse(odi rInfo.GetFileSy stemInfos().Len gth.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 ( ManagementObjec t oReturn in colSearch )
{
if ( (System.Int32.P arse(oReturn.To String())/100) < 1048576 )
iSize = 1048576;
if ((System.Int32. Parse(oReturn.T oString()) / 100) > 4194304)
iSize = 4194304;
else
iSize = System.Int32.Pa rse(oReturn.ToS tring());
}
}
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(oRe turn.ToString() ) instead of the fixed value of
1048576.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2972
by: Brad Williams | last post by:
I'm trying to get clearer on limitations of assignment/modifications within a foreach. Why does the following gives a compilation error if MyType is a struct, but it does not if MyType is a class? public struct MyType // change to class, and it compiles clean { public int f; } public void foo()
104
7208
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
32
6529
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains on the "bit = false;" stating that bit is read-only.
2
3196
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.