.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Extension Method ForEach for ArrayList class

Posted By:      Posted Date: September 05, 2010    Points: 0   Category :ASP.Net
Hi,I need to create an extension method(ForEach) for arraylist classI have created an extension method for Array class but cannot do that for arraylist.Please help me namespace MyExtension { public static class IEnumerableExtension { public static void ForEach<T>(this IEnumerable<T> list, Action<T> action) { if (action == null) throw new ArgumentNullException("action"); foreach (T item in list) { action(item); } } } } Any Help Shall be appreciatedThanks

View Complete Post

More Related Resource Links

Custom markup extension or custom binding class?

Hey experts! I'm doing a project where I've encountered an issue I'd love some feedback on. I've created a custom Markup Extension called MyMarkupExtension, with two properties.  I can use it like below: <cc:DialogBorder Title="{local:MyMarkupExtension MyProperty='property value goes here', AnotherProperty='and another value here'}"> It works, and in 99% of the cases, it fits my needs perfectly.  In 1% of the cases, the values of MyProperty and AnotherProperty can not be set at design time, so I'd need some binding there. <cc:DialogBorder Title="{local:MyMarkupExtension MyProperty={Binding Path='SomePropertyOfTheViewModel'}, AnotherProperty='and another value here'}"> This presents my two questions (yes I did make only one post for 2 questions, bad karma...): 1. the binding inside my markup is not legal, after all, a binding can only be set on a DependencyProperty.  Is there any workaround which I could use for this problem at hand? In case there isnt... 2. I'm thinking about dropping the markup extension and creating a custom binding instead.  For this, I'd have to inherit from bindingbase, and so far I've read online that's one ____ of a nightmare.  Does anyone have some good code / examples on where to start? "The improbable we do, the impossible just takes

