Passing checkedlistbox.checked items cast as a List<BuiltInCategory> from a Form instance to a Revit class function - winforms

I am not having any issues making calls from the form instance to the Revit class. It's when I try to assign a List to the Revit class's function categoryList(), that I get a variable doesn't exist in the context error. I tried prefixing a reference to the instance of the form class "Form UF = new Form;" This doesn't work.
//The Revit Class
public Result Execute(ExternalCommandData commandData, ref string message,....)
{
User_Form UF = new User_Form(commandData);
UF.ShowDialog();
public List<BuiltInCategory> categoryList(Document doc, int intSwitch)
{
//list built in categories for built in filter_1
builtInCats_List = new List<BuiltInCategory>();
switch (intSwitch)
{
case (1):
...
case (3):
...
case (4):
{
builtInCats_List = newStateCb1;
return builtInCats_List;
}
default:
{
builtInCats_List = newStateCb1;
return builtInCats_List;
}
}
}
using Form = System.Windows.Forms.Form;
using WS = ModelAuditor_2014.WorksetSorter_2014;
using ModelAuditor_2014;
using System.Threading;
//The Form
namespace ModelAuditor_2014
{
public partial class User_Form : Form
{
//Constructor
WorksetSorter_2014 WS = new WorksetSorter_2014();
//Revit references
public Autodesk.Revit.UI.UIApplication rvtUiApp;
public Autodesk.Revit.UI.UIDocument rvtUiDoc;
public Autodesk.Revit.ApplicationServices.Application rvtApp;
//Global Variables
public List<BuiltInCategory> Filter01_CategoryList;
public List<BuiltInCategory> Filter02_CategoryList;
public int intSwitch;
public List<BuiltInCategory> newStateCb1;
public User_Form(ExternalCommandData commandData)
{
//Revit references
rvtUiApp = commandData.Application;
rvtUiDoc = rvtUiApp.ActiveUIDocument;
rvtApp = rvtUiApp.Application;
InitializeComponent();
}
public void User_Form_Load(object sender, EventArgs e)
{
//use rvtDoc = Doc
Autodesk.Revit.DB.Document rvtDoc = .....
//CheckedListBox for filter01
checkedListBox1.DataSource = WS.categoryList(rvtDoc, intSwitch = 1);
Filter01_CategoryList = new List<BuiltInCategory>();
Filter01_CategoryList = WS.RetrieveSchema(rvtDoc, false);
foreach (BuiltInCategory ChkedB1 in Filter01_CategoryList)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.Items[i].ToString() == ChkedB1.ToString())
{
checkedListBox1.SetItemChecked(i, true);
}
}
}
public List<BuiltInCategory> returnNewStateCB1()
{
newStateCb1 = checkedListBox1.CheckedItems.Cast
<BuiltInCategory>().ToList<BuiltInCategory>();
return newStateCb1;
}

I passed the list from the win form to another public function in the revit app, I was able to access the list returned by this function.

Related

.net c# WebChannelFactory keepAliveEnabled=false with webHttpBehavior

I create WebChannelFactory. I need set KeepAlive=false.
I found only one solution use CustomBinding. and set property keepAliveEnabled to false.
I use custom behavior for my factory also.
Code:
static CustomBinding GetBinding(MySettings serviceSettings = null)
{
var customBinding = new CustomBinding();
HttpTransportBindingElement transportBindingElement = new HttpTransportBindingElement();
transportBindingElement.KeepAliveEnabled = false;
transportBindingElement.MaxBufferSize = 0x07000000;
transportBindingElement.MaxReceivedMessageSize = 0x07000000;
if (serviceSettings != null)
{
customBinding.SendTimeout = serviceSettings.SendTimeout;
}
customBinding.Elements.Add(transportBindingElement);
return customBinding;
}
var customBinding = GetBinding(serviceSettings);
WebChannelFactory<TChannel> factory = new WebChannelFactory<TChannel>(customBinding, new Uri(url));
factory.Endpoint.Behaviors.Add(new MyWebHttpBehavior(userId));
class MyWebHttpBehavior : IEndpointBehavior
{
private int _userId;
public MyWebHttpBehavior(int userId)
{
_userId = userId;
}
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{ }
public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.ClientRuntime behavior)
{
behavior.MessageInspectors.Add(new MyClientMessageInspector(_userId));
}
public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{ }
public void Validate(ServiceEndpoint serviceEndpoint)
{ }
}
In current situation i get error "does not have a Binding with the None MessageVersion".

