C# Inconsistant Accessibility on public method (with ref) - c#

I searched some fora and topics to look for the answer but I'm not able to find the solution for my problem. I'll post the code:
namespace Configurator
{
public partial class Dialog : Form
{
private DataStorage dataStorage = null;
public Dialog
{
InitializeComponent();
}
public void setDataStorage(ref DataStorage ds)
{
this.dataStorage = ds;
}
}
}
And it's being used in this class:
namespace Configurator
{
public partial class MainView : Form
{
private DataStorage dataStorage = new DataStorage();
private Dialog DialogBox = new Dialog();
public MainView
{
InitializeComponent();
}
private void newObjectButton_Click(object sender, EventArgs e)
{
DialogBox.Show();
DialogBox.setDataStorage(ref dataStorage);
}
}
}
This is the error:
Inconsistent accessibility: parameter type 'ref Configurator.DataStorage' is less accessible than method Configurator.Dialog.setDataStorage(ref Configurator.DataStorage)

Mark your class DataStorage with public and your error will go away :)
Your class Dialog is public. Your method setDataStorage is also public. This makes this method visible to all other assemblies. But how can other assemblies use that method if they do not have access to the parameter type DataStorage because that one is not visible (probably because it is marked private or internal.)

Related

Can't use variable from public Form2 in private void?

