Sign in to follow this  
Followers 0
AwAke

Help with some C# Code

16 posts in this topic

#1 ·  Posted (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 :D

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 by AwAke

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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 :D

Either way, thank you very much for your time Manadar! And thank you for the help.

Thanks again, Awake.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

I certainly will do, thanks for the help Manadar. I appreciate it :D

Thanks, Awake.

Share this post


Link to post
Share on other sites

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. : )

Share this post


Link to post
Share on other sites

Hehe, just something about the AutoIt forums. The replys are really quick, very helpful and noob friendly :D

Share this post


Link to post
Share on other sites

Yeah, I love these forums. But I think you can guess that after 7000 posts. :D

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

I didn't get the math part :D


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

This is high school material. Lower grade even. Would you like to learn it? :D:D

My math teacher.

I'll admit, it's time for me to take step forward. Please.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0