-1

I'm trying to populate a combobox in a windows form with all my dvd/cd drives and say if they have a disc in them.

I'm trying to do it with a class tried sending an object and list but the object just comes up with and error and the list just comes up with collection.

using System;
using System.Windows.Forms;

namespace DVD_main
{
        public frmMain()
        {
            InitializeComponent();
            cboDrive.Items.AddRange(Cls_DVD_Player.DVDDrvCtrls.cdDrv);
        }
}

using System;
using System.Collections.Generic;
using System.Windows;
using System.IO;
using System.Runtime.InteropServices;



namespace Cls_DVD_Controls
{

    class DVDDrvCtrls
    {
        private static readonly object cboDrive;

        public static List<string> cdDrv()
        {
            List<string> drvNames = new List<string>();
            DriveInfo[] cdDrives = DriveInfo.GetDrives();
            foreach (DriveInfo d in cdDrives)
            {
                if (d.DriveType == DriveType.CDRom && d.IsReady)
                {
                    drvNames.Add(d.Name + " " + d.VolumeLabel);
                    MessageBox.Show(d.Name + " " + d.VolumeLabel);
                }
                else if (d.DriveType == DriveType.CDRom) {
                    drvNames.Add(d.Name + " Drive Empty");
                }
            }
            //drvNames.AsReadOnly.cdDrives;
            return drvNames;

        }
    }
}

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • What kind of error, what's the text of the error, and on what line does it happen? And, what does "the list just comes up with collection" mean? – Flydog57 Aug 31 '19 at 22:23
  • it says cannot convert from method group to object – Clint Gaydon Aug 31 '19 at 23:40
  • That's a compile error. It means that you are using the name of a function in a place where you should be using a variable that represents an object. What line of code does this occur on? By the way, when quoting an error, it's best to use quotation marks and to copy & paste the entire error, and to include information about where the error occurred – Flydog57 Sep 01 '19 at 00:07
  • Ah, take a look at the second line of `frmMain`. You have `cboDrive.Items.AddRange(Cls_DVD_Player.DVDDrvCtrls.cdDrv);`. The name `cdDrv` is the name of a function (or in C# compiler-ese, a method group (yes, that name makes sense)). If you want to call that function and use its return value as the parameter to AddRange, you need parentheses (`cdDrv()`). If you'd included the error message and the offending line in your initial question, you would have gotten an instant answer. You may want to look up C# coding and naming standards, it makes it easier for others to read your code – Flydog57 Sep 01 '19 at 00:14
  • `cboDrive.DataSource = Cls_DVD_Player.DVDDrvCtrls.cdDrv();`.I suggest not to use a `List` for this. Your method could return a more complex object that can hold more informations on the selected drive, including a Property that returns a string, to present the information in the textual part of the ComboBox list (similar to what your returning now, as a single string). See the `DisplayMember` and `ValueMember` properties of the ComboBox. – Jimi Sep 01 '19 at 03:48
  • @Jimi thank you i've been trying to find one that works for doing just that but wasn't sure you could return one. not sure how to give credit for anything in comments though – Clint Gaydon Sep 01 '19 at 12:52
  • @Flydog thanks you didn't know that but I might be missing something in your comment since some of it wasn't very clear. I've not programed for a couple of years trying to get back into it. – Clint Gaydon Sep 01 '19 at 12:54
  • You can ask the person that gave you the solution in a comment to post that solution as an answer. Or you can post an answer yourself, if the comment just pointed you in the *right direction* and you applied your own solution. In this case, if you found a solution, you should post the answer yourself: you're the one that knows what the adopted solution actually is. Mark your own answer when allowed. – Jimi Sep 01 '19 at 12:56
  • @Jimi I'll do that thank you – Clint Gaydon Sep 01 '19 at 13:01
  • @clintgaydon: sorry. What wasn't clear. Did you add parentheses after `cdDrv`, and did it fix your issue? My other two comments were 1) in the future, please copy/paste the error message and indicate the offending line of code (had you done this initially, you would have gotten a response nearly instantaneously), and 2) it would be worthwhile to look up C# naming and coding conventions (they are well documented); it makes your code easier for others to read – Flydog57 Sep 01 '19 at 17:54
  • @jimi can't find a control that works as a class for this – Clint Gaydon Sep 01 '19 at 19:41
  • @Flsydog57 yes I did its in the code Its line 20 in the form I've added brake points in the class at lines 24 and 37 sorry I don't know how to reference it here since I only added part of the code since that's what was suggested by the website when adding code. I think they wanted this to make it less confusing and to stop people from confusing a part of the code with something that not needed. – Clint Gaydon Sep 01 '19 at 20:47
  • @Flydog57 adding brackets after cdDrv an error comes up saying no definition for cdDrv – Clint Gaydon Sep 01 '19 at 20:48

1 Answers1

0
/*this was the best i could do couldn't find way for the class to pass the details i wanted this was the best i could find*/    

private void CheckDrive()
    {
        //string[] dvdDrives = new Cls_DVD_Player.DVDDrvCtrls();

        //List<string> drvNames = new List<string>();
        DriveInfo[] cdDrives = DriveInfo.GetDrives();
        cboDrive.Items.Clear();

        foreach (DriveInfo d in cdDrives)
        {
            if (d.DriveType == DriveType.CDRom && d.IsReady)
            {
                cboDrive.Items.Add(d.Name + " " + d.VolumeLabel);
                //MessageBox.Show(d.Name + " " + d.VolumeLabel);
            }
            else if (d.DriveType == DriveType.CDRom)
            {
                cboDrive.Items.Add(d.Name + " Drive Empty");
            }
        }
    }