Calling a method with a class object as parameter data type.

   [WebMethod]    public string GetInfo(Customer inCust)    {        // Code in here    } I have a web service that resides on a remote server. I want to call a public method of theweb service named  "GetInfo". The method is shown above. The web service method expects a class object oftype "Customer". What I did to create a Customer object was to define in my asp.net project my own class named "Customer" which is exactlythe same as the class that belongs to the web service project on the remote server. I then created an instance of Customerand tried to call the web service passing in an object of type customer : my.Service ws = new my.Service(); // Get web service connectionCustomer aCustomer = new Customer();aCustomer.Initialize();ws.GetInfo(aCustomer) // call web service passing in a Customer object The problem is that the compiler gives me the following error: Value of type '_Default.Customer' cannot be converted to 'ws.Class1'. Here Default is refering to my Default.aspx.cs code. Why is this? Do I need to Cast my "aCustomer" class object to a type ofws.Customer ? How do I do that?

[] brackets before a class or method Declaration

Hi Guys,Some times we have [] brackets before a class or method Declaration.  Can anyone guide me please in understanding those squre brackets.  what do we write in those and what they represent. like this [SerializableAttribute][ObsoleteAttribute]class Moon { }Thanks in advance.

Is it possible to add a class method to CLR base class like Date?

Hi All, I would like to add a class method like ParseYYYYMMDD to the Date base class. So that I can use the following code. Dim result As Date = Date.ParseYYYYMMDD(dateString) Any idea? Is it possible via extension? Thanks! Regards, AlexAlex Lau from HKSAR

how to get a class reference as a reference and not a string to send for a generic method

I got some property that i get using generic method, there for I dont have it's <T>and I want to send it to another generic method but , how the heck do I get it's <T> :P?please look at the code below to understandit gets an entity framework objects, scan all it's properties and if the property is another entity framework object it would send it back to him self , the issue is that i dont know the new object <T> to send it back to him self :(search for the bold part to see what i'm missing//contains objects which we added so there wont be double adding private List<object> _addedObjects; public FakeGenericRepositoryExtension() { _addedObjects = new List<object>(); } public void Create<T>(T entity) where T : class { //add to created objects _addedObjects.Add(entity); //do something with object //... //start recircle process Type entityType = typeof(T); //run thrue each member of Type foreach (var member in typeof(Type).GetProperties()) { //get member value var member_value = member.GetValue(entity, null); //if not null //we must test this otherwise we can't get .BaseType if (member_value != null) { //get current member's base type string memberBaseType = member.GetValue(entity, null).GetType().BaseTyp

C#NET2008 FrmMain using DELEGATE CLASS Method



Hullo Good Guys,


I am back here seeking help. Please Help me.


Here us the description of what I was ihstructed to do by the System Analyst.


Create DELEGATE Class to perform Mortgage computation and return the result back to the calling FrmMortgage.cs. Apparently my coding are not working as I am not familiar with DELEGATE Class


Here are the Erro

Calling a static method in a static class from a base class.


I have the following static class and method:

public static class GenericList


      public static List<DocumentElement> PopulateGenericList()



What is the syntax to call the PopulteGenericList() method from a base class?



add an extra method to an existing class


Suppose that I want to implement another method for a class namely hasFiles for System.IO.Directory , How can I do that?

for example if I add <Extension()> _  tag before each function or Sub I can add a new function or method for each Control.

How should it be for a specific Class?

Unusual problem using a class method


I have a class that runs calculations on numbers and populates the results to my SQL database.  My code behind page instantiates the class object, and in a loop it makes use of the method five times (once per loop).  Essentially, my code behind collects the information from controls and passes the information to the class method for processing.  The method works just fine most of the time.

On one of the iterations I get a error before the code even gets to the class.  The error is, "Arithmetic operation resulted in an overflow."  What strikes me as odd is that while stepping through the code, the loop crashes with that error before getting to the class that runs the calculations.  Additionally, no arethmetic operations are performed on the form's code-behind page (which calls the method).  I even put a break-point on the class constructor but the code crashed before reaching that constructor.

The information being sent is the results of matches from a sporting event.  As a troubleshooting measure I changed the order of the matches by deleting them an re-entering them into the database in a different order.  Again only this one particular match crashed.

Here is the code that crashes:

cRating.SetRating(dMatchDate, iSetID, 8, iHPlayerID, 

foreach item of a class


I've made a class with a sub class, example:

    public class FieldCollection
        public class PValues
            public string stdFieldName { get { return stdFieldName; } set { stdFieldName = value; } }
            public string excelFieldName { get { return excelFieldName; } set { excelFieldName = value; } }
            public string Value { get { return Value; } set { Value = value; } }

            public void PValues(string _stdFieldName, string _dbaseFieldName, string _Value)
                stdFieldName = _stdFieldName;

        public PValues ExhibitorID { get { return ExhibitorID; } set { ExhibitorID = value; } }
        public PValues UserName { get { return UserName; } set { UserName = value; } }


There are more Pvalue's in it.
I like to run dynamically trough all var's in this class.
how can i do that?

what i want is something like this:

        ImportClass.FieldCollection fc;

        //fill FC

        foreach (ImportClass.FieldCollection.PValues v in fc)


Executing method of the Generic class using Reflection.



I have a generic class:

 abstract public class BaseTC<T>:IExecutable where T : Core, new()
    private Type2 testInfo;
    private Type1 settings;
    private T core = null;

and I want to load such class from library the class is located in, and create an instance of it and execute methods later on.

I know how to use non-generic classes, but no idea how to call the same with generics.

DataBinding + Extension Method Problem


Inside my repeater I'm calling an extension method like so:


<asp:label id="fileSize" runat="server" text='<%# ((UploadedFile)DataBinder.GetDataItem(Container.DataItem)).Size() %>' />


he above is giving me an "Object not set to an instance of a object" exception inside the extension method. If I comment the above expression out, it works fine, and the items are binded wtih no problems. Any ideas?

very weird method in partial class not listed in intellisense


I have created partial class for winform many times with no problem. When doing the same thing in asp.net I am stucked even with the most basic sample code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
protected void Page

DateTime formatting extension method


Notes: I also updated the pieces of code at http://www.extensionmethod.net/Details.aspx?ID=393

In current project, I get some troubles in DateTime class, about format the date time object with the pattern specific string and the current culture, so I decide to code some extension methods in DataTime class in .NET library. And I think it's it very useful if somebody can use it as right way. I take some idea from this link and coding the enum class for its. After that I also used the Lambda Expression for pass the parameter into the DateTime formatting method. It's really easy for read code. And below is my implementation for that:

+ DateTimeFormat enum class:

    public enum DateTimeFormat

How to visist the method or member of service class?


i have a service interface such as IPublishService(PublishService), and i run the service like below:

Uri publishAddress = new Uri("net.tcp://localhost:8004/QTAServer");
publishHost = new ServiceHost(typeof(PublishService), publishAddress);

suppose the pulishservice has a public member news, how can i visit the news memeber in the server client, i have tried method like below:

PublishService ps = publishHost.SingletonInstance as PublishService;


but it is wrong, why is my method not correct?  is there any other method?

Days360 extension method?


i All,

I'm trying to find C# code that will emulate the excel Days360 function.  Has anyone seen a DateTime extension method that does this?

Thanks in advance!


DrawArc() ... an extension method to DrawingContext

Considering the lack of a DrawArc() method of DrawingContext, here is one that does the job as an extension method of DrawingContext...

    /// <summary>
    /// Draw an Arc of an ellipse or circle. Static extension method of DrawingContext.
    /// </summary>
    /// <param name="dc">DrawingContext</param>
    /// <param name="pen">Pen for outline. set to null for no outline.</param>
    /// <param name="brush">Brush for fill. set to null for no fill.</param>
    /// <param name="rect">Box to hold the whole ellipse described by the arc</param>
    /// <param name=&q
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend