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)
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.
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
Related
I am trying to access to a public static variable in a class but I don't know how to do it. I try different solutions like using and namespace but doesn't work.
In the form called "CategoriasCaracteristicas.cs" (blue), I am trying to access to empresaGlobal.cs (red) as you can see in the next image:
But I can't do it using namespace, or others. The code of the file "CategoriasCaracteristicas.cs" that is where I am trying to access the other class is the following:
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;
using System.Data.SqlClient;
using System.Configuration;
using Utils;
namespace imlnet
{
public partial class CategoriasCaracteristicas : Form
{
private string codEmp;
private string cadenaConexion;
public CategoriasCaracteristicas()
{
And this is the code of empresaGlobal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImeApps
{
public static class empresaGlobal
{
public static string empresaID = "3";
public static String EmpresaID
{
get { return empresaID; }
set { empresaID = value; }
}
}
}
Thanks in advance!
Put your empresaGlobal.cs file in a project and add that project as a reference to the GestorCategoriasCaracteristicasIngenieria project.
Make sure any classes, properties and methods you need to access are available using the public keyword.
I have a class named MyFillerClass in the file MyFillerClass.cs like so :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace trial
{
public static class MyFillerClass
{
public static List<string> returnCategoryNames()
{
List<string> catNames = new List<string>();
catNames.Add("one");
catNames.Add("two");
catNames.Add("three");
catNames.Add("Others");
return catNames;
}
}
}
now when i want to call it from somewhere else (like a form class) :
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 trial
{
public partial class Form1 : Form
{
static string lastSelectedCategory;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = returnCategoryNames(); //error : The name 'returnCategoryNames' does not exist in the current context
lastSelectedCategory = listBox1.SelectedValue.ToString();
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
lastSelectedCategory = listBox1.SelectedValue.ToString();
System.Diagnostics.Debug.Print("### User choosed " + lastSelectedCategory + " category");
}
}
}
the line "listBox1.DataSource = returnCategoryNames();" produce an error as indicated in the code ,to fix it i have to adjust it to "listBox1.DataSource = MyFillerClass.returnCategoryNames();".
the question is : in a long program that can add a lot of typing ,can i adjust the class MyFillerClass in such a way that i can just call the function like so : returnCategoryNames() ?
No, not in C# up to 5.0. You need to prefix the static method name with the class name.
However, in C# 6.0 there will be static using statements available. This new language feature will allow you to access directly static classes methods.
Yo can't do it in C# yet. To do it, you need to do a none static class and none static method.
You can do an extension method.
To call a function from a class you need to have an object created for that class then only you can call the method defined in the class.
In case of static class no need to create any object. you have to direct call the method followed by the class name.
In your case
MyFillerClass.returnCategoryNames();
How to use MessageBox in class library?
Here is my code
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 MessageBoxes
{
class ShowInfo
{
MessageBox.Show("test");
}
}
i can load MessageBox but can't have show property, MessageBox.Show("test"); <-- fail
You should NOT use a Windows forms MessageBox inside a class library. What if you use this library in an ASP.NET application. The MessageBox will be shown in Webserver. And your webserver will be waiting (hung) untill someone responds to that MessageBox in webserver.
An ideal design would be that you either return the message as string and deal with that string in caller specific way or throw an exception if thats what you want.
If you still want then here is your code corrected
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 MessageBoxes
{
class ShowInfo
{
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
}
}
You have the call to messagebox outside any method.
This code cannot be compiled at all.
You should write
namespace MessageBoxes
{
class ShowInfo
{
public void ShowUserMessage(string messageText)
{
MessageBox.Show(messageText);
}
}
}
and then call it after instancing an object of type ShowInfo
ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");
Additional Answer to this Question:
After the Class Library Project has been created.
Right Click your Project Add > New Item > Windows form
it's done by adding reference System.Windows.Forms.dll
Make sure you actually use the the class in the main form.
class ShowInfo
{
public static void show()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
...
public Form1()
{
InitializeComponent();
ShowInfo.show();
}
I wrote a simple ServicedComponent
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.EnterpriseServices;
namespace ComPlusServer
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("9C674ECA-1B71-42EA-9DB2-9A0EA57EC121")]
[Description("Hello Server")]
public class HelloServer : ServicedComponent
{
[Description("Say Hello!")]
public String SayHello()
{
return "Hello!, ";
}
}
}
and a Windows Forms application
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComPlusServer;
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HelloServer server = new HelloServer();
MessageBox.Show(server.SayHello(), "Message from HelloServer");
}
}
}
on the Component Services MMC, on the application properties, security tab I lowered Authentication Level for Calls to None and Impersonation Level to Identify and Unchecked Enforce access checks for this application on Authorization.
I keep getting a ServicedComponentException exception saying
Method-level role based security requires an interface definition for
class method.
Any idea on this?
I believe that it means that methods of your Component class needs to be defined in an interface.
[ComVisable(true)]
public interface IHelloServer
{
public String SayHello();
}
Now have your componet class implement the interface:
[ComVisable(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComDefaultInterface(typeof(IHelloServer))]
[Guid("9C674ECA-1B71-42EA-9DB2-9A0EA57EC121")]
[Description("Hello Server")]
public class HelloServer : ServicedComponent, IHelloServer
{
[Description("Say Hello!")]
public String SayHello()
{
return "Hello!, ";
}
}
(I still feel like a complete newbie in MS Visual environments... so please bear with!)
I'm using Microsoft Visual C# 2008 Express Edition.
I have a project and in that project are two different forms. The .cs file for each form starts out:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace
{
public partial class MyFormName : Form
{
...
(...and the second is "MyFormName2" but no differences besides that)
I want to write a function that I know both forms are going to need to access. I right-clicked on my project, selected "Add", selected "New Item" then selected "Code File" and named my file "Common.cs" and it gave me a completely blank file that's in my project.
How do I set this up...? I thought I should do the following...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace
{
}
...but then when I try to add a function like:
public void mytestfunc() {
}
within that namespace I get the following error:
"Expected class, delegate, enum, interface, or struct"
How do I set things up so I can have "mytestfunc" be available to both MyFormName and MyFormName2?
Thanks!
-Adeena
UPDATE:
Understand (now) that everything must be in a class, but then I don't understand how to really use it. Does that mean I have to create an object? This common function happens to just be some math...
so now if I have this:
namespace MyNameSpace
{
public class MyCommonClass
{
public void testFunc()
{
MessageBox.Show("Hee hee!");
return;
}
}
}
...how do I call testFunc from my Form? Must I do the following:
MyCommonClass temp = new MyCommonClass;
temp.testFunc();
or is there another way to call testFunc?
If you do something like:
namespace MyNameSpace
{
public class myclass
{
public myMethod()
{
// Code
}
}
}
You will be able to instantiate and access it. If you change it to:
namespace MyNameSpace
{
public class myclass
{
public static myMethod()
{
// Code
}
}
}
You will be able to call myClass.myMethod without instantiating a new myClass.
The short answer is that everything needs to be inside a class; I'd suggest you sit down with a basic tutorial to help you get to grips with the basics...
Code need to be inside classes.
It would look something like this:
using System;
namespace MyNameSpace
{
public class CommonHelper
{
public string FormatMyData(object obj)
{
//do something
return String.Empty;
}
}
}
If the function you call is not related to the forms, make it static
namespace myns
{
public static class myhelper
{
public static void DoSomething()
{
}
}
}
and call the method using myhelper.DoSomething();
If the function you want to call is somehow form-related, e.g. common functionality across multiple forms, derive a class from Form (does not need a visual form) and make it base class of the visual forms:
namespace myns
{
public class MyFormBase : Form
{
protected void DoSomethingWithTheForm()
{
}
}
}
and in your form's .cs:
namespace myns
{
public partial class MyFormName : MyFormBase
{
}
}