How to maintain variable throughout the application in c#? Multiple users access same variable with different values? - c#-4.0

string static str="User1";
Above in str variable we are storing User1 values.but another user store User2 value in same variable .is it possible are not.

Use Singleton class
http://csharpindepth.com/Articles/General/Singleton.aspx
http://www.c-sharpcorner.com/UploadFile/8911c4/singleton-design-pattern-in-C-Sharp/
singleTon classes are C# classes which instences can be only 1 in the application runtime so all you need is to save the str as prop of your singleton class include the class in whichever place you want in application
P.s microsoft link
https://msdn.microsoft.com/en-us/library/ff650316.aspx

Related

How do I use get task for mailboxes in systemverilog?

I want to use get task of a mailbox, the mailbox contains data of a class Transaction type. While accessing do I need to make an object of variable tr or just a handle is sufficient to access the data received from mailbox?
mailbox #(transaction) gentodriver;
transaction tr;
gentodriver.get(tr);
Would this work or do I need to create an object of tr using
tr=new();
Your question is not specific to mailboxes. SystemVerilog uses handles to class objects, you never refer to class objects directly. Class handles are stored in class variables.
When you use the mailbox::put() and get() methods, you are making assignments from or to the arguments of those methods. You are putting a handle to the object into the mailbox, and they getting the handle. You are not copying the object, just copying the handle to it into another class variable.

How To Call a String From A Different Method Than It Was Originally Declared

I'm trying to write an Oregon Trail type story in java. In one of the methods later on, you are asked to input your name. I've used this to get the name:
Scanner keys = new Scanner(System.in);
String name = keys.nextLine();
I would like to keep referring to the player as the name they entered in other methods and I'm unsure on how to call it. Any help is appreciated.
When you declare
String name = keys.nextLine();
You are creating a string inside the scope of that method. As you probably noticed, it's no longer accessible once the method finishes. Rather than storing the character name in a variable local to that method, you want to save it to a variable in an outside scope.
In Java's object oriented design, the ideal place to put that would be an instance variable for the relevant class. Say you have some master class called "Game". An instance of this class will represent a running game, have methods for interacting with the game, and hold data about the game. You could have an instance variable in Game declared as:
String playerName;
If that method is within Game, then you would simply have the code:
Scanner keys = new Scanner(System.in);
this.playerName = keys.nextLine();
Since you're assigning the name to a variable that exists outside the scope of the method, it will remain accessible to you later. The exact approach to this depends on how you structured your classes.
A more general solution, which could work better than the above solution depending on your code structure, would be to have that method return a String, rather than set one. For instance:
String getPlayerName() {
Scanner keys = new Scanner(System.in);
return keys.nextLine();
}
A method like that would return a string holding the name, which would allow you to work with it outside of the method.

Use MEF to allow only 2 instances at max per application

