my Namespace does not allow Directory.GetFiles - c#

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(....);

Related

When importing namespace compiler returns error message type or namespace name could not be found

When I'm trying to import a namespace from another file, the compiler gives me the error message, "type or namespace name could not be found". Take a look at my code below. Both files are in the same directory, so I don't understand what's wrong.
Setup.cs (the main file)
using System;
using ScreenTextLine; // Error is here.
namespace Setup
{
namespace Screen
{
class Text
{
static void Main(string[] args) { }
}
}
}
Problem.cs (the file with the namespace I'm trying to import)
using System;
namespace Setup
{
namespace ScreenTextLine
{
class WelcomeText
{
static void Main(string[] args){}
}
}
}
You need to use their fully-qualified name when referencing them by separating each namespace by a dot using the following format...
[Top_level_namespace].[nested_namespace]
So, this nested structure...
namespace Setup
{
namespace Screen
{
}
}
It's referenced by a using statement this way...
using Setup.Screen;

I can't access my class property

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

Functions inside a static class need the name of the class to be called

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();

Missing directive of assembly reference

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?

using another c# class from a c# class

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;

Categories