i've a gallery.aspx with code behind (this is not a project, only have two files), i want to use another c# class called CFileInfo at same directory.
i use using CFileInfo; in gallery.cs but it is not work.
CFileInfo oDetailedFileInfo = new CFileInfo(sFileName);
The above code is also not work. The CFileInfo looks like
public class CFileInfo
{
...
}
There is no namespace created.
How can i use CFileInfo.cs in gallery.cs?
Create your class and with name space
namespace FileObjects
{
public class CFileInfo
{
public CFileInfo() { }
//Add your functions here
}
}
Now you call this in gallyer.aspx
**using FileOBjects;**
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gallery : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CFileInfo oDetailedFileInfo = new CFileInfo(sFileName);
}
}
You need to put your CFileInfo.cs into App_Code directory. I'm pretty sure that VS compiler issues warning about this.
Try the following, C# has a global (or unnamed) namespace - you can use global:: to access your class::
using myCFileInfo = global::CFileInfo;
Related
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
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();
I added an existing form (and its references) to another project and I am trying to show the new form. There are no coding issues, just a reference error:
The type or namespace 'frmEmail' could not be found (are you missing a
using directive or an assembly reference?)
I cannot figure out what "using" or reference I failed to use when importing the other form. Any ideas?
Here is the code causing the error:
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 Notify_Setup
{
public partial class frmNotifications : Form
{
public frmNotifications()
{
InitializeComponent();
pbBlue.MouseEnter += new EventHandler(pbBlue_MouseEnter);
}
private void pbGreen_Click(object sender, EventArgs e)
{
frmEmail frmEmail = new frmEmail();
frmEmail.Show();
this.Hide();
}
}
}
You need to pull the namespace in. For example, if this was how your form currently was in the other project:
namespace Your.Form.Namespace { // this is important
public class YourForm : Form {
// stuff
}
}
Then in the project you're adding it to.. you need to add your assembly as a reference, then import the namespace in like this:
using Your.Form.Namespace; // import the namespace
namespace Other.Project {
public class OtherClass {
YourForm _form; // this is fine now
}
}
The other options is to fully qualify the type. What this means, is to use the entire namespace and type name in the declaration. It would be like this:
namespace Other.Project {
public class OtherClass {
Your.Form.Namespace.YourForm _form; // this is fine too
}
}
I added an existing form (and it's references) to another project and I am trying to show the new form.
It looks like the form you added was frmNotifications, but is creating an instance of frmEmail. Did you also added it?
i have the following code:
using System;
using System.IO;
namespace Project
{
public partial class Documentation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
}
}
}
for some reason, i get the error "Project.Directory does not contain a definition for 'GetFiles'". If i change the namespace to anything else, the error goes away. Is there a way to find out what exactly I am doing in my current namespace that disallows the usage of the directory function?
You have defined your own class named Directory. Instead, try specifying
string[] filePaths = System.IO.Directory.GetFiles(...);
Alternatively, you can specify a class alias for System.IO.Directory
using System;
using System.IO;
using SystemDirectory = System.IO.Directory;
...
SystemDirectory.GetFiles(....);
(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
{
}
}