I am using Visual Studio 2013. I am trying to start unit testing by using this tutorial.
I have added a class library and a reference to MVC. However, the Intellisense/Autocompletion is not working properly within my class library. Currently, this is all the code I have in my test class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Web.Mvc;
using System.Web;
using Application_Portal;
using Application_Portal.Controllers;
namespace ApplicationPortalTests
{
[TestFixture]
public class HomeControllerTest
{
HomeController home = new HomeController();
}
}
The Intellisense does not seem to recognize the home variable (no suggested properties, etc.) Typing home.Index() gives the following error:
ApplicationPortalTests.HomeControllerTest.home' is a 'field' but is used like a 'type'
Furthermore, it does not even seem to recognize "var" (as in var result = ...). Instead, when I type var and hit space, it autocompletes it as EnvironmentVariableTarget.
I have tried cleaning and rebuilding the project, and closing and reopening Visual Studio, but with no success.
What might the issue be? I appreciate any advice.
You have declared your variable inside the class. If you want to use this variable, it must be within the context of a member. For instance:
[Test]
public void test_my_index_page()
{
var result = home.index();
}
Related
I have only been coding for about two years now (off and on) but I'm well acquainted with this cryptic error across multiple versions of VS. I know this issue isn't related to just RestSharp.
This 'caret error' has been the bane of my VS experience so that is why I'm here.
Environment:
Microsoft Visual Studio Community 2019 Version 16.8.1
RestClient v4.0.30319
Windows 10 Professional
What I'm trying to do:
I'm following the very simple directions on the RestClient site. I have installed RestClient, added references but when I try to get started I get that dreaded caret error.
None of this is my code aside from the reference to RestClient, which may or may not be appropriate (I don't know).
Screen Snip
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;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;
namespace newDawn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var client = new RestClient("https://api.twitter.com/1.1");
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);
var response = client.Get(request);
}
}
}
Thanks for reading.
using RestClient;
Here you are declaring that you are using the namespace RestClient, not the object.
var client = new RestClient(...
The error is raised here because you are trying to create an instance of the namespace you just declared you were using.
It's uncommon and confusing for an object to share a name with part of its namespace, so you should generally not expect to see a using statement with the same name you use to create an object.
Namespaces are used to separate, organize classes and limit what classes are loaded at compile time.
Classes of the same name can exist in different namespaces.
You could create your own class named RestClient and use it in this project as well(maybe for testing) as long as they existed in different namespaces. The full declaration of the class you are using now is RestSharp.RestClient. You could create a class with the full declaration Facundo.Test.RestClient and use them both in the same project. However, in this case, you could not use using statements and everytime you wanted to instantiate one or the other you would need to fully declare the namespace
RestSharp.RestClient client = new RestSharp.RestClient(...);
or
Facundo.Test.RestClient testClient = new Facundo.Test.RestClient(...);
I would not recommend naming them the same in practice, just trying to illustrate namespaces.
In my web form project I opened a folder called App_Code inside that folder I have class named Test123, but when I try to create instance inside WebForm1.aspx.cs it doesn't recognize that type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace StephanProject_2.App_Code
{
public class Test123
{
}
}
My project structure:
I tried to add using StephanProject_2.App_Code;
What's the problem?
The class you are calling does not seem to be in the same namespace as the Webform1 class
if its from another namespace use.
StephanProject_2.App_Code.Test123 t = new StephanProject_2.App_Code.Test123()
Or alternatively in the top say
using StephanProject_2.App_Code;
When i try to same as your project then it works my code
You can see Here
I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).
I'm trying to use the Process class but it gives me this compiler error every time I do
The name 'Process' does not exist in the current context
I've already added using System.Diagnostics; at the top of my code but I still get this error. Autocomplete wont even recognize the class.
Hear is the code. I've only included the Main method because the rest of the code has nothing to do with the error.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
public class MainClass
{
static void Main()
{
AIRuntime adam = new AIRuntime("Adam", false);
adam.AIUpdate();
Process.Start(adam.directory); //adam.directory is just a string
}
}
My IDE is Xamarin Studio if it matters.
After web searching and attempts, I found the problem.
To fix this, I manually add a reference to System in my project options instead of just typing using System;. I also need to keep using System.Diagnostics; at the top. It's all in the picture below
Thanks guys for trying to help
I have a c# problem. I have created a project called 'classproject' and I have added several forms to project successfully and successfully ran the program, but there is this other windows form that I just added and when I double-clicked on the form, it took me to the code view and brought out this:
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 demo : Form
{
public demo()
{
InitializeComponent();
}
private void demo_Load(object sender, EventArgs e)
{
}
}
}
Now, my challenge is that the name of the project is meant to show by default after the namespace as in "namespace classproject" instead of "namespace WindowsFormsApplication1". Please can anyone help me out, what am I meant to do because have tried everything possible. Even if I change the name myself its still going to flag error... someone please help me as soon as possible.
I'm not sure exactly what your issue is. If you change the namespace manually in the code to classproject it should be fine. What error are you talking about? You would also need to change the designer code file as well (probably called demo.Designer.cs).
Just a side note as well. Standard naming convention for C# is to use Pascal-case (as in ClassProject).
You must change namespace in ALL files that was generated by Visual Studio. You can do it manually, or just set cursor on "WindowsFormsApplication1", press F2 and enter new name.
Optionaly you can also change namespace in project properties on Application tab - then all new files added to project will also have namespace that you set there.