I am using MEF as an IOC in my application. I found myself stuck in a situation where I need exactly two instance at a time for a class in my application (across all threads). I thought it would be easy just by adding export attribute twice with different container name and then using that container name to create two instances.
[Export("Condition-1",typeof(MyClass)]
[Export("Condition-2",typeof(MyClass)]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
public class MyClass { }
And then export them as
Container.GetExport<MyClass>("Condition-1").Value
Container.GetExport<MyClass>("Condition-2").Value
But this trick did not work. I finally able to solve my problem by using CompsositionBatch
cb.AddExportedValue<MyClass>("Condition-1",new MyClass());
cb.AddExportedValue<MyClass>("Condition-2",new MyClass());
But my question is, Why am I not able to get different instances on the basis of Contract Name. Is it right that Contract name does not matter if CreationPolicy is shared?
The problem is in the setup of PartCreationPolicyAttribute that decorates MyClass.
CreationPolicy.Shared means that a single instance will be returned for each call to Container.GetExport. It is like a singleton. What you need in your case is the CreationPolicy.NonShared policy which will return a different instance for each clla to Container.GetExport.
Here's a good article on Part Creation Policy.
Also have a look at ExportFactory in MEF 2 for MEF2 additions regarding the lifetime and sharing of parts

How to auto-generate early bound properties for Entity specific (ie Local) Option Set text values?

After spending a year working with the Microsoft.Xrm.Sdk namespace, I just discovered yesterday the Entity.FormattedValues property contains the text value for Entity specific (ie Local) Option Set texts.
The reason I didn't discover it before, is there is no early bound method of getting the value. i.e. entity.new_myOptionSet is of type OptionSetValue which only contains the int value. You have to call entity.FormattedValues["new_myoptionset"] to get the string text value of the OptionSetValue.
Therefore, I'd like to get the crmsrvcutil to auto-generate a text property for local option sets. i.e. Along with Entity.new_myOptionSet being generated as it currently does, Entity.new_myOptionSetText would be generated as well.
I've looked into the Microsoft.Crm.Services.Utility.ICodeGenerationService, but that looks like it is mostly for specifying what CodeGenerationType something should be...
Is there a way supported way using CrmServiceUtil to add these properties, or am I better off writing a custom app that I can run that can generate these properties as a partial class to the auto-generated ones?
Edit - Example of the code that I would like to be generated
Currently, whenever I need to access the text value of a OptionSetValue, I use this code:
var textValue = OptionSetCache.GetText(service, entity, e => e.New_MyOptionSet);
The option set cache will use the entity.LogicalName, and the property expression to determine the name of the option set that I'm asking for. It will then query the SDK using the RetrieveAttriubteRequest, to get a list of the option set int and text values, which it then caches so it doesn't have to hit CRM again. It then looks up the int value of the New_MyOptionSet of the entity and cross references it with the cached list, to get the text value of the OptionSet.
Instead of doing all of that, I can just do this (assuming that the entity has been retrieved from the server, and not just populated client side):
var textValue = entity.FormattedValues["new_myoptionset"];
but the "new_myoptionset" is no longer early bound. I would like the early bound entity classes that gets generated to also generate an extra "Text" property for OptionSetValue properties that calls the above line, so my entity would have this added to it:
public string New_MyOptionSetText {
return this.GetFormattedAttributeValue("new_myoptionset"); // this is a protected method on the Entity class itself...
}
Could you utilize the CrmServiceUtil extension that will generate enums for your OptionSets and then add your new_myOptionSetText property to a partial class that compares the int value to the enums and returns the enum string
Again, I think specifically for this case, getting CrmSvcUtil.exe to generate the code you want is a great idea, but more generally, you can access the property name via reflection using an approach similar to the accepted answer # workarounds for nameof() operator in C#: typesafe databinding.
var textValue = entity.FormattedValues["new_myoptionset"];
// becomes
var textValue = entity.FormattedValues
[
// renamed the class from Nameof to NameOf
NameOf(Xrm.MyEntity).Property(x => x.new_MyOptionSet).ToLower()
];
The latest version of the CRM Early Bound Generator includes a Fields struct that that contains the field names. This allows accessing the FormattedValues to be as simple as this:
var textValue = entity.FormattedValues[MyEntity.Fields.new_MyOptionSet];
You could create a new property via an interface for the CrmSvcUtil, but that's a lot of work for a fairly simple call, and I don't think it justifies creating additional properties.

groovy domain objects in Db4O database

I'm using db4o with groovy (actually griffon). I'm saving dozen of objects into db4o objectSet and see that .yarv file size is about 11Mb. I've checked its content and found that it stores metaClass with all nested fields into every object. It's a waste of space.
Looking for the way to avoid storing of metaClass and therefore reduce the size of result .yarv file, since I'm going to use db4o to store millions of entities.
Should I try callConstructors(true) db4o configuration? Think it would help?
Any help would be highly appreciated.
As an alternative you can just store 'Groovy'-beans instances. Those are compiled down to regular Java-ish classes with no special Groovy specific code attached to them.
Just like this:
class Customer {
// properties
Integer id
String name
Address address
}
class Address{
String street;
}
def customer = new Customer(id:1, name:"Gromit", address:new Address(street:"Fun"))
I don't know groovy but based on your description every groovy object carries metadata and you want to skip storing these objects.
If that is the case installing a "null translator" (TNull class) will cause the "translated" objects to not be stored.
PS: Call Constructor configuration has no effect on what gets stored in the db; it only affects how objects are instantiated when reading from db.
Hope this helps

Resources