creating Dynmaic Objects in C#
-
How to create dynamic objects in C# in Runtime. For example, I will list no of drives present in a System if it is 6 drives i have to create 6 Different objects can any one help me regarding this... string []drives=GetLogicalDrives(); for(int i=0;i
-
How to create dynamic objects in C# in Runtime. For example, I will list no of drives present in a System if it is 6 drives i have to create 6 Different objects can any one help me regarding this... string []drives=GetLogicalDrives(); for(int i=0;i
This is basic stuff, you should read a C# book as you will get a much more detailed explanation than we can put here (and probably easier to follow): In your example:
string[] drives = GetLogicalDrives();
Returns an array of strings, each containing a name (lets say) of a drive. Use a
foreach
loop, rather than afor
loop - you don't need to know how many drives there are.foreach (string drive in drives)
{
DriveInfo di = new DriveInfo(drive);
... do something with the drive.
}DriveInfo is a class which I invented to return info about a drive given its name. Each time you deal with a different drive from the drives array, you create a new instance of DriveInfo to handle it, with the
new
keyword. Does that answer your question?You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
This is basic stuff, you should read a C# book as you will get a much more detailed explanation than we can put here (and probably easier to follow): In your example:
string[] drives = GetLogicalDrives();
Returns an array of strings, each containing a name (lets say) of a drive. Use a
foreach
loop, rather than afor
loop - you don't need to know how many drives there are.foreach (string drive in drives)
{
DriveInfo di = new DriveInfo(drive);
... do something with the drive.
}DriveInfo is a class which I invented to return info about a drive given its name. Each time you deal with a different drive from the drives array, you create a new instance of DriveInfo to handle it, with the
new
keyword. Does that answer your question?You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
OriginalGriff wrote:
DriveInfo is a class which I invented
System.IO.DriveInfo :)
-
OriginalGriff wrote:
DriveInfo is a class which I invented
System.IO.DriveInfo :)
Yes, yes, I know! :laugh: Just using it as a sensible name for a class the OP might want to implement to go with the "drives" array they already had. I dunno, .NET has taken too many of the good names already, why can't they have used DICFMSTOMYLH instead, and left all the good ones to us? They used to in the early days! :omg: DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Yes, yes, I know! :laugh: Just using it as a sensible name for a class the OP might want to implement to go with the "drives" array they already had. I dunno, .NET has taken too many of the good names already, why can't they have used DICFMSTOMYLH instead, and left all the good ones to us? They used to in the early days! :omg: DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
OriginalGriff wrote:
DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...
ROFL! I was wondering, I knew there had to be some method to that madness ;)
var question = (_2b || !(_2b));