Class structure confirmation - c#

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.

Related

Expected class, delegate, enum, interface, or struct (CS1518)

simple code here and the answers I find don't seem to work.
I'm using
SharpDevelop Version : 3.2.1.6466
.NET Version : 2.0.50727.5485
The problem is the error code
Expected class, delegate, enum, interface, or struct (CS1518).
Any ideas?
Program.cs codes:
using System;
using System.Threading;
namespace Threshold
{
public class Class1
{
public Class1()
{
Heritage YOLO = new Heritage();
YOLO.Fractal();
}
}
static void Main()
{
//do nothing
}
}
The cs file it calls is:
using System;
using System.Threading;
namespace Threshold
{
public class Heritage
{
int Fractal()
{
//Do stuff.
}
}
internal partial class DefineConstants
{
public const string DRIVERPATH = "d:\\tc\\bgi";
}
}
Please help with a fix.
Thanks.
Your main method is outside the class. Put it inside.

Error when trying to create a partial class

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.

Why do extension methods not work with namespace aliasing?

This may be an ignorant question, but I'm unsure why I can not use namespace aliasing and extension methods together.
The following example works just fine:
Program.cs
using System;
using ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new Domain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
However, when I alias domain as follows in Program.cs:
using Domain = ExtensionMethodTest.Domain;
The extension method no longer works..
This can be rather frustrating when I'm dealing with converting various domain objects to contract objects (let's say I have 4 domain assemblies and 4 contract assemblies) for use in a web service. Using aliasing would be very handy as I could alias as follows and continue to use the various extension methods (such as ToContract, etc.):
using BillingContracts = Namespace.Billing.Contracts;
using IssuingContracts = Namespace.Issuing.Contracts;
etc...
I look forward to the answer.. I'm sure it's straight forward, but I, for the life of me, can't figure out why it doesn't work.
Thanks!
Make sure to still add a non-aliased using statement:
Program.cs
using System;
using ExtensionMethodTest.Domain; //DON'T FORGET A NON-ALIASED USING
using MyDomain = ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new MyDomain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
I also love to use namespace aliasing but its not working in case of Extension methods. So one thing that i did is, I changed the namespace of extension class to same namespace that my main project has (although my extension class resides in sub folder of main project).
Suppose I have a project myFirstProj which surely has namespace myFirstProj for root classes. My extension class is present in myFirstProj/Common/myExtensionClass with contains namespace myFirstProj.Common { //myExtensionClass }.
So now what I did is, I changed the namespace of myExtensionClass from namespace myFirstProj.Common{ //myExtensionClass } to namespace myFirstProj{ //myExtensionClass } .
Now i can use my extension methods in my whole project myFirstProj event without specifying using statement for my extension class.
I know this isn't a standard way to that but I haven't found any other workaround for it expect this one because for my Project there is a requirement to go with namespace aliasing for project namespaces.

Can I identify the sub class from a base class parameter?

Take a look at the following. The question near the end of the code - in the "whoAmI" function...
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 MY_TEST_PROJECT
{
// a form class
public partial class frmTestForm1 : Form
{
// zillion lines of code
private void aFunction()
{
ClassTest.whoAmI(this);
}
// zillion lines of code
}
// another form class...
public partial class frmTestForm2 : Form
{
// zillion lines of code
private void aFunction()
{
ClassTest.whoAmI(this);
}
// zillion lines of code
}
// a home made test class
public static class ClassTest
{
// zillion lines of code
public static void whoAmI(Form theForm)
{
// IS THERE A WAY TO SEE WHAT KIND OF FORM theForm IS?
// LIKE:
// if (theForm IS A frmTestForm1)
// doThis();
// else if (theForm IS A frmTestForm2)
// doThat();
}
// zillion lines of code
}
}
You can check with the keyword is.
Also, you might want to solve your problem using polymorphism instead of checking the type.
There are few ways you can do this:
as you have guessed you can use "is" key word as Sjoerd has proposed
if (theForm is frmTestForm1)
doThis();
//So on
Another approach is to use reflection to get the exact type of the form you have. Your code should look like somthing like this:
if (theForm.GetType().UnderlyingSystemType
== typeof(frmTestForm1))
doThis();
The drawback of the first approach is that if, for example, your frmTestForm2 is derivative of frmTestForm1 and you use code like this if(yourform is frmTestForm1) and your form is pointing to a frmTestForm2 instance "is" keyword will return true.
Did you try this and it is not working? There is no problem trying to get a type from an object. Although you are sending it in as the base type the object still is a derived class type.
if (theForm.GetType().ToString() == typeof(frmTestForm1).ToString())
{
// Do your stuff
}

How do I add common C# code to a Visual C# 2008 Express project/solution?

(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
{
}
}

Categories