How do I access a public class property value in a code module other than the one where it was created?

I have created a simple public class and instantiated it in module 1.

public class Cashflow
{
    public int x = 100;
}

// code in module 1
Cashflow cf;
cf = new Cashflow();
a = cf.x

// code in module 2
b = cf.x;

I simply want to make the property value visible to separate code modules in application.

Recommended Answers

All 4 Replies

since you didn't post all the code it's hard to tell, but I'm guessing the the line "Cashflow cf" is in a method. This makes 'cf' only available to that method. Post more code.

Thanks, here is all code. File 1 defines the class Cashflow. File 2 successfully instantiates an instance of Cashflow as CF. In File 3 I try to access the object CF created in File 2 and get the error (very last line of code).

Source Code File 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Cashflow
{

    public int x = 100;

}

File 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
     static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
         [STAThread]


        static void Main()
        {
            int y;
            Cashflow cf;

            // var cf = new Cashflow(); //instead of declaring as variable above
            //cf = new Cashflow();
            //y = cf.x;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());

            cf = new Cashflow();
            y = cf.x;
        }
    }
}

File 3

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 WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //Cashflow cf;

            DateTime saveNow = DateTime.Now;
            string dtString;

            InitializeComponent();

            // do calcs and file work here

            this.textBox1.Text = "456";

            dtString = saveNow.ToString();
            this.textBox2.Text = dtString;

        }

        public void MainForm_Load(object sender, EventArgs e)

            // do calcs and file work here

        {

        }

        public void button1_Click(object sender, EventArgs e)
        {
            int y;

            y = cf.x;
            
        }
    }
}

As I said, cf was declared inside the method Main(), so it is only available to the method Main. You need to understand Scope. Yes, it's long and complicated, but you really need to understand it if you are going to program in any OO language.

Thanks Momerath, trying to make the jump from structured to oop. Where outside of main? I have attempted to define cf outside of main within WindowsFormsApplication1 namespace but that produces errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.