Saving data from DDBB into AS class

i am trying to make a "game" in Flex similar to Shake&Fidget. I'm saving all the user data in a mysql database and I retrieve the data using ZendFramework (PHP).
I thought of saving all the user and character info I'm going to need into am AS class so I can use that data in every view.
The data recived from the database is correct. I was able to load it in my app labbels but everytime i changed views it had to ask it again, so i thought of making this classes in order to just ask once for the information.
I'll post here the files so it's all more clear.
ViewNavigatorAplication.mxml
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="views.RotrHomeView"
persistNavigatorState="true">
<fx:Script>
<![CDATA[
import flash.net.registerClassAlias;
import valueObjects.Character;
registerClassAlias("Character", valueObjects.Character);
]]>
</fx:Script>
The first View goes to the login screen, it works OK. So i'll go to the view that loads and "tries" to show the character data.
char_panel.mxml
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:usersservice1="services.usersservice1.*"
title="Character Sheet"
viewActivate="char_panel_viewActivateHandler()">
<fx:Script>
<![CDATA[
import mx.binding.utils.*;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import valueObjects.Character;
//[Bindable]protected var character:Character = new Character();
public function updateStats():void{
var str:int = parseInt(getGlobalStatsResult.lastResult.globalSTR) + parseInt(getCharacterStatsResult.lastResult.str);
var dex:int = parseInt(getGlobalStatsResult.lastResult.globalDEX) + parseInt(getCharacterStatsResult.lastResult.dex);
var intel:int = parseInt(getGlobalStatsResult.lastResult.globalINT) + parseInt(getCharacterStatsResult.lastResult.intel);
var cha:int = parseInt(getGlobalStatsResult.lastResult.globalCHA) + parseInt(getCharacterStatsResult.lastResult.cha);
var sta:int = parseInt(getGlobalStatsResult.lastResult.global_VIT) + parseInt(getCharacterStatsResult.lastResult.vit);
data.modStats(str,intel,cha,sta,dex)
data.showStats(lb_show_str,lb_show_dex,lb_show_int,lb_show_cha,lb_show_vit);
//character.showStats(lb_show_str,lb_show_dex,lb_show_int,lb_show_cha,lb_show_vit);
}
public function char_panel_viewActivateHandler():void{
if(!data){
data = new Character();
}
getCharacterStatsResult.token = usersService1.getCharacterStats("user01");
getGearListResult.addEventListener(ResultEvent.RESULT,onResult);
getGearListResult.token = usersService1.getGearList();
}
public function onStatsResult(event:ResultEvent):void{
if(getGlobalStatsResult.lastResult.globalSTR != null){
updateStats();
}
}
public function onResult(event:ResultEvent):void{
if(getGearListResult.lastResult[0].itemName != null){
getGlobalStatsResult.addEventListener(ResultEvent.RESULT, onStatsResult);
getGlobalStatsResult.token = usersService1.getGlobalStats("user01");
currentState = "Character";
}
}
]]>
</fx:Script>
<s:states>
<s:State name="Loading"/>
<s:State name="Character"/>
</s:states>
<fx:Declarations>
<s:CallResponder id="getCharacterStatsResult"/>
<s:CallResponder id="getGearListResult"/>
<s:CallResponder id="getGlobalStatsResult"/>
<usersservice1:UsersService1 id="usersService1"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label id="lb_show_str" includeIn="Character" x="119" y="46" text=""/>
<s:BusyIndicator includeIn="Loading" left="172" right="171" top="148" bottom="424"
horizontalCenter="0" verticalCenter="-138"/>
<s:Label id="lb_show_int" includeIn="Character" x="119" y="90"/>
<s:Label id="lb_show_cha" includeIn="Character" x="119" y="139"/>
<s:Label id="lb_show_vit" includeIn="Character" x="119" y="191"/>
<s:Label id="lb_show_dex" includeIn="Character" x="119" y="243"/>
As for the AS classes:
Character.as
package valueObjects{
import mx.data.ChangedItems;
import mx.messaging.channels.StreamingAMFChannel;
import spark.components.Label;
import spark.skins.spark.StackedFormHeadingSkin;
[Bindable]
public class Character
{
private var _name:String;
private var _stats:FinalStats;
private var _gear:GearList;
public function Character()
{
this._stats = new FinalStats();
this._gear = new GearList();
}
public function modStats(str:int,intel:int,cha:int,sta:int,dex:int):void{
this._stats.modStr(str);
this._stats.modInt(intel);
this._stats.modCha(cha);
this._stats.modVit(sta);
this._stats.modDex(dex);
}
public function getStats():Array{
var aStats:Array;
aStats["str"]=this._stats.getStr();
aStats["int"]=this._stats.getInt();
aStats["cha"]=this._stats.getCha();
aStats["sta"]=this._stats.getVit();
aStats["dex"]=this._stats.getDex();
return aStats;
}
public function setName(charName:String):void{
this._name = charName;
}
public function getName():String{
return this._name;
}
public function showStats(lbSTR:Label, lbDEX:Label, lbINT:Label, lbCHA:Label, lbVIT:Label):void{
lbSTR.text = "" + this._stats.getStr();
lbDEX.text = "" + this._stats.getDex();
lbINT.text = "" + this._stats.getInt();
lbCHA.text = "" + this._stats.getCha();
lbVIT.text = "" + this._stats.getVit();
}
}}
FinalStats.as
package valueObjects{
public class FinalStats
{
private var str:int = 0;
private var intel:int = 0;
private var cha:int = 0;
private var sta:int = 0;
private var dex:int = 0;
public function FinalStats()
{
}
public function getStr():int{
return this.str;
}
public function modStr(x:int):void{
this.str+=x;
}
public function getDex():int{
return this.dex;
}
public function modDex(x:int):void{
this.dex+=x;
}
public function getInt():int{
return this.intel;
}
public function modInt(x:int):void{
this.intel+=x;
}
public function getCha():int{
return this.cha;
}
public function modCha(x:int):void{
this.cha+=x;
}
public function getVit():int{
return this.sta;
}
public function modVit(x:int):void{
this.sta+=x;
}
}
}
And the last one GearList.as
package valueObjects{
import mx.data.ChangedItems;
import mx.messaging.channels.StreamingAMFChannel;
import spark.components.Label;
import spark.skins.spark.StackedFormHeadingSkin;
public class GearList
{
private var _headID:String;
private var _shoulderID:String;
private var _chestID:String;
private var _bracersID:String;
private var _glovesID:String;
private var _pantsID:String;
private var _bootsID:String;
private var _main_handID:String;
private var _off_handID:String;
public function GearList()
{
}
public function showStats(lbHead:Label, lbShoulder:Label, lbChest:Label, lbBracer:Label, lbGlove:Label, lbPants:Label, lbBoots:Label, lbMainHand:Label, lbOffHand:Label):void{
lbHead.text = ""+this._headID;
lbShoulder.text = ""+this._shoulderID;
lbChest.text = ""+this._chestID;
lbBracer.text = ""+this._bracersID;
lbGlove.text = ""+this._glovesID;
lbPants.text = ""+this._pantsID;
lbBoots.text = ""+this._bootsID;
lbMainHand.text = ""+this._main_handID;
lbOffHand.text = ""+this._off_handID;
}
public function getOff_handID():String
{
return _off_handID;
}
public function setOff_handID(value:String):void
{
_off_handID = value;
}
public function getMain_handID():String
{
return _main_handID;
}
public function setMain_handID(value:String):void
{
_main_handID = value;
}
public function getBootsID():String
{
return _bootsID;
}
public function setBootsID(value:String):void
{
_bootsID = value;
}
public function getPantsID():String
{
return _pantsID;
}
public function setPantsID(value:String):void
{
_pantsID = value;
}
public function getGlovesID():String
{
return _glovesID;
}
public function setGlovesID(value:String):void
{
_glovesID = value;
}
public function getBracersID():String
{
return _bracersID;
}
public function setBracersID(value:String):void
{
_bracersID = value;
}
public function getChestID():String
{
return _chestID;
}
public function setChestID(value:String):void
{
_chestID = value;
}
public function getShoulderID():String
{
return _shoulderID;
}
public function setShoulderID(value:String):void
{
_shoulderID = value;
}
public function getHeadID():String
{
return _headID;
}
public function setHeadID(value:String):void
{
_headID = value;
}
}}
If you are still here you have all my respect :D
When i try it out, i get the following error. I've tried in char_panel.mxml using Character::modStats(...) instead of data.modStats i'll put the error log under this one.
Error log when using data.modStats(...)
TypeError: Error #1006: modStats is not a function.
at views::char_panel/updateStats()[C:\Users\Zebrah\Rotr\Rotr\src\views\char_panel.mxml:23]
at views::char_panel/onStatsResult()[C:\Users\Zebrah\Rotr\Rotr\src\views\char_panel.mxml:39]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc::CallResponder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\CallResponder.as:122]
at mx.rpc::AsyncToken/http://www.adobe.com/2006/flex/mx/internal::applyResult()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncToken.as:239]
at mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\events\ResultEvent.as:207]
at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractOperation.as:244]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
at mx.rpc::Responder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
at NetConnectionMessageResponder/resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\channels\NetConnectionChannel.as:552]
at mx.messaging::MessageResponder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\MessageResponder.as:235]
Error Log using Character::modStats(...)
Error: Error #1034: Type Coercion failed: cannot convert valueObjects::Character$ to Namespace.
at views::char_panel/updateStats()[C:\Users\Zebrah\Rotr\Rotr\src\views\char_panel.mxml:23]
at views::char_panel/onStatsResult()[C:\Users\Zebrah\Rotr\Rotr\src\views\char_panel.mxml:39]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc::CallResponder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\CallResponder.as:122]
at mx.rpc::AsyncToken/http://www.adobe.com/2006/flex/mx/internal::applyResult()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncToken.as:239]
at mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\events\ResultEvent.as:207]
at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractOperation.as:244]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
at mx.rpc::Responder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
at NetConnectionMessageResponder/resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\channels\NetConnectionChannel.as:552]
at mx.messaging::MessageResponder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\MessageResponder.as:235]
Thanks in advance to anyone that get so far in this help cry :D i'd apreciate any sugestions that you can make.
ok so the reason it doesn't work with data is that event though you said
data = new Character();
data is still actually an object (that now looks like a character). to make it work
var myChar:Character = Character(data); // cast it to a Character and it now knows the method
myChar.modStats()
the reason the other call didn't work is the way you called it:
Character::modStats(...)
This say look for a method called modStats in the namespace Character. Instead call this
character.modStats()
and it will work.
You Sir are my personal Hero :D, i've just tried it and it shows the data.
I changed the constructor for the character in Character.AS like this:
public function Character(obj:Object)
{
this._stats = new FinalStats();
this._gear = new GearList();
}
I'm not entirely sure if this is correct, but as you mentioned in your answer:
var myChar:Character = Character(data);
I supose i'll have to use that data object to initialice myChar...am i right? Well, later when i have the time i'll try to add new views and buttons to go through views and see if the data is there :D

