C# Type Expected Error

I am trying to work with two radio buttons in a simple C# app that allows a user to select and open a PDF using the Process object. One PDF will be assigned to each radio button. The code is shown below. I get the following errors:  (1) "Type expected" and (2) "Cannot convert method group ‘OpenFile’ to non-delegate type ‘string’. Did you intend to invoke the method?" Please help me resolve the error.

The following line causes the error (line 24 in the second code box):
cmd = commands.OpenFile;

Open in new window


Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CSh_Test1
{
    class commands
    {
        public void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
    }

Open in new window

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test1
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
      
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
            string fileName = "";
            string cmd;
            cmd = commands.OpenFile;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmOpenPDF_Load(object sender, EventArgs e)
        {

        }
        }
    }

}

Open in new window

The following code runs without any errors.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test2
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
        private void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            OpenFile(fileName);
        }
        }
    }

Open in new window

Mark01Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
cmd = commands.OpenFile("c:\test_3\johnson.pdf");

You need to pass in a string.
0
AndyAinscowFreelance programmer / ConsultantCommented:
class commands
    {
        public void OpenFile(string fileName) <<---- HERE
        {

That defines a function (method) which requires a string as a parameter and returns nothing.
You need something like the following

        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
            string fileName = "";  //Put the name of the file you want to open here
           // string cmd;
            //cmd = commands.OpenFile;
commands.New().OpenFile(fileName);
        }
0
Mark01Author Commented:
I get an "Unrecognized escape sequence" error. I am trying call the OpenFile function.
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Fernando SotoRetiredCommented:
It should be
cmd = commands.OpenFile("@c:\test_3\johnson.pdf");

Open in new window

0
NorieAnalyst Assistant Commented:
Mark01

If your filepath contains single \ you either need to double them up, or precede them with @, as you have in the last piece of code from you original post.
0
Fernando SotoRetiredCommented:
They it like this
cmd = commands.OpenFile(@"c:\test_3\johnson.pdf");

Open in new window

0
Mark01Author Commented:
I am trying call the OpenFile function and add the following code which will be modified:

              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            OpenFile(fileName);

Open in new window

0
AndyAinscowFreelance programmer / ConsultantCommented:
Please read my comment.  It explains why you got both errors you did and how to correct them.
0
Mark01Author Commented:
@Fernando Soto

Code:
cmd = commands.OpenFile("@c:\test_3\johnson.pdf");

Open in new window

Errors:
(1) Unrecognized escape sequence
(2) An object reference is required for the non-static field, method, or property

Code:
cmd = commands.OpenFile(@"c:\test_3\johnson.pdf");

Open in new window

Error:
(1) An object reference is required for the non-static field, method, or property.

@AndyAinscow: I read your comment but I don't have enough experience to write the code that uses two radio buttons.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Major problem.
I don't like to say this but most of the other code snippets you have been given won't work.  You MUST understand the very basics of the language and writing code.

To modify your original code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test2
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
        private void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            OpenFile(fileName);
        }
        }
    }

Open in new window


into this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test2
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            commands.New().OpenFile(fileName);
        }
        }


    class commands
    {
        public void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
    }
    }
0
Mark01Author Commented:
@AndyAinscow: Project CSh_Test2 runs without errors. I just ran it. The code came from EE questions.

Here is some similar code:
https://www.experts-exchange.com/questions/29084081/C-Return-Type-and-Identifier-Expected-Errors.html
0
AndyAinscowFreelance programmer / ConsultantCommented:
>>Project CSh_Test2 runs without errors. I just ran it.

By that you mean the code in my previous comment is working OK.  Now read my first comment again and see just what I did to make it work.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Actually I have just realised I'd also got a mistake

it should be
            new commands().OpenFile(fileName);
not
commands.New().OpenFile(fileName);


for completeness:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test2
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            new commands().OpenFile(fileName);
        }
        }


    class commands
    {
        public void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
    }
    } 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark01Author Commented:
@AndyAinscow: I just created a new project CSh_Test4 and I'm working through the errors as I paste your code from 42528098. In my comment (42528076), I was referring to the project CSh_Test2 code in this question, not to your code in 42528060.
0
Mark01Author Commented:
I created a new project CSh_Test4 and I pasted AndyAinscow's code from 42528098 into it. I get an "The type or namespace name 'commands' could not be found (are you missing a using directive or an assembly reference?)" error.

The following line causes the error (line 26 in the second code box):
new commands().OpenFile(fileName);

Open in new window


Full Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test4
{
    class Commands
    {
        public void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSh_Test4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
            if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            new commands().OpenFile(fileName);
        }
    }
}

Open in new window

To clarify my previous comments, the following code runs without any errors. The code came from previous EE questions.

Here is some similar code:
https://www.experts-exchange.com/questions/29084081/C-Return-Type-and-Identifier-Expected-Errors.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSh_Test2
{
    public partial class frmOpenPDF : Form
    {
        public frmOpenPDF()
        {
            InitializeComponent();
        }
        private void OpenFile(string fileName)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UseShellExecute = true;
            psi.FileName = fileName;
            p.StartInfo = psi;
            p.Start();
        }
        private void btnOpenPDF_Click(object sender, EventArgs e)
        {
              string fileName = "";
            if (radJohnson.Checked)
                fileName = @"c:\test_3\johnson.pdf";
          if (radRoss.Checked)
                fileName = @"c:\test_3\ross.pdf";
            OpenFile(fileName);
        }
        }
    }

Open in new window

0
AndyAinscowFreelance programmer / ConsultantCommented:
you changed commands into Commands in your class definition
0
Mark01Author Commented:
Summary: The comments/code in 42528098 solved the problem.

Thank you, AndyAinscow, Fernando Soto, Kyle Abrahams and Norie. Moderator: With the interface changes, I wasn't able to give points to more than one person.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.