namespace Bus
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
string selectedItem=listBox1.SelectedItem.ToString();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text=selectedItem;
}
}
}
When I run this, I get an error for selectedItem in private void saying that selectedItem does not exist in the current context.
Why is that? If I declare the string in public Form2(), shouldn't others be able to use it since it's public?
selectedItem is a local string for your Form2's constructor only. You need to either create it as a field or property for all components of your Form2 class to be able to access it.
define selectedItem as property or field:
namespace Bus
{
public partial class Form2 : Form
{
public string selectedItem {get; private set}
//private string selectedItem;
public Form2()
{
InitializeComponent();
selectedItem=listBox1.SelectedItem.ToString();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text=selectedItem;
}
}
}
there are a number of things you need to consider here
firstly any variable created inside a method can only be used inside that method
because of this you need to upgrade it from a local variable to a class variable
secondly how are are going to upgrade it it can be done in several ways
Field : this is generally used to things that are only needed inside the class not outside (though they can be declared public this is not best practice)
eg
public partial class Form2 : Form
{
private string selectedItem;
Property : Properties are used when fields need to be used outside the class or when you need addition control over what happens when data is got or set
eg
public partial class Form2 : Form
{
public string SelectedItem{get;set;}
Static : this is when you need to access the variable from every instance of a class not just the one its set in, static can be either a field or property
eg
public partial class Form2 : Form
{
public static string SelectedItem{get;set;}
which you need is dependent on what you need to accomplish
also just because a class is public doesn't mean everything on it is public,
you might have things on it that are only useful inside the class (private)
only in the class or its descendent (protected)
only inside the project (internal)
or accessible to everything (public)
so you must set the access on each method or property separately (if you don't it will default to internal), properties allow you to set this for both the get and the set separately so you can have a property that anything can read from but only the class itself can change
Well, selectedItem is a local variable and can be used locally only, within the constructor scope in your case. Convert it into property:
namespace Bus
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public String SelectedItem {
get {
if (null == listBox1.SelectedItem)
return "";
return listBox1.SelectedItem.ToString()
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text = SelectedItem;
}
}
}
You need to make it a class level field
namespace Bus
{
public partial class Form2 : Form
{
private string selectedItem;
public Form2()
{
InitializeComponent();
selectedItem=listBox1.SelectedItem.ToString();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text=selectedItem;
}
}
}

Static class object reference

I have one main form and a static class when i access static class member it gives me nullreference error . Previously it was working fine donot know what happend . Any one can suggest what is wrong.
code snap:
namespace MyNamespace
{
public partial class myForm : Form
{
public myForm()
{
InitializeComponent();
}
private void myForm_Load(object sender, EventArgs e)
{
My_Static_Data_Class.player_name="Demo Player"
}
}
public static class My_Static_Data_Class
{
public static string player_name = "";
}
}
please help?
You're either accessing the static class member before it was set to "Demo Player". For example, you are trying accessing My_Static_Data_Class.player_name in Program.cs code before it calls the main form from the Main[] method. Alternatively you're might be setting My_Static_Data_Class.player_name to null elsewhere in code and then accessing it.
Check all the references in code editor and follow it up. To do this, right click on My_Static_Data_Class.player_name in Visual Studio editor, and choose Find All References menu item.

specify a object on a public method

I have the following method
public partial class formTabelasPsi : Form
{
private Form1 Opener { get; set; }
public formTabelasPsi(Form1 opener)
{
this.Opener = opener;
InitializeComponent();
}
public static void publicmethod1(string path)
{
//some code related to path
}
}
I want publicmethod1 to check a checkbox whenever this formTabelasPsi runs it.
I tried to specify it using formTabelasPsi.checkBox1.Checked = true; but the code says a object reference is required.
Maybe this is a newbiez question for most of you, but honestly, as a amateur programmer I didn't find this clearly anywhere.
The checkbox belongs to an instance of that form, you need to reference that instance in order to update it
public void publicmethod1(string path)
{
this.checkBox1.Checked = true;
}
The method also needs to belong to an instance of the form, you can find out more about instances here

ASP.NET expose controls to other class

I have been battling this for some time and I need some guidance.
I'm coding in ASP.NET 4.0 WEBFORMS.
Question is: How to expose a textbox, Label or any other control to another class.
I have a webform (see below).
public partial class WebForm1 : System.Web.UI.Page
{
}
This is then referenced and sent to another class.
public class SearchInitializer
{
private WebForm1 _webform1;
public SearchInitializer(WebForm1 Webform1)
{
_webform1 = Webform1;
}
public void ChewSettings()
{
_webform1 //can't find any control in here?!
}
}
First I thought of creating a public property which I thought I could access from the reference I sent to the new class.. But nooo!
public partial class WebForm1 : System.Web.UI.Page
{
public string KeywordBox1
{
get {return txt_keyword.Text;}
set {txt_keyword.Text = value;}
}
}
The I tried to inherit the Webform into the other class. Making the the property available but no luck there.
public class SearchInitializer : Webform1
{
private WebForm1 _webform1;
public SearchInitializer(WebForm1 Webform1)
{
_webform1 = Webform1;
}
public void ChewSettings()
{
_webform1 //can't find any control in here?!
}
}
Okay an abstract class migth be of use here, inheriting everything. But I think I got that wrong to. I have events and static classes, so they can talk with the page. But I really would like not to use a static class as a container to save all the info in my controls.
So these are the examples I have tried and they all failed. So this is me basicly trying to expand what I know ;) Thanks for reading!!
Why have they failed and how should I do it?
EDIT AS REQUESTED!
public partial class WebForm1 : System.Web.UI.Page
{
protected void btn_click(object sender, EventArgs e)
{
SearchInitializer searchIni = new SearchInitializer(this);
}
}
To expose the controls there are two methods I can think of that you can employ.
You can remove the following statement from the myPage.designer.cs file and place it in your code behind as a public declaration:
protected global::System.Web.UI.WebControls.TextBox myTextBox;
becomes
public System.Web.UI.WebControls.TextBox myTextBox;
This should make it immediately accessible. My preferred method is to add a property for each specific control that you want to provide access to.
public System.Web.UI.WebControls.TextBox MyTextBoxElement
{
get
{
return myTextBox;
}
}
This allows to provide supplementary access controls if you need them or other conditionals. In any case, to access either the field or the property, the consuming object must reference this by your specific page type.
Not sure what you are trying to do, but to access a base class within an inherited calss you need to use the base keyword, not declare an instance there of.
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string KeywordBox1
{
get { return txt_keyword.Text; }
set { txt_keyword.Text = value; }
}
}
public class SearchInitializer : WebForm1
{
public SearchInitializer()
{
}
public void ChewSettings()
{
// Works
base.KeywordBox1 = "Red";
}
}
If intellisense is not showing the property, try rebuilding the solution. It will then refresh the list of available properties and it should show.
Your original approach must work. I suggest you create a small test project with a form with text box and SearchInitializer class and see that it works, after that figure out what is different in your current project.

Using an Instantiated Class in another Class

Please excuse my ignorance, I am transitioning from VB6 to C# (very steep learning curve). I have googled this to death and I am not able to figure this out. I instantiated a Class on my main form:
namespace App1_Name
{
public partial class Form1 : Form
{
public cConfig Config = new cConfig();
}
}
In my Config Class I am instantiating two other classes:
namespace App1_Name
{
class cConfig
{
//Properties
public cApplication Application = new cApplication();
}
}
In my cApplications Class I have the following:
namespace App1_Name
{
class cApplication
{
//Properties
public string ApplicationName { get { return "App Name"; } }
}
}
So in another class I am looking to use the class I instantiated on Form1 as so:
namespace App1_Name
{
class cXML
{
public void Method1()
{
Console.WriteLine(Config.Application.ApplicationName);)
}
}
}
But I am getting an error that states "Config" doesn't exist in the current context, what am I doing wrong here?
Don't you miss instance of Form1?
Form1 form = new Form1();
Console.WriteLine(form.Config.Application.ApplicationName);
because you are working with properties... not static classes and static methods.
I think you want this:
Console.WriteLine(Form1.Config.Application.ApplicationName);
EDIT: Dampe is correct; you need an instance of Form1, since Config is not a static member of the class. Please refer to his answer.
All of the above, or a one-liner:
Console.WriteLine(new Form1().Config.Application.ApplicationName);
Ok, in order to get my original code to work I made the cApplication Static. This is because I was instantiating the cXML in Form1's Form_Load event so the above solutions just created an endless loop.
What I did to resolve the issue was to pass the Config Class as a reference to the cXML class as follows:
namespace App1_Name
{
public partial class Form1 : Form
{
public cConfig Config = new cConfig();
}
private void Form1_Load(object sender, EventArgs e)
{
cXML XML = new cXML();
XML.Config = Config; //Passing the Config Class by Reference to cXML
}
}
In the cXML class I am doing the following:
namespace App1_Name
{
class cXML
{
public cConfig Config;
public string AppName
{
Console.WriteLine(Config.Application.AppName);
}
}
}
This does exactly what I originally set out to do. My new question is is this an acceptable way of doing this?

Categories