Filter a collection with LINQ vs CollectionView

I want to filter a ObservableCollection with max 3000 items in a DataGrid with 6 columns. The user should be able to filter in an "&&"-way all 6 columns.
Should I use LINQ or a CollectionView for it? LINQ seemed faster trying some www samples. Do you have any pro/cons?
UPDATE:
private ObservableCollection<Material> _materialList;
private ObservableCollection<Material> _materialListInternal;
public MaterialBrowserListViewModel()
{
_materialListInternal = new ObservableCollection<Material>();
for (int i = 0; i < 2222; i++)
{
var mat = new Material()
{
Schoolday = DateTime.Now.Date,
Period = i,
DocumentName = "Excel Sheet" + i,
Keywords = "financial budget report",
SchoolclassCode = "1",
};
_materialListInternal.Add(mat);
var mat1 = new Material()
{
Schoolday = DateTime.Now.Date,
Period = i,
DocumentName = "Word Doc" + i,
Keywords = "Economical staticstics report",
SchoolclassCode = "2",
};
_materialListInternal.Add(mat1);
}
MaterialList = CollectionViewSource.GetDefaultView(MaterialListInternal);
MaterialList.Filter = new Predicate<object>(ContainsInFilter);
}
public bool ContainsInFilter(object item)
{
if (String.IsNullOrEmpty(FilterKeywords))
return true;
Material material = item as Material;
if (DocumentHelper.ContainsCaseInsensitive(material.Keywords,FilterKeywords,StringComparison.CurrentCultureIgnoreCase))
return true;
else
return false;
}
private string _filterKeywords;
public string FilterKeywords
{
get { return _filterKeywords; }
set
{
if (_filterKeywords == value)
return;
_filterKeywords = value;
this.RaisePropertyChanged("FilterKeywords");
MaterialList.Refresh();
}
}
public ICollectionView MaterialList { get; set; }
public ObservableCollection<Material> MaterialListInternal
{
get { return _materialListInternal; }
set
{
_materialListInternal = value;
this.RaisePropertyChanged("MaterialList");
}
}
Using ICollectionView gives you automatic collection changed notifications when you call Refresh. Using LINQ you'll need to fire your own change notifications when the filter needs to be re-run to update the UI. Not difficult, but requires a little more thought than just calling Refresh.
LINQ is more flexible that the simple yes/no filtering used by ICollectionView, but if you're not doing something complex there's not really any advantage to that flexibility.
As Henk stated, there shouldn't be a noticable performance difference in the UI.
For an interactive (DataGrid?) experience you should probabaly use the CollectionView. For a more code-oriented sorting, LINQ.
And with max 3000 items, speed should not be a (major) factor in a UI.
How about both? Thomas Levesque built a LINQ-enabled wrapper around ICollectionView.
Usage:
IEnumerable<Person> people;
// Using query comprehension
var query =
from p in people.ShapeView()
where p.Age >= 18
orderby p.LastName, p.FirstName
group p by p.Country;
query.Apply();
// Using extension methods
people.ShapeView()
.Where(p => p.Age >= 18)
.OrderBy(p => p.LastName)
.ThenBy(p => p.FirstName)
.Apply();
Code:
public static class CollectionViewShaper
{
public static CollectionViewShaper<TSource> ShapeView<TSource>(this IEnumerable<TSource> source)
{
var view = CollectionViewSource.GetDefaultView(source);
return new CollectionViewShaper<TSource>(view);
}
public static CollectionViewShaper<TSource> Shape<TSource>(this ICollectionView view)
{
return new CollectionViewShaper<TSource>(view);
}
}
public class CollectionViewShaper<TSource>
{
private readonly ICollectionView _view;
private Predicate<object> _filter;
private readonly List<SortDescription> _sortDescriptions = new List<SortDescription>();
private readonly List<GroupDescription> _groupDescriptions = new List<GroupDescription>();
public CollectionViewShaper(ICollectionView view)
{
if (view == null)
throw new ArgumentNullException("view");
_view = view;
_filter = view.Filter;
_sortDescriptions = view.SortDescriptions.ToList();
_groupDescriptions = view.GroupDescriptions.ToList();
}
public void Apply()
{
using (_view.DeferRefresh())
{
_view.Filter = _filter;
_view.SortDescriptions.Clear();
foreach (var s in _sortDescriptions)
{
_view.SortDescriptions.Add(s);
}
_view.GroupDescriptions.Clear();
foreach (var g in _groupDescriptions)
{
_view.GroupDescriptions.Add(g);
}
}
}
public CollectionViewShaper<TSource> ClearGrouping()
{
_groupDescriptions.Clear();
return this;
}
public CollectionViewShaper<TSource> ClearSort()
{
_sortDescriptions.Clear();
return this;
}
public CollectionViewShaper<TSource> ClearFilter()
{
_filter = null;
return this;
}
public CollectionViewShaper<TSource> ClearAll()
{
_filter = null;
_sortDescriptions.Clear();
_groupDescriptions.Clear();
return this;
}
public CollectionViewShaper<TSource> Where(Func<TSource, bool> predicate)
{
_filter = o => predicate((TSource)o);
return this;
}
public CollectionViewShaper<TSource> OrderBy<TKey>(Expression<Func<TSource, TKey>> keySelector)
{
return OrderBy(keySelector, true, ListSortDirection.Ascending);
}
public CollectionViewShaper<TSource> OrderByDescending<TKey>(Expression<Func<TSource, TKey>> keySelector)
{
return OrderBy(keySelector, true, ListSortDirection.Descending);
}
public CollectionViewShaper<TSource> ThenBy<TKey>(Expression<Func<TSource, TKey>> keySelector)
{
return OrderBy(keySelector, false, ListSortDirection.Ascending);
}
public CollectionViewShaper<TSource> ThenByDescending<TKey>(Expression<Func<TSource, TKey>> keySelector)
{
return OrderBy(keySelector, false, ListSortDirection.Descending);
}
private CollectionViewShaper<TSource> OrderBy<TKey>(Expression<Func<TSource, TKey>> keySelector, bool clear, ListSortDirection direction)
{
string path = GetPropertyPath(keySelector.Body);
if (clear)
_sortDescriptions.Clear();
_sortDescriptions.Add(new SortDescription(path, direction));
return this;
}
public CollectionViewShaper<TSource> GroupBy<TKey>(Expression<Func<TSource, TKey>> keySelector)
{
string path = GetPropertyPath(keySelector.Body);
_groupDescriptions.Add(new PropertyGroupDescription(path));
return this;
}
private static string GetPropertyPath(Expression expression)
{
var names = new Stack<string>();
var expr = expression;
while (expr != null && !(expr is ParameterExpression) && !(expr is ConstantExpression))
{
var memberExpr = expr as MemberExpression;
if (memberExpr == null)
throw new ArgumentException("The selector body must contain only property or field access expressions");
names.Push(memberExpr.Member.Name);
expr = memberExpr.Expression;
}
return String.Join(".", names.ToArray());
}
}
Credit:
http://www.thomaslevesque.com/2011/11/30/wpf-using-linq-to-shape-data-in-a-collectionview/
Based on a visual complexity and number of items there really WILL be a noticable performance difference since the Refresh method recreates the whole view!!!
You need my ObservableComputations library. Using this library you can code like this:
ObservableCollection<Material> MaterialList = MaterialListInternal.Filtering(m =>
String.IsNullOrEmpty(FilterKeywords)
|| DocumentHelper.ContainsCaseInsensitive(
material.Keywords, FilterKeywords, StringComparison.CurrentCultureIgnoreCase));
MaterialList reflects all the changes in the MaterialListInternal collection. Do not forget to add the implementation of the INotifyPropertyChanged interface to Material class, so that MaterialList collection reflects the changes in material.Keywords property.

