2boolORNOT2bool
2boolORNOT2bool

Reputation: 567

Problem with Encapsulation in C# (inaccesible due to protection level)

I am very new to C# and am stumbling through. I understand the need for encapsulation but whenever I break a working application into diffrent classes I always have problems. I have written a simple program that allows a user to click checkboxes on an inventory list. The items are shown in a textbox and the contents of the textbox are emailed to a pre-defined address when a submit button is clicked. The line...

oMsg.Body = Form1.textBox1.text

Gives me the Error: "MY_App.Form1.textBox1.text is inaccesible due to its protection level". Form1 and Class1 are as follows...

namespace MY_App 
{ 

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

List<string> ls = new List<string>(); 

private void Checkbox1_CheckedChanged(object sender, EventArgs e) 
{ 
            ls.Add( "P.C. "); 
} 
private void Checkbox2_CheckedChanged(object sender, EventArgs e) 
{ 
        ls.Add( "WYSE Terminal" ); 
} 
private void Checkbox3_CheckedChanged(object sender, EventArgs e) 
{ 

    ls.Add("Dual Monitors ");  
} 
public void button1_Click(object sender, EventArgs e) 
{ 
    InputText(); 
    Class1.SendMail(textBox1.Text);

} 
public void textBox1_TextChanged(object sender, EventArgs e) 
{ 

} 
public void InputText()
    {
        string line = string.Join(",", ls.ToArray());
        textBoxTEST.AppendText(line);
    }

And then the Emailing class (Class1)…

using System;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace MY_App
{
    public class Class1: Form1
    {
        public void SendMail(string[] args)
        {
            try
            {
                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem
                (Outlook.OlItemType.olMailItem);

                Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("[email protected]");
                oRecip.Resolve();
                oMsg.Subject = "Deskstop Standards: Required Items";
                oMsg.Body = Form1.textBox1.text
                oMsg.Display(true);  
                oMsg.Save();
                oMsg.Send();
                oRecip = null;
                oMsg = null;
                oApp = null;
            }

             catch (Exception e)
            {
                Console.WriteLine("{problem with email execution} Exception caught: ", e);
            }
             return;
        }
    }
}

I relize this is a ton of code but I don't know where to isulate the problem. Also, I greatly appreciate any help but if anyone could explain the answer as well so I don't continue writing bad code I truely would appreciate it. Thanks in advance.

Upvotes: 0

Views: 809

Answers (2)

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

There are a couple of problems here. The first is you are trying to have Class1 inherit from Form1, but you want to use the values from an instance of Form1 inside an instance of Class1, rather than from the instance of Class1.

Let me see if I can explain this using your code names. Suppose you have a builder that builds homes using prebuilt parts (ie, the house is shipped to you whole). He has a base house called Form, a more specific house design called Form1 that is based on Form and an even more specific design Class1 that is based on Form1.

Your neighbor buys a Form1 house and you buy a Class1 house. you each have a mailbox called a TextBox1. You are at your house and want to read your neighbors mail out of his textbox1. Since your neighbor is down the street, this is impossible.

A bit convoluted? Sure.

For proper design of your application, Form1 inheriting from Form is correct. Class1 inheriting from Form1 is not, unless Class1 is going to actually be a Form in the application. If it is a mail sender, you should create a class more like this:

public class Class1
{
    public void SendMail(string args)
    {
        try
        {
            var oApp = new Outlook.Application();
            var oMsg = (Outlook.MailItem) oApp.CreateItem(Outlook.OlItemType.olMailItem);
            var oRecip = (Outlook.Recipient) oMsg.Recipients.Add("[email protected]");
            oRecip.Resolve();
            oMsg.Subject = "Deskstop Standards: Required Items";
            oMsg.Body = body
            oMsg.Display(true);
            oMsg.Save();
            oMsg.Send();
            oRecip = null;
            oMsg = null;
            oApp = null;
        }
        catch (Exception e)
        {
            Console.WriteLine("{problem with email execution} Exception caught: ", e);
        }
        return;
    }
}

i would actually use System.Net.Mail instead of Outlook, but that is another lesson. To call this, you use the following from Form1:

 Class1 mailer = new Class1();
 mailer.SendMail("This is my body message");

Upvotes: 2

sll
sll

Reputation: 62544

As long as Class1 is inherited from Form1 and text box access modifier is protected you can simply access it:

oMsg.Body = this.textBox1.text;

If textBox is private - expose a wrapper for the textbox Text property and then use it:

public partial class Form1
{
   public string TextWrapper
   { 
     get
     {
       return this.textBox.Text;
     }

  set
     {
         this.textBox.Text = value;
     }
}

And use it:

 oMsg.Body = this.TextWrapper;

PS: your original code looks wrong to me because you'are accessing textbox as type member of Form1 class, basically like a static variable.

Upvotes: 1

Related Questions