AwAke Posted September 15, 2009 Share Posted September 15, 2009 (edited) Not sure if this belongs here, if this isn't the case feel free to tell me what forum it should be in and/or move it. Ty So I thought I'd try learn another language other than AutoIt. Which was the first programming language I really learned. I'm trying to learn C# and atm I'm just trying to call a method which looks for a removable drive, when found returns the name and holds it in the iDrive variable. The errors I get are: Error 1 An object reference is required for the non-static field, method, or property 'PenDriveBackup.Program.iDrive' Error 2 An object reference is required for the non-static field, method, or property 'PenDriveBackup.Program.GetDriveList()' Error 3 'PenDriveBackup.Program.GetDriveList()': not all code paths return a value. And the code is: using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; namespace PenDriveBackup { class Program { public string[] iDrive; public static void Main(string[] args) { iDrive = GetDriveList(); //System.Threading.Thread.Sleep(5000); } public string GetDriveList() { DriveInfo[] aDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in aDrives) { if (d.DriveType == DriveType.Removable) { return Convert.ToString(d.Name); } } } } } Thanks in advance, AwAke. Edited September 15, 2009 by AwAke Link to comment Share on other sites More sharing options...
jvanegmond Posted September 15, 2009 Share Posted September 15, 2009 The first two are errors because you are trying to access from a static function into non-static (these need to be initialized) function/variables. You'll see that your Main function is static, but your GetDriveList function is not static. You can't do this without some kind of instantiation. Simple solutions: - Make your functions static. - Create a new class, put functions in that class, instantiate the class. Error 3 'PenDriveBackup.Program.GetDriveList()': not all code paths return a value. public string GetDriveList() { DriveInfo[] aDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in aDrives) { if (d.DriveType == DriveType.Removable) { //dType = Convert.ToString(d.Name); return Convert.ToString(d.Name); } } // What if the code gets here?? Then there is nothing to return and there is no return statement either. This is not allowed. // return ""; } Also, an error that you'll see if you fix these three errors by following the above: These three lines: public string[] iDrive; iDrive = GetDriveList(); public string GetDriveList() You are trying to put a string into an array of strings without explicitly saying what element they should go in. This is not possible. github.com/jvanegmond Link to comment Share on other sites More sharing options...
AwAke Posted September 15, 2009 Author Share Posted September 15, 2009 Ah I see, may I ask what is the advantage of using static or not? I know google would give me the answer here but sometimes its overcomplicated or when asking a person specifically they sort of 'dumb' it down for you to understand Either way, thank you very much for your time Manadar! And thank you for the help. Thanks again, Awake. Link to comment Share on other sites More sharing options...
jvanegmond Posted September 15, 2009 Share Posted September 15, 2009 Well, first of all your program needs a static entry point (main function). Second, static can be used for things like nice design patterns. Singleton is a good example of that.I think a good thing for you to do would be to learn about classes (and instantiation). github.com/jvanegmond Link to comment Share on other sites More sharing options...
AwAke Posted September 15, 2009 Author Share Posted September 15, 2009 I certainly will do, thanks for the help Manadar. I appreciate it Thanks, Awake. Link to comment Share on other sites More sharing options...
jvanegmond Posted September 15, 2009 Share Posted September 15, 2009 No problem. We all have to learn programming at some point. Users of the AutoIt forum taught me how to 3 or 4 years ago, and now it's my turn to do something back. : ) github.com/jvanegmond Link to comment Share on other sites More sharing options...
AwAke Posted September 15, 2009 Author Share Posted September 15, 2009 Hehe, just something about the AutoIt forums. The replys are really quick, very helpful and noob friendly Link to comment Share on other sites More sharing options...
jvanegmond Posted September 15, 2009 Share Posted September 15, 2009 Yeah, I love these forums. But I think you can guess that after 7000 posts. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Valik Posted September 15, 2009 Share Posted September 15, 2009 Yeah, I love these forums. But I think you can guess that after 7000 posts. One (who didn't know me) would think I love the forum nearly twice as much based on your logic. And we all know how true that is... Link to comment Share on other sites More sharing options...
Richard Robertson Posted September 15, 2009 Share Posted September 15, 2009 One (who didn't know me) would think I love the forum nearly twice as much based on your logic. And we all know how true that is...If you didn't love us, you wouldn't still be here. Link to comment Share on other sites More sharing options...
Valik Posted September 15, 2009 Share Posted September 15, 2009 If you didn't love us, you wouldn't still be here.Hm. I think you have it wrong. If I didn't like some of you none of you would be here. Link to comment Share on other sites More sharing options...
Richard Robertson Posted September 15, 2009 Share Posted September 15, 2009 Hm. I think you have it wrong. If I didn't like some of you none of you would be here.Makes me feel all warm and fuzzy. Link to comment Share on other sites More sharing options...
jvanegmond Posted September 16, 2009 Share Posted September 16, 2009 One (who didn't know me) would think I love the forum nearly twice as much based on your logic. And we all know how true that is...That is assuming that love and posts have a exponential or logarithmic relationship. In reality, love for these forums comes at a certain number of posts, once you've reached that number your love does not increase. github.com/jvanegmond Link to comment Share on other sites More sharing options...
trancexx Posted September 16, 2009 Share Posted September 16, 2009 I didn't get the math part ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
jvanegmond Posted September 16, 2009 Share Posted September 16, 2009 I didn't get the math part This is high school material. Lower grade even. Would you like to learn it? github.com/jvanegmond Link to comment Share on other sites More sharing options...
trancexx Posted September 16, 2009 Share Posted September 16, 2009 This is high school material. Lower grade even. Would you like to learn it? My math teacher.I'll admit, it's time for me to take step forward. Please. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now