Dictionary object syntax? - idictionary

I'm having trouble figuring out the syntax for a JScript .NET dictionary object. I have tried
private var myDictionary: Dictionary<string><string>;
but the compiler complains that it's missing a semicolon and that the Dictionary object is not declared.
I know JScript does have a native dictionary-like object format, but I'm not sure if there are disadvantages to using it instead of the .NET-specific construct. I.e., what if someone wants to extend my script using another .NET language?

JScript.Net doesn't support generic types and methods.
Check this link:
http://msdn.microsoft.com/en-us/library/xfhwa508(v=VS.80).aspx
Click on the JScript Tab under Syntax heading.

There is One way to use the Dictionary Type in JScript.Net
Create a c# assembly
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample
{
class Tools
{
public Dictionary<string,object> Dict()
{
return new Dictionary<string,object>();
}
}
}
IN JScript
import System;
import System.Text;
package Sample
{
public class Main
{
public var MyDictionary = Tools.Dict();
function Main()
{
MyDictionary["test"] = "Works";
Console.WriteLine(MyDictionary["test"]);
}
}
}
FTW
so you can use c# assembly with your JScript.net stuff

Related

Issues with tutorial - Serializing data from/to disk

So I'm trying to work through the Catel 'getting started' examples here:
https://catelproject.atlassian.net/wiki/display/CTL/Getting+started+with+WPF
But I'm getting some errors in visual studio on step 3 (Serializing data from/to disk) - https://catelproject.atlassian.net/wiki/pages/viewpage.action?pageId=15630363
I create a 'top container' model called 'Settings' and a 'child class' of this called 'Global' (pretty much exactly the same as the tutorial except for less properties and different model names).
I create an interface based on the example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App.Models;
namespace App.Services.Interfaces
{
public interface IGlobalService
{
IEnumerable<Global> LoadGlobals();
void SaveGlobals(IEnumerable<Global> globals);
}
}
Then I create the service implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Catel.Collections;
using Catel.Data;
using App.Models;
using App.Services.Interfaces;
namespace App.Services
{
public class GlobalService : IGlobalService
{
private readonly string _path;
public GlobalService()
{
string directory = Catel.IO.Path.GetApplicationDataDirectory("CatenaLogic", "WPF.GettingStarted");
_path = Path.Combine(directory, "global.xml");
}
public IEnumerable<Global> LoadGlobals()
{
if (!File.Exists(_path))
{
return new Global[] { };
}
using (var fileStream = File.Open(_path, FileMode.Open))
{
var settings = Settings.Load(fileStream, SerializationMode.Xml);
return settings.Globals;
}
}
public void SaveGlobals(IEnumerable<Global> globals)
{
var settings = new Settings();
settings.Globals.ReplaceRange(globals);
settings.Save(_path, SerializationMode.Xml);
}
}
}
Visual studio then throws 2 errors and a warning:
Error CS0619 'SavableModelBase.Load(Stream,
SerializationMode)' is obsolete: 'Please use Load(Stream,
SerializationMode, ISerializationConfiguration) instead. Will be
removed in version
5.0.0.'
Error CS0619 'SavableModelBase.Save(string,
SerializationMode)' is obsolete: 'Please use Save(string,
SerializationMode, ISerializationConfiguration) instead. Will be
removed in version 5.0.0.'
Warning CS0618 'CollectionExtensions.ReplaceRange(ObservableCollection,
IEnumerable)' is obsolete: 'Please use ReplaceRange(this
ICollection<T>, IEnumerable<T>) instead. Will be treated as an error
from version 5.0.0. Will be removed in version 5.0.0.'
So far all research I have done on this has come up blank. What is 'ISerializationConfiguration' and how do I implement it? Am I missing something obvious?
Setup is:
Visual Studio 2015 Community (14.0.25425.01 Update 3)
Project targeting .NET 4.5.2
Project initialized using New > Online > WPF application using Catel
NuGet:
Catel.Core 4.5.3
Catel.Extensions.Controls 4.5.3
Catel.MVVM 4.5.3
Catel.Fody 2.14.0
Any help would be much appreciated.
Use the overload as specified by the error / warning:
Load(stream, null);
Save(stream, null);

