Visual Studio 2017 intellisense includes full namespace - c#

Assuming I have two classes called Job and Person with a property called Jobs that is a List<Job>. Inside the constructor when I create a new List<Job> using intellisense it automatically adds the full namespace this.Jobs = new List<Demo.Namespace.Job>(). I just want it to be like this: this.Jobs = new List<Job>(). Any ideas how to solve this?
public class Job
{
}
public class Person
{
public Person()
{
this.Jobs = new List<Demo.Namespace.Job>() // this should be ... new List<Job>();
}
public List<Job> Jobs { get; set; }
}

2 ways of doing that:
Add Manuallu an using
using Demo.Namespace;
on top of the class
Use CTRL+. autocompletion
First put the cursor on the object Job and press CTRL+., then pur an using statement with the first menu options (it's the same as point 1).
That will avoid you to have to specify a full namespace every time.
IF YOU ALREADY KNOW THIS, then you have namespace ambiguity.
Try to just put List as you want to do and check what it says in the error window, it will tell you whatever other class is creating ambiguity, then you will have to adapt the namespaces to be different

Related

Assigning Dynamic Variables from an Input Model C#

I am having some issues understanding how I can assign dynamic values from another class into other variables - I have tried using the correct namespaces, correct syntax and reading up on the documentation that the error provides - however no luck even when trying to implement examples shown. I have very little knowledge in regards to C# as I am mainly doing front end, however have to step up and start picking up some Back end oriented things at the company I work at
The current code I have is as follows:
BrazeConnectionInputs.cs
namespace Workflow.Connector.Braze.Models
{
public class BrazeConnectionInputs
{
public string Username { get; set; }
public string Password { get; set; }
}
}
CreateCampaign.cs
public class CreateCampaignRunner
{
private const string Username = BrazeConnectionInputs.Username; // BrazeConnectionInputs.Username errors
private const string Password = BrazeConnectionInputs.Password; // BrazeConnectionInputs.Username errors
}
You need to learn about objects vs classes. You should have an instance of the source class (BrazeConnectionInputs) that might be called something like model.
You can then explicitly assign across by creating a new instance of CreateCampaignRunner like var runner = new CreateCampaignRunner() and then assign the values in a number of ways:
Explicitly like runner.UserName = model.UserName
By using an explicit constructor var runner = new CreateCampaignRunner(model)
Object initializer syntax
Other ways are available
Highly recommend you do a basic C# course

Unable to run xaml and c# example [duplicate]

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.
Could someone please tell me what this error is and how to fix it?
namespace TimeTest
{
class TimeTest
{
static void Main(string[] args)
{
Time2 t1 = new Time2();
}
}
}
I suspect you've got the same problem at least twice.
Here:
namespace TimeTest
{
class TimeTest
{
}
... you're declaring a type with the same name as the namespace it's in. Don't do that.
Now you apparently have the same problem with Time2. I suspect if you add:
using Time2;
to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)
(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)
namespace TestApplication // Remove .Controller
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Remove the controller word from namepsace
The class TimeTest is conflicting with namespace TimeTest.
If you can't change the namespace and the class name:
Create an alias for the class type.
using TimeTest_t = TimeTest.TimeTest;
TimeTest_t s = new TimeTest_t();
All the answers indicate the cause, but sometimes the bigger problem is identifying all the places that define an improper namespace. With tools like Resharper that automatically adjust the namespace using the folder structure, it is rather easy to encounter this issue.
You can get all the lines that create the issue by searching in project / solution using the following regex:
namespace .+\.TheNameUsedAsBothNamespaceAndType
If you're working on a big app and can't change any names, you can type a . to select the type you want from the namespace:
namespace Company.Core.Context{
public partial class Context : Database Context {
...
}
}
...
using Company.Core.Context;
someFunction(){
var c = new Context.Context();
}
I had this problem as I created a class "Response.cs" inside a folder named "Response". So VS was catching the new Response () as Folder/namespace.
So I changed the class name to StatusResponse.cs and called new StatusResponse().This solved the issue.
If you are here for EF Core related issues, here's the tip:
Name your Migration's subfolder differently than the Database Context's name.
This will solve it for you.
My error was something like this:
ModelSnapshot.cs error CS0118: Context is a namespace but is used like a type
Please check that your class and namespace name is the same...
It happens when the namespace and class name are the same.
do one thing write the full name of the namespace when you want to use the namespace.
using Student.Models.Db;
namespace Student.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Student> student = null;
return View();
}
}
if the error is
Line 26:
Line 27: #foreach (Customers customer in Model)
Line 28: {
Line 29:
give the full name space
like
#foreach (Start.Models.customer customer in Model)

Getting error in MVC Model regarding use of List(T)

I am learning to build the application using one of the online tutorials regarding MVC. It requires to create a user db.
I am getting the following error while building the application. I have just copy-pasted the code from the tutorial. I googled few things, but I am not getting it. Please help to resolve and explain.
using System;
using System.Collections.Generic;
using System.Collections;
using System.EnterpriseServices;
namespace AdvancedMVCApplication.Models
{
public class Users
{
public List UserList = new List();
//action to get user details
public UserModels GetUser(int id)
{
UserModels usrMdl = null;
foreach (UserModels um in UserList)
if (um.Id == id)
usrMdl = um;
return usrMdl;
}
//action to create new user
public void CreateUser(UserModels userModel)
{
UserList.Add(userModel);
}
//action to udpate existing user
public void UpdateUser(UserModels userModel)
{
foreach (UserModels usrlst in UserList)
{
if (usrlst.Id == userModel.Id)
{
usrlst.Address = userModel.Address;
usrlst.DOB = userModel.DOB;
usrlst.Email = userModel.Email;
usrlst.FirstName = userModel.FirstName;
usrlst.LastName = userModel.LastName;
usrlst.Salary = userModel.Salary;
break;
}
}
}
//action to delete exising user
public void DeleteUser(UserModels userModel)
{
foreach (UserModels usrlst in UserList)
{
if (usrlst.Id == userModel.Id)
{
UserList.Remove(usrlst);
break;
}
}
}
}
}
Error: CS0305: Using the generic type 'List' requires 1 type arguments\Models\Users.cs Line:11
You can view the example here: https://www.tutorialspoint.com/mvc_framework/mvc_framework_advanced_example.htm
I was going to say "maybe the code blocks on tutorialspoint hide the necessary <xxx> after the List because it gets interpreted as an HTML tag".. but then I saw the next code block had actual html tags in just fine
To expand on the point Klaus made, it is possible to write classes in C# that are completed by the compiler rather than you. You specify some placeholder for the type of object the class deals with and then the compiler can use it to create an actual class in the background for you
class TenThings<T>{
private T[] _things = new T[10];
private T GetFirst(){
return _things[0];
}
}
T isn't any type in your program, or in the framework, for the purposes of this class/as written here but if you then say somewhere else:
var tt = new TenThings<string>();
Then the compiler can know "anywhere T is mentioned, in this case it needs to be a string" so it can knock together a class for you that is an array of ten strings and has a GetFirst method that returns a string. On the very next line you can have a TenThings<int> and you'll get another different type of class out that deals with ints. You created a template for the compiler to use to write code for you, and the benefit you get is that your GetFirst really does return a string in one case and an int in another. You could have just made a class like this:
class TenThings{
private object[] _things = new object[10];
private object GetFirst(){
return _things[0];
}
}
But then you have to cast everything that comes out - old classes like ArrayList worked this way, and it wasn't a great experience
List is a generic class like this new "templates" way; you really need to have another type of class in angle brackets after its name, such as List<UserModel> and it becomes a part of the type at the same time as dictating to the compiler how to create the template. Per the comment it seems that tutorials point forgot to put the relevant <UserModels> after the List
There are a few other things I take exception to in that tutorial, but talking specifically about this property; creating the List as a public field for one, calling the class UserModels when it seems to represent a single item (unwarranted plural / collections of items are typically recommended to have a name that ends with "Collection" - plurals are used for properties that are collections), I.e. it should be public List<UserModel> UserModels { get; set; } = new List<UserModel>();. I'll leave picking on it for not being a read only collection typed as something generic like IEnumerable<T> for another time :)

c# auto generated partial class redefine property get set

I'm moving from mainly classic asp to .NET. So this may be a stupid question, but I can't find an answer.
I have an MVC App using Database First and Entity Framework. Now I would like to add some logic to the auto generated 'partial' classes. From what I have read it should be a matter of creating a new partial class with the same namespace and name. But when I do that I get an error "(This member is defined more than once)" and "Ambiguity between [partial class] and [partial class]". I understand what the error is saying, but I'm not sure how to resolve the problem.
I would like to add some logic to the set; accessor.
So in the generated class I have
public partial class QualityChecks
{
.....
public int DailyCount { get; set; }
...
}
in my new partial class I would like to add to the set code to make sure only values greater then 0 are added. If a negative value is added it needs to be logged and changed to 0
e.g. my new partial class is:
public partial class QualityChecks {
public int DailyCount {
set
{
DailyCount = value;
if it's < 0 log and set to 0
}
}
If that's not clear maybe this will help:
Currently I have loads of code that simply does
QualityChecks qc = new QualityChecks();
qc.DailyCount = enteredAmount;
....
db.QualityChecks.add(qc);
Rather then update that logic everywhere it would be nice to have it wrapped up in the QualityChecks class.
Is this the right way of going about it? If so what do I need to change to make this work?
Thank you in advance for any tips and help!
You cannot define the same members in two different files.
You can try to define a new wrapper property (eg. MyDailyCount) that add that extra logic and update the underlying DailyCount at the end so it get persisted to database.
public int MyDailyCount
{
get { return DailyCount; }
set
{
DailyCount = value;
// your extra logic
}
}

'namespace' but is used like a 'type'

My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.
Could someone please tell me what this error is and how to fix it?
namespace TimeTest
{
class TimeTest
{
static void Main(string[] args)
{
Time2 t1 = new Time2();
}
}
}
I suspect you've got the same problem at least twice.
Here:
namespace TimeTest
{
class TimeTest
{
}
... you're declaring a type with the same name as the namespace it's in. Don't do that.
Now you apparently have the same problem with Time2. I suspect if you add:
using Time2;
to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)
(Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)
namespace TestApplication // Remove .Controller
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Remove the controller word from namepsace
The class TimeTest is conflicting with namespace TimeTest.
If you can't change the namespace and the class name:
Create an alias for the class type.
using TimeTest_t = TimeTest.TimeTest;
TimeTest_t s = new TimeTest_t();
All the answers indicate the cause, but sometimes the bigger problem is identifying all the places that define an improper namespace. With tools like Resharper that automatically adjust the namespace using the folder structure, it is rather easy to encounter this issue.
You can get all the lines that create the issue by searching in project / solution using the following regex:
namespace .+\.TheNameUsedAsBothNamespaceAndType
If you're working on a big app and can't change any names, you can type a . to select the type you want from the namespace:
namespace Company.Core.Context{
public partial class Context : Database Context {
...
}
}
...
using Company.Core.Context;
someFunction(){
var c = new Context.Context();
}
I had this problem as I created a class "Response.cs" inside a folder named "Response". So VS was catching the new Response () as Folder/namespace.
So I changed the class name to StatusResponse.cs and called new StatusResponse().This solved the issue.
If you are here for EF Core related issues, here's the tip:
Name your Migration's subfolder differently than the Database Context's name.
This will solve it for you.
My error was something like this:
ModelSnapshot.cs error CS0118: Context is a namespace but is used like a type
Please check that your class and namespace name is the same...
It happens when the namespace and class name are the same.
do one thing write the full name of the namespace when you want to use the namespace.
using Student.Models.Db;
namespace Student.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<Student> student = null;
return View();
}
}
if the error is
Line 26:
Line 27: #foreach (Customers customer in Model)
Line 28: {
Line 29:
give the full name space
like
#foreach (Start.Models.customer customer in Model)

Categories

Resources