I like to connect the DLL (which contains namespace Chart) and call the subroutine public Simple() from the second class (namespace WinApp2). There is a error:
couldn't find type or namespace name Simple
Although I consider a reference and put using Chart and created a instance in the second namespace.
namespace Chart
{
public partial class Simple : Form
{
Manage _mProject;
public Simple()
{
InitializeComponent();
_mProject = new Manage();
_mProject.Add(new Task() { Name = "New Task" });
_mChart.Init(_mProject);
}
}
}
using Chart;
namespace WinApp2
{
public partial class Form4 : Form
{
Form2 fh;
public Form4(Form2 aufrufer)
{
fh = aufrufer;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var instance = new Simple(); (error !!!)
instance.Simple();
}
}
}
What do you mean by "consider a reference and put using Chart" ? Did you actually include the dll in References section, or did you put using Chart and think that will reference the relevant dll?
The proper steps are:
Right click References section in VS
Choose Add Reference
Browse to location of relevant dll, choose OK
Another cause may be that Chart is a defined class in System.Web.DataVisualization.dll, so VS is confused. Try changing your Chart namespace to something else, e.g., MyChart
Hope this helps..
Related
UPDATE: I tried working out by making every class in Code public, but it doesn't seem to accept my class hierarchy.
This is my first post so please bear with me. Besides that, i'm a pretty big noob, so do excuse me if something dumb comes along
I'm currently doing a school project and I have a fully fleshed out .NET Framework project in Visual Studio. Now I have to visualize it with a WPF app. So I made a new WPF project in the same source.
Let's respectively call them Code and Visualization.
I've given Visualization a project reference to Code and put using Code; at the top of the XAML.cs
I made a button in Visualization and I want it's OnClick event to use
Code.Start();
What somehow seems to sort of work is making every class in Code public, but I don't remember that being a good practice, but do correct me if i'm wrong!
I've put multiple hours into finding a solution with none to be really found before. Seeing as nobody seems to have posted this question before I must be missing something really simple.
Cheers!
namespace Code
{
class Program
{
public void Start()
{
/// Do something
}
}
}
using Code;
namespace Visualization
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
// Use the Start() function from Code
}
}
}
To use classes and their functions and properties from another project, you have to declare them as public.
namespace Code
{
public class Program
{
public void Start()
{
// Do something
}
}
}
Then go to the other project, right click > Add > Reference > select your project containing the code above (assuming you're using Visual Studio IDE). After, you can access the public functions and properties:
using Code;
namespace Visualization
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
Program p = new Program();
p.Start();
}
}
}
If you are worried about security, then ensure that public functions and properties you deem are safe to expose to other projects. For example, what we just did to use the Start function, any other project or some 3rd party program could do also. The only difference is that a reference would be made to the project's .dll produced instead of the project itself.
A basic rule of thumb (at least for me) is that if there are anything I don't want to expose, then don't make them public and have a public function that can be called to perform different actions. This way I can limit what actions and information can be performed or accessed:
//within some project
namespace Code
{
public class Program
{
// can't be access from another project directly
private string _privateText { get;set; }
// can be accessed directly
public string PublicText { get;set; }
public void Start()
{
// Do something
}
public string getPrivateText()
{
// here you can limit what actions are done and what information to return
return _privateText;
}
}
}
You can then do the following:
// within another project
using Code;
namespace Visualization
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
Program p = new Program();
string s1 = p.getPrivateText();
string s2 = p.PublicText;
p.Start();
}
}
}
Hope this helps!
How do i rename an imported class so i can access it.Below is the code of the Class with a different name space which was imported and the Class of the one i am working on respectively.Should i just change the name space?
namespace FaultTreeSelectionAsistant
{
public partial class Astra : Form
{
public Astra()
{
InitializeComponent();
}
}
}
namespace THE_HELP
{
public partial class MainPanel : Form
{
public MainPanel()
{
InitializeComponent();
}
private void MainPanel_Load(object sender, EventArgs e)
{
}
}
}
You can give an alias to a namespace or a class like this :
using FTSA = FaultTreeSelectionAsistant;
using FtsaAstra = FaultTreeSelectionAsistant.Astra;
Use . (dot) to delimit namespaces in a declaration :
In MainPanel, you can declare:
FaultTreeSelectionAsistant.Astra astra;
To avoid expliciting the namespace name, you can import it with a using declarative:
using FaultTreeSelectionAsistant;
Please consult MSDN for a complet description of what are namespaces and how to use them.
Simplest way is to use intellisense:
Just write the class name for example Astra, mouse hover and see the option:
I Have created two .cs files with namespaces ,classes and methods . I want to call the classes of one .cs file into another .cs file. Can u help me how to declare namespace and use the namespace so that i can call the classes of the preceding .cs file.
Please forgive if my explanation is not correct.
Suppose i have the following code.
ClassFile1
using system
namespace namespace1
{
class c1
{
Methods()
}
}
ClassFile2
using system
//here i need to declare the namespace1 .Can u help me how to declare namespace1 in this ClassFile2//
namespace namespace2
{
class c2
{
Methods()
}
}
You can reference the fully-qualified name of the class:
namespace SecondNamespace
{
public class SecondClass
{
private FirstNamespace.FirstClass someObject;
}
}
Or you can add a using directive to the file (note, this is at the file level, not the class level) to include a specific namespace when resolving type names:
using FirstNamespace;
namespace SecondNamespace
{
public class SecondClass
{
private FirstClass someObject;
}
}
Taken from here:
namespace SampleNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
// Create a nested namespace, and define another class.
namespace NestedNamespace
{
class SampleClass
{
public void SampleMethod()
{
System.Console.WriteLine(
"SampleMethod inside NestedNamespace");
}
}
}
class Program
{
static void Main(string[] args)
{
// Displays "SampleMethod inside SampleNamespace."
SampleClass outer = new SampleClass();
outer.SampleMethod();
// Displays "SampleMethod inside SampleNamespace."
SampleNamespace.SampleClass outer2 = new SampleNamespace.SampleClass();
outer2.SampleMethod();
// Displays "SampleMethod inside NestedNamespace."
NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
inner.SampleMethod();
}
}
}
Note also that sometimes in addition to the "using" entry (I'm not quite clear on how you app is structured, if it's all one project this is probably moot) you may also need to add the reference. Also not sure what environment you're using. From VSExpress while in the project/file that's the recipient click on Project - Add Reference, select solution and then select your namespace.
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'm trying to write my first program in C# without the use of a tutorial. To ensure that I adopt from the start good coding practices, I want to create each class in an different .cs file. However, I'm running into some troubles when trying to access the elements of the program in such an .cs file.
For example, I have an Form1.cs with an Label and a Start button. When clicking on the start button, a text should appear in the Label. So:
In Form1.cs I have:
namespace TestProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void startButton_Click(object sender, EventArgs e)
{
WriteToLabel message = new WriteToLabel();
message.WelcomeMessage();
}
}
}
And in my separate WriteToLabel.cs file:
namespace TestProgram
{
public class WriteToLabel
{
public void WelcomeMessage()
{
Form1 myForm = new Form1();
//myForm.. --> myForm doesn't have an 'outputLabel'?
outputLabel.Text = "Welcome!"; // This returns an error: 'The name outputLabel does not exits in the current context'.
}
}
}
'outputLabel' is the (Name) I've given the label, and this is in accordance to the name in Form1.Designer.cs.
Both files are using the same components such as 'using System';.
However, from my WriteToLabel.cs file I can't seem to access the Form which holds my program. I did manage to succeed to create different .cs files in an Console Application, which only added to my confusion. So, I have two questions:
First, how can I access the Form from a separate class (i.e. not an partial class) in a separate file?
Second, is this the good way to do it, or is it inefficient to create multiple instances of different classes?
Any thoughts or ideas are highly welcome,
Regards,
The designer automatically creates controls as private fields, because of that your WriteToLabel class can't access it. You need to change that.
Also a good start would be to change the class to something like that:
namespace TestProgram
{
public class WriteToLabel
{
Form1 form;
public WriteToLabel(Form1 form)
{
this.form = form;
}
public void WelcomeMessage()
{
//Form1 myForm = new Form1();
//myForm.. --> myForm doesn't have an 'outputLabel'?
form.outputLabel.Text = "Welcome!";
}
}
}
You're actually instantiating a new instance of Form1, whereas you need to pass in a reference to your existing instance:
public void WelcomeMessage(Form1 form)
{
form.outputLabel.Text = "Welcome";
}
You also need to ensure that outputLabel is a public (or internal) property/field of Form1 so you can set the value accordingly. Then the calling code is slightly different:
private void startButton_Click(object sender, EventArgs e)
{
WriteToLabel message = new WriteToLabel();
message.WelcomeMessage(this);
}
You need to make sure that Form1.outputLabel has public or internal visibility.
You only need something like a LabelWriter class if the class is going to share a significant amount of state or private methods. If all you have is a bunch of methods that set properties on separate objects, you might as well just keep it as a method on the same object (in this case the Form1 object):
void startButton_Click(object sender, EventArgs e)
{
displayWelcomeMessage();
}
void displayWelcomeMessage()
{
this.outputLabel = "Welcome!";
}