WPF dialogbox to select file or folder

I would like to know if there is a way to let an user when he clicks on a button "select file or folder", letting him choose a file or a folder as he wishes.
I know there is way to make a select file or a select folder, I would like to do it in one way where the user either choose a file, either choose a folder, and then in my code, I get either the file or the list of files of the folder.
Thanks in advance for your help
It kind of sucks, but I think you will need to make your own OpenFileDialogue and use the Directory class.
using System.IO;
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
string[] dirPaths = Directory.GetDirectories(#"c:\MyDir\");
The directory class has some useful things that will make this process a little easier.
create a class FolderPicker then code it like this >>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace AutoRunGenerator
{
class FolderPicker
{
public static string FileName = "";
public FolderPicker()
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr= ofd.ShowDialog();
string filename = ofd.FileName;
FileName = filename;
}
public string GetFileName()
{
return FileName;
}
}
}
then
call it from your event method or from anywhere like this >>
FolderPicker fp = new FolderPicker();
txtBox.Text= fp.GetFileName();
i hope this will help for file dialogue box

How to Access members of WPF App & ClassLibrary and vice versa

I am into this wpf from last 2 weeks. I am currently developing a wpf application based on MVVM pattern. I have 2 projects inside my Solution in Visual C# 2010. One is a WPF application(lets say MSPBoardControl) and other is a Class Library(lets say ConnectViewComponent). Thus both the MSPBoardControl and ConnectViewComponent have the view, viewmodel, model classes respectively.
I have added the reference of ConnectViewComponent in my MSPBoardControl and I am able to access the member variables of ConnectViewComponent in my MSPBoardControl's View,Viewmodel and model class. My concern is how to access the member variables of MSPBoardControl from my ConnectViewComponent.
ViewModel of MSPBoardControl:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ConnectViewComponent.Model;
using System.Windows.Input;
using ConnectViewComponent.Commands;
[[[using MSPBoardControl.ViewModel;]]]
namespace ConnectViewComponent.ViewModel
{
public class ConnectViewModel : INotifyPropertyChanged
{
public List<ConnectModel> m_BoardNames;
[[[BoardControlViewModel mBoardVM;]]]
public ConnectViewModel()
{
m_BoardNames = new List<ConnectModel>()
{
new ConnectModel() {Name = "Bavaria", Connection_Status = "Disconnected"},
new ConnectModel() {Name = "Redhook", Connection_Status = "Disconnected"},
};
}
public List<ConnectModel> BoardNames
{
//get set
}
private ConnectModel m_SelectedBoardItem;
public ConnectModel SelectedBoard
{
//get set
}
private ICommand mUpdater;
public ICommand ConnectCommand
{
get
{
if (mUpdater == null)
mUpdater = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));
return mUpdater;
}
set
{
mUpdater = value;
}
}
public bool SaveCanExecute()
{
return true;
}
public void SaveExecuted()
{
if (SelectedBoard.Connection_Status == "Disconnected" && SelectedBoard.Name == "Bavaria")
{
SelectedBoard.Connection_Status = "Connected";
}
else if (SelectedBoard.Connection_Status == "Disconnected" && SelectedBoard.Name == "Redhook")
{
SelectedBoard.Connection_Status = "Connected";
}
}
}
}
[[[ -- ]]] in my code denotes I am not able to access the members of BoardControlViewModel as well as USING Namespace.ViewModel too.
I cannot add the reference of BoardControl in my ConnectComponent project since it will lead to circular dependency. How can I access it? Please Help!!
Having circular dependencies in your project can be a "code smell". There are various ways to remove this "smell". For simplicity lets say that you have project A and project B that has a circular dependency.
Factor out the common types used by both projects and move them into a new project C. Let A and B reference C. This should remove either the dependency from A to B or the opposite dependency or even both dependencies.
If A and B has types that need to interact you need to decouple this interaction into a common set of abstractions (e.g. interfaces or abstract base classes). You should then move these types without any implementation into project C that both A and B references. This will allow the types in A and B to interact but only using the definitions in C. An example could be that A is the main application. It calls in interface IService defined in C but implemented in B and registers a callback IServiceCallback defined in C via IService. B can then call back into A using IServiceCallback without knowing that the implementation is in A.
If the types in A and B are strongly coupled you should merge A and B into a single project.
You can add a some type of "Common" library (project) that will contain those general classes. So both BoardControl and ConnectComponent can reference it.
Also you can check similar question.

