Category: c#

Aug 12 2016

How can you create a c# class that creates instances of itself using a static method but still has access to the instance methods and properties?

I am trying to create a class that can create itself using a static method but where you can still access the instance member after it creates itself. Why am I finding this so hard to do?

public class Widget
{
  public static Widget Init()
  {
    return new Widget();
  }

  public string WidgetUPC {get; set;}
}

...when I create an instance like this...

var w = Widget.Init();

I cannot set the property like this...

w.WidgetUPC = 'someUPC';

The instance Why not?

What changes can I make to get this to work?

Sep 25 2015

How can I use Linq and c# to compare a property against a List<string>

I have code that is filtering a List using code like this..

    List<Product> products = new List<Product>() { /*<INIT THE COLLECTION>*/ }
    //get the ones you need.
    var newListOfProducts = products.Where(p=>p.MyProperty == "prop1" || p.MyProperty == "prop2" || p.MyProperty == "prop3");

My preferences is to use syntax like this...

List<string> stringsToCompare = new List<string>() {"prop1","prop2","prop3"};
var newListOfProducts = products.Where(p=>p.MyProperty.IsInList(stringsToCompare));

This way I can dynamically build the stringToCompare instead of hard-coding them.

But I can't figure out how to do it despite googling for the last half-hour. I think Intersect or Union can work but I can't get the syntax right.

Sep 25 2015

How can I use Linq and c# to compare a property against a List<string>

I have code that is filtering a List using code like this..

    List<Product> products = new List<Product>() { /*<INIT THE COLLECTION>*/ }
    //get the ones you need.
    var newListOfProducts = products.Where(p=>p.MyProperty == "prop1" || p.MyProperty == "prop2" || p.MyProperty == "prop3");

My preferences is to use syntax like this...

List<string> stringsToCompare = new List<string>() {"prop1","prop2","prop3"};
var newListOfProducts = products.Where(p=>p.MyProperty.IsInList(stringsToCompare));

This way I can dynamically build the stringToCompare instead of hard-coding them.

But I can't figure out how to do it despite googling for the last half-hour. I think Intersect or Union can work but I can't get the syntax right.

Jun 03 2014

Which DataGridView event do I use when I want to change the type of a cell at runtime?

I have a winform application that has a user control that is using a DataGridView control on it.

When rendering the DataGridView I want some of the rows Quantity columns cells to be read only DataGridViewTextBox cells and other rows to a a DataGridViewComboBoxCell.

What is the appropriate DataGridView event to use this. CellFormatting or CellPaint or some other event?

Jun 03 2014

Which DataGridView event do I use when I want to change the type of a cell at runtime?

        <p>I have a winform application that has a user control that is using a DataGridView control on it. </p>

When rendering the DataGridView I want some of the rows Quantity columns cells to be read only DataGridViewTextBox cells and other rows to a a DataGridViewComboBoxCell.

What is the appropriate DataGridView event to use this. CellFormatting or CellPaint or some other event?

Feb 20 2014