Calling .cs file/program upon click of Button

//compares text to the id number below
}
CLASS BEING CALLED IS A SEPERATE .CS FILE ALL IN THE SAME PROJECT AND DESCRIBED AS BELOW
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;
namespace Eagle_Eye_Class_Finder
{
class GetSchedule
{
class IDnumber
{
public string Name { get; set; }
public string ID { get; set; }
public string year { get; set; }
public string class1 { get; set; }
public string class2 { get; set; }
public string class3 { get; set; }
public string class4 { get; set; }
}
//
// Displays the Students Class Schedule.
//
Console.WriteLine("--- Students Class Schedule ---");
foreach (IDnumber IDnumber in IDnumbers)
{
Console.Write(IDnumber.Name);
Console.Write(": ");
Console.WriteLine(IDnumber.ID);
Console.WriteLine(IDnumber.year);
Console.WriteLine(IDnumber.class1);
Console.WriteLine(IDnumber.class2);
Console.WriteLine(IDnumber.class3);
Console.WriteLine(IDnumber.class4);
//get { return this.label1.Text; }
//set { this.label1.Text = class1; }
//get { return this.label2.Text; }
//set { this.label2.Text = class2; }
//get { return this.label3.Text; }
//set { this.label3.Text = class3; }
//get { return this.label4.Text; }
//set { this.label1.Text = class4; }
}
// Clear first two elements in IDnumbers array.
Array.Clear(IDnumbers, 0, Math.Min(2, IDnumbers.Length));
}
}
}
Modify the definition of GetSchedule to Public Class and Public Static Main and then David's code will work.
The default access level for a class defined with just class is internal (C#)/ friend (VB).
The obvious answer is
Eagle_Eye_Class_Finder.GetSchedule.Main()
But, it seems to me that you're hoping to do something with the value
900456317
For that to work, you'd have to declare an additional function which receives that value as a parameter, and then does something useful with it. Maybe something like:
static void ProcessNumber(IDNumber myNum)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(IDnumber.Name);
myData.AppendLine(": ");
myData.AppendLine(IDnumber.ID);
myData.AppendLine(IDnumber.year);
myData.AppendLine(IDnumber.class1);
myData.AppendLine(IDnumber.class2);
myData.AppendLine(IDnumber.class3);
myData.AppendLine(IDnumber.class4);
MessageBox.Show(myData);
}
Then you can call it like:
if (text == "900456317")
{
Eagle_Eye_Class_Finder.GetSchedule.ProcessNumber(new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" });
}
Might be easier if you could give more detail on exactly what you're hoping to accomplish.
EDIT
What you need to do for that to work is move the concept of the IDnumbers array out of the function call itself into a class member. For example, consider adding the following code:
IDnumber[] IDnumbers = new IDnumber[3];
public GetSchedule()
{
IDnumbers[0] = new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson",ID = "900456319",year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };
}
This will cause that code to run every time you create a new instance of your class. Then, you can have a function
public string GetDataFromNumber(string ID)
{
foreach (IDnumber idCandidateMatch in IDnumbers)
{
if (IDCandidateMatch.ID == ID)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(IDnumber.Name);
myData.AppendLine(": ");
myData.AppendLine(IDnumber.ID);
myData.AppendLine(IDnumber.year);
myData.AppendLine(IDnumber.class1);
myData.AppendLine(IDnumber.class2);
myData.AppendLine(IDnumber.class3);
myData.AppendLine(IDnumber.class4);
return myData;
}
}
return "";
}
And then your method call from Form1 changes to:
public void button2_Click(object sender, System.EventArgs e)
{
string text = textBox1.Text;
Mainform = this;
this.Hide();
GetSchedule myScheduleFinder = new GetSchedule();
string result = myScheduleFinder.GetDataFromNumber(text);
if (!string.IsNullOrEmpty(result))
{
MessageBox.Show(result);
}
else
{
MessageBox.Show("Enter A Valid ID Number!");
}
}
The idea here is to break your program into a series of smaller 'parts', each of which is responsible for doing one thing, and doing it well. In this case, the class 'GetSchedule' represents the part of your program which, given an ID number, is able to retrieve a description of that user. The line of code above that reads
GetSchedule myScheduleFinder = new GetSchedule();
basically says "I want a new copy of my GetSchedule class, and I want to keep track of it with the name 'myScheduleFinder'". Whenever you see the word 'new' in C#, that's what's happening. When the word new is followed by the classname and parenthesis, its invoking what's called the 'constructor'. Constructors are basically special functions that are called every time the class is created; in your case, its the code we placed in the method
public GetSchedule()
Now, given that we have a copy of the GetSchedule class, properly initialized by our constructor, we can call the ProcessNumber function on that class, passing in the number we're searching for. The 'if' statement basically makes sure that of all of our possible records, we're only using the one that has the same ID. We then take that record, convert it to a string, and return it. Then we show it in a nifty little message box, although you can obviously do with it whatever you want at that point.
This should do it:
Eagle_Eye_Class_Finder.GetSchedule.Main()

