I have created a Area class using Linq-to-SQL.
Now I want to create a partial class of the same name so I can implement validation.
Error 1 Cannot implicitly convert type
'System.Data.Linq.Table<SeguimientoDocente.Area>'
to
'System.Linq.IQueryable<SeguimientoDocente.Models.Area>' C:\Users\Sergio\documents\visual
studio
2010\Projects\SeguimientoDocente\SeguimientoDocente\Models\AreaRepository.cs 14 20 SeguimientoDocente
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SeguimientoDocente.Models
{
public class AreaRepository
{
private SeguimientoDataContext db = new SeguimientoDataContext();
public IQueryable<Area> FindAllAreas()
{
return db.Areas;
}
public Area GetArea(int id)
{
return db.Areas.SingleOrDefault(a => a.ID == id);
}
public void Add(Area area)
{
db.Areas.InsertOnSubmit(area);
}
public void Delete(Area area)
{
db.Areas.DeleteOnSubmit(area);
}
public void Save()
{
db.SubmitChanges();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SeguimientoDocente.Models
{
public partial class Area
{
}
}
Here's a screenshot.
This is almost certainly because your partial class is not in the right namespace. Go into the .designer.cs file of the LINQ model, look for the generated Area class, and make sure the namespace you wrapped your partial class in matches.
EDIT
I just fixed the formatting in your question. The error message does in fact indicate that your partial class is in the wrong namespace.
Error 1 Cannot implicitly convert type
'System.Data.Linq.Table<SeguimientoDocente.Area>'
to
'System.Linq.IQueryable<SeguimientoDocente.Models.Area>'
As you can see from the error above, you need to change the namespace your partial class is in to be SeguimientoDocente, not SeguimientoDocente.Models. As it stands now, they are two completely different incompatible types that happen to have the same simple name.
The error message tells you that the problem is in line 14 of the AreaRepository.cs file. Specifically, you are trying to return db.Areas from a method whose return type is IQueryable<Area>, though db.Areas is in fact of type System.Data.Linq.Table.
Related
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DevExpress.AspNetCore.RichEdit;
using DevExpress.AspNetCore;
using DevExpress.Web.Mvc.UI;
using DevExpress.Blazor.RichEdit;
using System.Runtime;
namespace TextEditor.Pages
{
public static RichEditBuilder RichEdit(this BuilderFactory factory,
string name
);
}
I get these errors:
Error CS1106
Extension method must be defined in a non-generic static class
Error CS0116
A namespace cannot directly contain members such as fields or methods
Error CS0501
'.RichEdit(BuilderFactory, string)' must declare a body because it is not marked abstract, extern, or partial TextEditor
You need to have a public static class RichEditUtils which then contains this method of yours - you cannot have that method directly in the namespace level...
namespace TextEditor.Pages
{
public static class RichEditUtils
{
public static RichEditBuilder RichEdit(this BuilderFactory factory, string name)
{
// do something here that ends up returning a "RichEditBuilder" ...
}
}
}
Long-story-short, if you look at my project you can see that I have an automatically generated model, but after importing the namespace of the mode and trying to use it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LRVault.Models;
using System.Data.Linq;
namespace LRVault.Repositories
{
public class ThreadRepository : VaultCRUD
{
public void AddOrUpdateThreads(IEnumerable<Thread> threads)
{
VaultContext.???
}
}
}
I can't get the tables, i.e. VaultContext.Posts is unrecognized. What am I missing?
EDIT: parent class is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using LRVault.Models;
using System.Data.Entity;
namespace LRVault.Repositories
{
/// <summary>
/// Serves as base class for the CRUD operations
/// </summary>
public abstract class VaultCRUD : IDisposable
{
public DbContext VaultContext { get; private set; }
public VaultCRUD()
{
VaultContext = new LRC_VAULTEntities();
}
public void Dispose()
{
VaultContext.Dispose();
}
}
}
You need to cast VaultContext property to LRC_VAULTEntities, to access the tables:
(VaultContext as LRC_VAULTEntities).
DbContext is the base class of LRC_VAULTEntities, but the 'Tables' are in LRC_VAULTEntities.
You need to create an instance,
using(var context = new VaultContext())
{
}
The VaultContext instance is declared with type DbContext which has nothing to do with your table, you have to change its type to be LRC_VAULTEntities, do this and you will get your tables as you want in the inherited classes
I'm attempting to extend my custom classes and running into a problem where it cannot find the extension method.. I have and can extend any built in classes or even ones contained within DLL's. I don't know if this is a compilation error or if I'm doing something wrong. Threw together a small program for an example, won't compile..
Here's the extension:
namespace ExtensionMethodTesting.Extension
{
public static class Extension
{
public static void DoSomething(this ExtensionMethodTesting.Blah.CustomClass r)
{
}
}
}
Here's the Custom Class:
namespace ExtensionMethodTesting.Blah
{
public class CustomClass
{
public static void DoNothing()
{
}
}
}
Here's the code calling it:
using ExtensionMethodTesting.Blah;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtensionMethodTesting.Extension;
namespace ExtensionMethodTesting
{
class Program
{
static void Main(string[] args)
{
CustomClass.DoNothing();
CustomClass.DoSomething();
}
}
}
I must be missing something... Anyways the exact error just for clarification is:
Error 1 'ExtensionMethodTesting.Blah.CustomClass' does not contain a definition for 'DoSomething' c:\users\damon\documents\visual studio 2013\Projects\ExtensionMethodTesting\ExtensionMethodTesting\Program.cs 16 25 ExtensionMethodTesting
Extension methods require an instance of an object. You'll have to new up a CustomClass to use it.
var custom = new CustomClass();
custom.DoSomething();
See this answer as to why that is.
You need to instantiate an object of the CustomClass to use its extension method.
CustomClass obj = new CustomClass();
obj.DoSomething();
I have this class file call SMSHelper.cs First I just wanted to know is my written structure is Correct or Wrong?(My class file name is also SMSHelper.cs & my first class also SMSHelper here you can see in the code.).
Basically I have 3 classes in same file. One class has the same name as the file name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
namespace SMSBase.SMSFunction
{
public class SMSHelper : DotNetNuke.Entities.Modules.PortalModuleBase
{
// Some Code here
// Return Something here
}
public class Validator
{
public bool IsValidate(string Item)
{
// Some Code Here Not return anything
}
public class HuntingDate
{
//Implementation & Constructor here.. Return Something
}
}
}
There is nothing wrong in your class structure (except one missing bracket). And there is no matter your class name and file name are same. You can access and initialize your class objects like that...
SMSBase.SMSFunction.SMSHelper objSMSHelper = new SMSBase.SMSFunction.SMSHelper();
SMSBase.SMSFunction.Validator objValidator = new SMSBase.SMSFunction.Validator();
SMSBase.SMSFunction.HuntingDate objHuntingDate = new SMSBase.SMSFunction.HuntingDate();
This SMSBase.SMSFunction is your namespace... you can access classes by your namespace or include this namespace in the class header like
using SMSBase.SMSFunction
There is a problem in opening closing brackets:
namespace SMSBase.SMSFunction
{
public class SMSHelper : DotNetNuke.Entities.Modules.PortalModuleBase
{ // Some Code here // Return Something here
}
public class Validator
{
public bool IsValidate(string Item)
{ // Some Code Here Not return anything
}
}
public class HuntingDate
{ //Implementation & Constructor here.. Return Something
}
}
If that is what you are asking.
Yes as Talha ,said one bracket is missing.Try to put that.
When we want to call the class name its better to call with "namespace.ClassName" format which gives clarity to the compiler.
(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
{
}
}