Silverlight 4 Domainservice problem

Why the methods of DomainService which have parameters of complex types (all types except int, bool, float,...) are not recognizable in Model?
in build and even in intelisense! :(
Update
CODE
namespace KhorasanMIS.Web.Services
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using KhorasanMIS.Web.Entities;
// Implements application logic using the KhorasanMISEntities context.
// TODO: Add your application logic to these methods or in additional methods.
// TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
// Also consider adding roles to restrict access as appropriate.
// [RequiresAuthentication]
[EnableClientAccess()]
public class DSCountry : LinqToEntitiesDomainService<KhorasanMISEntities>
{
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'Countries' query.
public IQueryable<Country> GetCountries()
{
return this.ObjectContext.Countries;
}
public IEnumerable<Country> GetCountryByCode(string pCode)
{
return this.ObjectContext.Countries.Where(a => a.ISOCode.Equals(pCode));
}
public bool IsValidEdit(string pCode)
{
List<string> message = new List<string>();
IEnumerable<Country> list = GetCountryByCode(pCode);
if (list.Count() > 0)
{
return false;
}
return true;
}
//***********************************************************
public bool Update(Country currentItem)
{
this.ObjectContext.Countries.AttachAsModified(currentItem, this.ChangeSet.GetOriginal(currentItem));
return true;
}
//***********************************************************
}
}
The method in the stars-comment can not be used in a model but if I change its parameter to int, it will become usable.
Check out the documentation of DomainServices. Insert, update and delete operation are handled by the EntitySet and the SubmitChanges method on the client side.
When you expose a domain service, an EntitySet object is generated in the domain context with properties that indicate which operations (insert, update, or delete) are permitted from the client. You execute data modifications by modifying the entity collection and then calling the SubmitChanges method.

Use SmallDateType in Model-First Entity Framework

I can't help but feel I am missing something but to this day I cannot find the answer.
I am doing a model-first entity framework and have a few properties set as DateTime. These translate to DateTime in the database - but I would like to use SmallDateTime. In my case, getting down to seconds and milliseconds just isn't worth the double storage for as many rows as I will have.
Does anyone know a way in the model-first environment to map DateTime to a SmallDateTime DB field? As a last hope, I can generate the DDL, replace all, and update the Model from the database after - but I feel that is obnoxious!
Thanks in advance.
It's a kind of tricky thing.
First of all read this article Model first if you have not yet.
Then create separate class library project with custom IGenerateActivityOutput implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Entity.Design.DatabaseGeneration.OutputGenerators;
using System.Activities;
namespace MyCSDLToSSDL
{
public class MyCsdlToSsdl: IGenerateActivityOutput
{
private CsdlToSsdl _generator;
public MyCsdlToSsdl()
{
_generator = new CsdlToSsdl();
}
public T GenerateActivityOutput<T>(OutputGeneratorActivity owningActivity, NativeActivityContext context, IDictionary<string, object> inputs) where T : class
{
var str = _generator.GenerateActivityOutput<T>(owningActivity, context, inputs) as string;
return str.Replace("Type=\"datetime\"", "Type=\"smalldatetime\"") as T;
}
}
}
The next step is to modify database generation workflow.
Locate TablePerTypeStrategy.xaml in your file system.
Copy this file to the same folder with different name. TablePerTypeStrategy_smalldatetime.xaml for example. Open it with VS.
Change OutputGeneratorType of CsdlToSsdlAndMslActivity to "MyCSDLToSSDL.MyCsdlToSsdl, MyCSDLToSSDL". Double quotes are required.
Change database generation workflow property to "TablePerTypeStrategy_smalldatetime.xaml (VS)".
Try to generate database from model.
Looks very much like a workaround but it works. :) Hope it helps!

Resources