简体   繁体   中英

Passing Variables From Cs File to another Cs File in C#

I am trying to pass the m_paths value from the class : ExtractDescriptorsForm.Cs to VisualizeFrom.Cs .. This is how the variable is defined in ExtractDescriptorsForm.Cs :

namespace MediaLab.TopSurf
{
    public partial class ExtractDescriptorsForm : Form
        {
            // list of paths that point directly at images, or at
            // directories that contain images
            List<string> m_paths;
            public List<string> Paths { get { return m_paths; } }
     ......}}

What I wrote in VisualizeForm.Cs :

List<string> j = ExtractDescriptorsForm.m_paths;

The error is :

an object reference is required for the non-static field,method or property 'MediaLab.TopSurf.ExtractDescriptorsForm.m_paths'

To start, I need to make sure you understand something very fundamental here:

Files have nothing to do with this!

You're working with class objects , not files. You can have more than one ExtractDescriptorsForm showing the on the screen at the same time, and there's no reason this form needs to be in a file named ExtractDescriptorsForm.cs . In fact, you could put both the ExtractDescriptorsForm and the VisualizeFrom class definitions in the same file.

Learn this, and learn it well, or you'll struggle to ever be effective writing code.

Case in point... for this issue, the VisualizeFrom object is trying to look for the Paths property in a ExtractDescriptorsForm object... but which object? You're using class name here, when you need to be thinking about instances of the class.

I see the comments that you're trying to create a new ExtractDescriptorsForm object. This is probably a mistake. You very likely already have an instance in your program somewhere that you're already using, possibly created by code that Visual Studio provided for you. You need to find that existing reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM