1

I have this C# class:

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

namespace Handler.FTPClient
{
    public class FTP
    {
        private static string _message = "This is a message";

        public static String message
        {
            get { return _message; }
            set { _message = value; }
        }

        public FTP()
        {
        }

        public static String GetMessage()
        {
            return message;
        }
    }
}

And am trying to call it with this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Handler.FTPClient;

namespace swift_ftp
{
    public partial class FTP : Form
    {
        FTP ftp;

        public FTP()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

            Application.Exit();
        }

        private void FTP_Load(object sender, EventArgs e)
        {
            ftp = new FTP();
            MessageBox.Show(FTP.message);
        }
    }
}

But for some reason, I am unable to call the property, is there something I have not done? I have also done this without static and got the same result -_-

Sorry for the stupid and basic question, it's just been such a while since I have done C#!

(I have added the ref to the second project which is where the ftp class library is held)

mason
  • 31,774
  • 10
  • 77
  • 121
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • 6
    You have two classes with the same name. Try `ftp = new Handler.FTPClient.FTP()` to make sure you instantiate the right class. – Gerald Schneider Dec 07 '15 at 14:54
  • @Rik This problem is not the partial class. The FTPClient class is not part of the other FTP class, they only have the same name. One is instantiated in the other. – Gerald Schneider Dec 07 '15 at 15:00

2 Answers2

5

You have two classes with the same name. Try

ftp = new Handler.FTPClient.FTP() 

to make sure you instantiate the right class.

Don't forget the variable in the class itself:

 Handler.FTPClient.FTP ftp;

You should avoid the same name for different classes for exactly this reason. Since your FTP class inherits from a Form I assume it is a window ... so refactor it to "FTPWindow" and you have less problems and the code is easier to read.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
2

Partial classes are only possible in the same assembly and the same namespace. Your (partial) classes reside in different namespaces.

See https://msdn.microsoft.com/en-us/library/wa80x488.aspx

Rik
  • 28,507
  • 14
  • 48
  • 67