Silverlight MVVM Validation in a DataForm

I am using generic data classes, so I can't use ria services attributes to control my validation - so I am looking for a way to manualy set up validation to work in a DataForm.
public partial class DataValue
{
private Dictionary<string, string> _errors = new Dictionary<string, string>();
public Dictionary<string, string> Errors
{
get { return _errors; }
}
public Object Value
{
get
{
object result = new object();
switch ((DataType)this.ModelEntity.ModelItem.DataType)
{
case DataType.Money:
return result = this.ValueText.ParseNullableFloat();
case DataType.Integer:
return result = this.ValueText.ParseNullableInt();
case DataType.Date:
case DataType.Time:
return result = this.ValueText.ParseNullableDateTime();
case DataType.CheckBox:
return result = this.ValueText;
default:
return result = this.ValueText;
}
}
set
{
if (!String.IsNullOrEmpty(value.ToString()))
{
bool invalid = false;
switch ((DataType)this.ModelEntity.ModelItem.DataType)
{
case DataType.Money:
float val;
if (!float.TryParse(value.ToString(), out val)) invalid = true;
break;
case DataType.Integer:
int val2;
if (!Int32.TryParse(value.ToString(), out val2)) invalid = true;
break;
case DataType.Date:
case DataType.Time:
DateTime val3;
if (!DateTime.TryParse(value.ToString(), out val3)) invalid = true;
break;
}
if (invalid == false)
ValueText = value.ToString();
else
{
ValueText = "";
_errors.Add(this.ModelEntity.LocalName, "error writing " + value.ToString() + " to " + this.ModelEntity.ModelItem.Label);
}
}
else
ValueText = "";
}
}
public partial class ModelValidater : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ModelValidatorId;
private int _ModelEntityId;
private int _ValidatorType;
private string _ValidatorParameters;
So in ASP MVC, I simply manually checked against these rules when the form was submitted... which I guess is pretty much what I want to do in MVVM (I am just not sure the best way to go about this).
ASP Code
protected bool ModelErrors(RecordDictionary record)
{
bool result = false;
foreach (var field in record)
{
foreach (var error in field.Value.Errors)
{
result = true;
ModelState.AddModelError(error.Key + "Validation", error.Value.ToString());
}
}
return result;
}
Silverlight 3 built-in validation is based on exceptions.
Just throw a meaningful exception in your generic Setter and you should be fine.
Remember to set ValidatesOnException=True and NotifyOnValidationError=True on your {Binding}.
Jesse has a good sample of validation with exceptions on his blog.
You can attach the validation attributes using the MetadataTypeAttribute attribute.
RIA Services will automatically generate these validation on the client for you once they're exposed in the DomainService.
Example:
[MetadataType(typeof(ContactMd))]
public partial class Contact
{
internal class ContactMd
{
[MyCustomValidation]
public string Name { get; set; }
}
}
(MyCustomValidation refers to anything that inherits from ValidationAttribute).

Resources