1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Beginner help with C#.

Discussion in 'C#' started by AwAk3e, Sep 15, 2009.

  1. AwAk3e

    AwAk3e New Member

    Joined:
    Sep 15, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    So I used a type-safe language before this one, AutoIt and was I speaking to a friend about software development and well he guided me too C#. I've read several tutorials but I guess I haven't taken it in.

    Can anyone see what is wrong with this code? I dont seem to understand the errors that 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.

    The code is:
    Code:
    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) 
                    {
                        //dType = Convert.ToString(d.Name);
                        return Convert.ToString(d.Name);
                    }
                }
            }
        }
    }
    Thanks in advance, AwAk3e.
     
  2. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore
    Hi AwAk3e,

    You are trying to call non-static or member function GetDriveList() in a static method. Thats why the error.
     

Share This Page