Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Scope Confusion
    primarykey
    data
    text
    <p>In my code I have 3 sections, but in 2 of the 3 sections the code is almost identical, I was wondering if anyone could help me structure them properly so that I would only need one line written once, in a scope so that it can be seen by both sections so I don't need to have so much code. (Framework 3.5)</p> <pre><code>public static void FakeDriveInfo() { List&lt;DriveInfo&gt; driveList = DriveInfo.GetDrives().Where(x =&gt; x.IsReady).ToList&lt;DriveInfo&gt;(); Server server = new Server(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Server ID : {0}", server.ServerID = 0); Console.WriteLine("Server Name : {0}", server.ServerName = string.Concat(System.Environment.MachineName)); Console.WriteLine(); for (int i = 0; i &lt; driveList.Count; i++) { ServerDrive serverDrives = new ServerDrive(); Console.WriteLine("Drive Letter: {0}", driveList[i].Name); Console.WriteLine("Total Size: {0}", FormatBytes(driveList[i].TotalSize)); Console.WriteLine("Volume Label: {0}", driveList[i].VolumeLabel); Console.WriteLine("Free Space: {0}", FormatBytes(driveList[i].TotalFreeSpace)); Console.WriteLine("Drive Format: {0}", driveList[i].DriveFormat); Console.ReadLine(); } } public static void RealDriveInfo() { //Create the server object - You will need create a list of the server objects. Server server = new Server(); //Get all drives information List&lt;DriveInfo&gt; driveList = DriveInfo.GetDrives().Where(x =&gt; x.IsReady).ToList&lt;DriveInfo&gt;(); //Insert information of one server - You will need get information of all servers server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK. server.ServerName = string.Concat(System.Environment.MachineName); //Inserts information in the newServers object for (int i = 0; i &lt; driveList.Count; i++) { ServerDrive serverDrives = new ServerDrive(); //Put here all the information to obeject Server serverDrives.DriveLetter = driveList[i].Name; serverDrives.TotalSpace = driveList[i].TotalSize; serverDrives.DriveLabel = driveList[i].VolumeLabel; serverDrives.FreeSpace = driveList[i].TotalFreeSpace; serverDrives.DriveType = driveList[i].DriveFormat; // server.ListServerDrives.Add(serverDrives); server.ServerDrives.Add(serverDrives); } //Add the information to an SQL Database using Linq. DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver"); // db.Servers.InsertAllOnSubmit(server); db.Servers.InsertOnSubmit(server); db.SubmitChanges(); } </code></pre> <p>What I want to do is move the SQL to Linq part at the bottom of the code into it's own section. But to do that I have to have the whole <code>RealDriveInfo</code> Section too..</p> <p>Another part that I want to do is make the <code>List&lt;DriveInfo&gt; driveList = DriveInfo.GetDrives().Where(x=&gt;x.IsReady).ToList&lt;DriveInfo&gt;();</code> so that it can be seen by FakeDriveInfo and RealDriveInfo..</p> <p>Any feedback would be greatly appreciated, thanks.</p> <p>EDIT : At the moment I am calling two methods, FakeDriveInfo(); = Executing the console app and showing me the info it is going to submit. Name, Letter, Label, Server Name, ID, etc. RealDriveInfo(); = Connecting to the SQL Server and inserting the information into the two tables.</p> <p>I want to have a third method WriteInToDB(); = The DB Writing code would be taken from the RealDriveInfo Method and moved here instead.</p> <p>At the moment I have two methods that practically look identical due to the scope. I want the scope shifted so that I can put the List part of the code into the Main() Method and both FakeDriveInfo and RealDriveInfo can use it from there instead of having the code duplicated.</p> <p>Hopefully this adds a bit more sense to it all :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload