I am putting together an application but I'm getting a strange issue where i can't use any methods from a class i've created with a couple of methods, the methods don't do anything at the moment because I'm just getting the shell of the program in place. I am trying to call from the Form1 class below, specifically from a button click checking a specific operation from radio buttons.
If btnDeviceControlAccept_Click is clicked it checks which of the radio buttons and goes to a method in the DeviceControlMethods class such as Add, Change or Delete VLAN. When i use the object (dc, DeviceControlMethods dc = new DeviceControlMethods();)I created in the Form1 i'm unable to use the methods even if the class is public or if i set the methods to static and use DeviceControlMethods.AddVlan etc.
I'm sure I'm just doing something daft because I've not doing C# in quite a while.
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;
namespace MFT___Configurator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnDeviceControlAccept_Click(object sender, EventArgs e)
{
DeviceControlMethods dc = new DeviceControlMethods();
if (rbAddDevice.Checked == true)
{
dc.CreateVlan() // the method is not found
resutlBox.Clear();
}
else if (rbChange.Checked == true)
{
resutlBox.Clear();
}
else if (rbDelete.Checked == true)
{
resutlBox.Clear();
}
else
{
resutlBox.Clear();
resutlBox.Text = "Select a valid operation; Add, Change or Delete.";
}
}
Class with the methods i want to call;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MFT___Configurator
{
public class DeviceControlMethods
{
static DeviceControlMethods()
{
string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
string ChangeVlan()
{
return "";
}
void DeleteVlan()
{
}
}
}
}
I see only private methods, you need to make them public explicitly, not only the class. See the docs about access modifiers
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or struct.
protected
The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.
private protected
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.
Edit
And, as other comments state as well, methods defined in the static constructor won't be accessible either.
You have scoping issues with your class. Read through this article to learn more about scoping in C#. https://msdn.microsoft.com/en-us/library/ms973875.aspx
But to solve your issue, change your class to be as follows:
public class DeviceControlMethods
{
public string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
public string ChangeVlan()
{
return "";
}
public void DeleteVlan()
{
}
}
Related
I found this code in stackOF but it doesn't work at all and i can't fix that.
would you tell me what's wrong with this code?
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.Text);
}
}
}
i think your problem because you named your program with same name of class that copy text to clipboard
take look at this code
using System.Windows.Forms;
namespace Clipboard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Clipboard.SetText(textBox1.Text); This will not work if you named your namespace Clipboard !
System.Windows.Forms.Clipboard.SetText(textBox1.Text); // you should use this way to confirm you need to access to clipboard not your namespace
}
}
}
i named my program with same name of class(Clipboard)
and i have problem now because the compiler confusing between your program and class that copy a text
So the best way is to specify a unique name each time you create a program :)
There is a namespace conflict with your own. You can either explicitly use the exact Clipboard.SetText() method using the full declaration as per #WaleedKhaled's answer:
System.Windows.Forms.Clipboard.SetText(textBox1.Text);
or else the using statement at the top of your example to say something like:
using WinForms = System.Windows.Forms;
then your line would read:
WinForms.Clipboard.SetText(textBox1.Text);
I've tried to solve this problem that I have but I really don't know what else to do. I get this error:
Inconsistent accessibility: field type 'ChatClient.Configurator.IPChangeHandler' is less accessible than field 'ChatClient.Configurator.IPChange'
and this is part of the code:
namespace ChatClient
{
public partial class Configurator : Form
{
public delegate void IPChangeHandler(object sender, IPAddressInfoEventArgs e);
public event IPChangeHandler IPChange;
// ...
}
}
making delegate and class public did not worked out.
Thank you!
Check accessible level of IPAddressInfoEventArgs class.
It must be public since the event IPChange is public too.
Please Go to Solution Part ..& Open the IPAddressInfoEventArgs.cs file ....
Update in that File....
using System;
using System.Collections.Generic;
using System.Text;
namespace ChatClient
{
public class IPAddressInfoEventArgs : EventArgs
{
private string _ipAddress;
public IPAddressInfoEventArgs(string ipAddress)
{
this._ipAddress = ipAddress;
}
public string IPAddress
{
get { return this._ipAddress; }
}
}
}
I'm terribly new so I might be completely off track overall with what I'm trying to do.
I don't really know how to ask the question, my english is a bit rocky.
But I have 2 files one containing this:
frmMain.cs
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 frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Class1 cls = new Class1();
cls.Visibility();
}
}
}
And another file containing this:
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public partial class Class1
{
public void Visibility()
{
frmMain c = new frmMain();
c.label1.Visible = false;
}
}
}
What I'm trying to get is that when I'm running the program and clicking label1 I want it to disappear.
But it doesn't. I don't get any errors or anything.
Any help is appreciated :).
Thanks in advance.
First: Why are you trying to let the label on your mainform dissappear by using another class?
I would suggest the following:
private void label1_Click(object sender, EventArgs e)
{
label1.Visible = false;
}
I think the reason why your code isn't working is that inside the function Visibility() of Class1 you are creating a new frmMain and on that frmMain you are setting the visible property of label1 to false. So you are actually working with a different form.
You are instantiating a new, separate form. This means the label is being hidden.. but on a hidden form you have created.
You need to pass the current form instance into your other class:
public void Visibility(frmMain mainForm) {
mainForm.label1.Visible = false;
}
Then call it like this:
new Class1().Visibility(this);
What you're doing is you're creating a second instance of your window (which might not be obvious to you, as you're not displaying it). Then you are hiding the label in your second window. Probably not what you intended in the first place.
What you need to do is to pass a reference to your original form to the method you're calling, or (depending what you want to do) a reference to the control you need to hide:
in Class1:
public void Visibility(Control controlToHide)
{
controlToHide.Visible = false;
}
in frmMain.cs
new Class1.Visibility(this.label1)
few more comments:
Naming: do not use names like Class1, label1; I appreciate this is probably
just 'play around with' kind of code, but such names are completely
unreadable when you try to come back to your code later (or get
someone else to have a look)
Naming 2: try to name your methods to describe what they will do - HideControl, or HideLabel is much better than Visiblity
You may want to read some basic C# tutorials to learn about references, instances, parameters, etc.
Other than that, happy C#-ing :)
You do not want to let Class1 know about frmMain. Change it to something like this:
public class Class1
{
public bool GetVisibility()
{
return false;
}
}
And from your form, call it like this:
private void label1_Click(object sender, EventArgs e)
{
Class1 cls = new Class1();
this.Label1.Visible = cls.GetVisibility();
}
Your current implementation of Class1 initializes a new frmMain, hides that form's Label1, does not do anything with that instance (e.g. it does not Show() it) and then returns, not affecting the already instantiated and shown frmMain instance (the one you instantiate Class1 from).
You can change this by passing the label or even the form into Class1, but that is just bad design.
You may change your code this way:
public partial class FrmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
new Class1().Visibility(this);
}
public void Go()
{
this.label1.Visible = false;
}
}
Then
public partial class Class1
{
public void Visibility(FrmMain form)
{
form.Go();
}
}
You are setting the visibility of the label of another form (one that's not being displayed.
this line of code in the Visibility method creates a new object
frmMain c = new frmMain();
It's of the same type as the form being display, but it's a different object, that's not displayed. If you insert a line after the above
c.Show();
you should see the newly created form and also see that the label disappears
However there's a straight forward fix to your problem. Change the event handler to this
private void label1_Click(object sender, EventArgs e)
{
((Label)sender).Visible = false;
}
The sender object is the control that was clicked, and since the event is attached to the label it self. All you need to do is cast the sender to the type of a Label and then you can access the visible property. Alternatively you could do this:
private void label1_Click(object sender, EventArgs e)
{
this.label1.Visible = false;
}
That uses the current object (aka this) and gets the label from that object.
I am trying to create a setup for a class I am making so when it is created a manager class can be setup for it, and it might require to call functions in that class via an interface
These calls are not always required, and the manager may not always be the class that called this class, so a simple return value and use it form the manager class does not meet the requirements.
What I am trying to do is following code. (Tried to strip as much unnecessary out as possible)
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
{
interface Itester
{
void LoadTest();
}
public partial class Form1 : Form, Itester
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
World testWorld = new World();
testWorld.SetManager(this);
testWorld.InitializeWorld();
}
void Itester.LoadTest()
{
//random action I want run
label1.Text = "Ran LoadTest()";
}
}
public class World
{
Itester worldManager;
public World()
{
InitializeWorld();
}
private void InitializeWorld()
{
worldManager.LoadTest();
}
public void SetManager(Itester test)
{
worldManager = test;
}
}
}
I get this error based on it. The error refer to the "public void in the World class"
Error 1 Inconsistent accessibility: parameter type 'WindowsFormsApplication1.Itester' is less accessible than method 'WindowsFormsApplication1.World.SetManager(WindowsFormsApplication1.Itester)' XYZ Location Form1.cs 316 21 Feudal World
What I would have expected to happen was that.
Form1 class creates a local instance of the World Class, then runs its constructor (does nothing), then it sets itself as its Manager (could in theory be another class implementing the Itester interface), finally it calls the World Class again and ask it to initialize the world, where I would have expected the world class to call the Form1 instance and have it update the label on the button.
lvl1:Form1 -> lvl2:World -> lvl3:Form1(or other manager) -> return void to lvl2:World -> return void to lvl1:Form1
What am I missing, why does this not work?
Your interface does not have accessibility modifier, so it is assumed internal. Change it to public:
public interface Itester
{
void LoadTest();
}
The reason you need to do this is because the method SetManager is public, thus anyone that can consume the SetManager method must also be able to see the interface.
now I just need to figure out why it works
Consider that SetManager can be called from any assembly because it is public. So if someone were creating an assembly, they could reference yours and call worldInstance.SetManager. Let's call that assembly BigHappyAssembly.
Now consider the first parameter of SetManager is ITester, however it is internal. When BigHappyAssembly tries to call SetManager, it would be presented with a problem: What am I suppose to give as the first parameter? It doesn't have access to the ITester type, so it doesn't know that is what the first parameter is.
To prevent this happening in the first place, the compiler stops you from introducing this problem. It's warning you that you have created a public member, that anyone can call, however not everyone will be able to know what the parameters are.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.ComponentModel;
namespace ConsoleApplication1
{
class BreakingChange
{
delegate void SampleDelegate(string x);
public void CandidateAction(string x)
{
Console.WriteLine("Snippet.CandidateAction");
}
public class Derived : BreakingChange
{
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
static void Main()
{
Derived x = new Derived();
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
factory("test");
}
}
}
\Program.cs(32,38): warning CS1707: Delegate 'ConsoleApplication1.BreakingChange.SampleDelegate' bound to 'ConsoleApplication1.BreakingChange.Derived.CandidateAction(object)' instead of 'ConsoleApplication1.BreakingChange.CandidateAction(string)' because of new language rules
\Program.cs(23,25): (Related location)
\Program.cs(16,21): (Related location)
Question:
I know what causes this warning and know the reason behind it. However, I don't know what the
best way to fix it?
1> Redefine the function (i.e.) change the function signature
2> Can we explicitly call BreakingChange.CandidateAction in the following line?
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
Well, there are multiple ways to "fix" this, depending on what you want to, and can, do.
Personally I would add another overload to Derived that took a string, since you're going to have the same issue with non-delegate calls as well.
public class Derived : BreakingChange
{
public new void CandidateAction(string x)
{
base.CandidateAction(x);
}
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
Or, since you know you want the base class method, you can cast the reference x:
new SampleDelegate(((BreakingChange)x).CandidateAction)