What is the best way to create "abstract" class members when declaring the class as abstract is not an option?

        <p>There is a class that I want to create as abstract but I can't because that class's base class is ActiveReports and as soon as I make it abstract the sub-reports are no longer designable.  (Interestingly the base class itself IS designable but not it's children).</p>
I know that I cannot declare class members as abstract without first declaring the class as abstract. In this case what is the NEXT BEST way to do this. At this point I made the members virtual and added comments to each one. Where it made sense I also declared a member as protected rather than public. But is there some other best way that would REQUIRE that these members be overridden at compile-time rather than run-time? If you were in this situation how would you do it?
Feb 20 2014

What is the best way to create "abstract" class members when declaring the class as abstract is not an option?

There is a class that I want to create as abstract but I can't because that class's base class is ActiveReports and as soon as I make it abstract the sub-reports are no longer designable. (Interestingly the base class itself IS designable but not it's children). I know that I cannot declare class members as abstract without first declaring the class as abstract. In this case what is the NEXT BEST way to do this. At this point I made the members virtual and added comments to each one. Where it made sense I also declared a member as protected rather than public. But is there some other best way that would REQUIRE that these members be overridden at compile-time rather than run-time? If you were in this situation how would you do it? EDIT Let me explain further. I can create a class (lets call it MyReportsBase) that inherits from ActiveReports. (I do that using add new and choose the ActiveReports type) Then ActiveReports provides a property of the report object in MyReportsBase (in design time properties) called MasterClass. Once I set that property to true then I can then create new reports and inherit from MyReportsBase instead of directly from ActiveReports. (The way you do that is by create add new ActiveReports but then edit the code in the code-behind to inherit from MyReportsBase instead of ActiveReports). That is all well and good and it all works perfectly well. However, my requirement is that MyUpwardBase would HAVE to be inherited (declared abstract with a few abstract members). And I CAN go in the code-behind and set the abstract modifier on the base class. And even then I can design MyReportsBase. However, at that point, all of the reports that I inherited from MyReportsBase are no longer designable. Attempting to open the derived reports in the designer throws an error about how the designer cannot open the base class because it is declared as abstract. All of this is to just clarify the question. Right now the Interface suggestion is making the most sense to me. EDIT 2
An Interface doesn't really do it for me either for this reason. The base classes "abtract-but-aren't-abstract" members HAVE To be present in my base classes because I have virtual and public members that depend on them. And in my case, because I cannot declare the class as abstract, I have to have an implementation for those members. As soon as I provide an implementation for those members then all of my inheriting classes ALSO have the implementation and the Interface doesn't complain at compile time. I thought by removing the virtual modifier from those members that the compiler would require them but it did not behave that way. I am beginning to think that my solution (document the members well) is the best solution.
Feb 13 2014

Creating an Orchard web application with customers, memberships, and silo-ing data

        <p>I have an idea on setting up an Orchard site that would provide "out-of-the-box" functionality like blogging, messaging, and calendaring.  Lets call this new web site coolApp.</p>
Each customer would have there own domain like coolApp.somedomain.com and coolApp.someotherdomain.com. I know that Orchard supports multi-tenancy so this is not a problem. However, I want to make sure that at all times the users of coolApp.somedomain.com and coolApp.someotherdomain.com are not fetching each others data. (This is a theme you will hear throughout this questions.) So each CUSTOMER is silo-ed from the other customers. Now each customer can setup groups. Each group has a manager and it has members. Each group gets the out-of-the-box functionality for its group. The manager can blog, setup event calendars, and message, etc to HIS/HER group members only. The manager can also setup other members to do the same. But, as with customers, they cannot see other members group information and data. However, the CUSTOMERS have the same out-of-the-box functionality to ALL its group managers and/or members all at once. So a customer can message,blog, etc to all of its groups. I think I would like it to work like this in terms of the http endpoints. somedomain.com can do its blogging, messaging, etc from coolapp.somedomain.com. Each group would do its blogging, messaging from coolapp.somedomain.com/group1, coolapp.somedomain.com/group2 etc. One thing I did not mention is that some of the out-of-the-box functionality will be custom functionality for my use cases. So it will not JUST do the blogging, calendar, etc but I will write custom modules. And, of course, I would like all of this to be fairly automated. Both in terms of setting up new customers (coolapp.somethirddomain.com) and, within each customer, make it easy to setup new groups. (BTW, it is way more import that setting up new groups be easier than new customers.) So, with all of that in mind, here are my questions.
  1. How well does Orchard support that without any programming? It seems that Orchard may be already setup to do that but I am not sure how. What features do I need to study and understand to achieve that.

  2. How do I write my custom module so that it will do the horizontally partitioning/siloing of the data. What PROGRAMMING features do I need to understand to achieve that.

  3. What advice can you give me about automating the creation of groups (new endpoints) in Orchard.

  4. Any gotchas that I should be wary of.

  5. I really want to do this in Orchard because I love Orchard and because I am a C# programmer. But if Orchard is not a good fit, can someone recommend a .NET free (or almost-free) solution that would work better.

Thanks for your help. Seth

Dec 04 2013

How do you get CSLA 3.02 BusinessListBase ListChanged event to identify the child object whose OnPropertyChanged triggered the event?

        <p>I have a project that is using CSLA 3.0.2.</p>

I have a BusinessListBase collection object that has child items that have an IsDefault property.

When a child object has its IsDefault property set to true, I want to set the other child members IsDefault property to false.

I am calling the OnPropertyChanged("IsDefault") in the child setter and I have that raising the collections ListChanged event. However, the sender of the event is the Collection object, not the child object that raised the event. Nor is the child item in the ListChangedEventArgs (e).

How do I get a reference to the specific child instance that raised the event?

Or should I be doing this some other way?

Any help is appreciated.

Dec 04 2013

How do you get CSLA 3.02 BusinessListBase ListChanged event to identify the child object whose OnPropertyChanged triggered the event?

        <p>I have a project that is using CSLA 3.0.2.</p>

I have a BusinessListBase collection object that has child items that have an IsDefault property.

When a child object has its IsDefault property set to true, I want to set the other child members IsDefault property to false.

I am calling the OnPropertyChanged("IsDefault") in the child setter and I have that raising the collections ListChanged event. However, the sender of the event is the Collection object, not the child object that raised the event. Nor is the child item in the ListChangedEventArgs (e).

How do I get a reference to the specific child instance that raised the event?

Or should I be doing this some other way?

Any help is appreciated.