Jump to content

Help with some C# Code


AwAke
 Share

Recommended Posts

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
Link to comment
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.

Link to comment
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.

Link to comment
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.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...