[c#] Access control on form from another .cs file

c0rrupt

Weaksauce
Joined
Jun 25, 2004
Messages
102
I have my form, fMP3mainFrm, which has a Windows Media Player control called WMP. From my other .cs file Program.cs (which is going to contain most of my program code, the fMP3mainFrm.cs file is for form specific code) I want to be able to access controls on the fMP3mainFrm form and use them pretty much like if I were doing the coding in the fMP3mainFrm.cs file itself. Only problem is that I can't figure this out. In VB, I could just do fMP3mainFrm.WMP.URL or fMP3mainFrm.TextBox1.Text, etc etc. How is this done in C#?
 
Yep; just use a scoped name: namespace.class.member

Files aren't really relevant -- just the namespace.
 
That still doesn't work. I tried forcemp3.fMP3mainFrm.WMP but it doesn't exist. I have the WMP control on my form. I also tried forcemp3.fMP3mainFrm.TextBox1 and forcemp3.fMP3mainFrm.Button1 but those controls don't show up either. I made my forcemp3 class and my fMP3MainFrm class public. What's going on?
 
Really: namespace.class.member is the way things are scoped. You need a reference to an instance of the class if the member isn't static.

I can't guess what is the correct name for what you want unless I see your code.

Is fMP3mainFrm the name of the class for your form, or the name of a variable that contains a reference to your other form?
 
When I try to run the app, I get this error.
Error 1 'forceMP3.fMP3mainFrm.WMP' is inaccessible due to its protection level

fMP3mainFrm
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace forceMP3
{
    public partial class fMP3mainFrm : Form
    {
        public fMP3mainFrm()
        {
            InitializeComponent();
        }
    }
}

Program.cs
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace forceMP3
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new fMP3mainFrm());
        }

        public static void MediaInfo()
        {
            string Artist = forceMP3.fMP3mainFrm.WMP.currentMedia.getItemInfo("Artist");
        }
    }
}
 
is forceMP3.fMP3mainFrm.WMP marked as private, protected or internal?
 
The complete source code. What you've posted doesn't contain a declaration for WMP.

But I'm with HJB: the error indicates that the access modifier on your WMP member isn't correct. Or, on the containing class.

This isn't a runtime error; it's a compile-time error.

You can get help for an error by placing the cursor on the error in the output window and pressing F1. For error CS0122, the help explains the problem and has a link to a topic about access modifiers.
 
Ok, it turns out hte WMP control was private. I changed it to public but now im getting this error.

Error 1 An object reference is required for the nonstatic field, method, or property 'forceMP3.fMP3mainFrm.WMP'
 
Sounds like you've coded class.member reference when the member isn't a static in that class. Either make the member static (so that the member exists even if no instance of the class has been created) or create an instance of the class and use reference.member to get to the members of that reference.

This is pretty basic OOP stuff, thought it has a C# spin. Maybe you're better off getting help from someone face-to-face. It'll be a lot easier for that person to help you since they can see your error messages and all your code.
 
I think you're trying to use an instance property or method as a static one.
new the object 1st.
E.x.:
Artist = new forceMP3.fMP3mainFrm.WMP().currentMedia.getItemInfo("Artist");
assuming WMP is a class with a default constructor.
 
Back
Top