instance

Why is the System.Random class not static?

流过昼夜 提交于 2019-11-27 05:14:33
When you use the System.Random class, you must make an instance of it. Why is it not static ? Because if I want a random number between 0 and 9, I can use the static method , System.Random.Next(int, int) : int ourRandomNumber = Random.Next(0,9); So why isn't the class just static ? BrokenGlass You wouldn't be able to use different seeds if it were static - the Random instance keeps track of that state. By default Random uses the current time as seed, but re-using a particular seed (i.e. new Random(42) ) allows you to exactly repeat the sequence of random numbers - they will always be the same

Static Variable Instances and AppDomains, what is happening?

♀尐吖头ヾ 提交于 2019-11-27 04:25:11
问题 I have public static class A { public static string ConnString; } [Serializable] public class Test{ // Accesing A's field; public string ConnString{get{return A.ConnString;}set{A.ConnString=value;}} } void Main() { A.ConnString = "InitialString"; // I set A.ConnString in the current domain var newDomain = AppDomain.CreateDomain("DomNew"); Test TObj = newDomain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, typeof(Test).FullName) as Test ; TObj.ConnString = "NewDomainString"; // It is

Ruby on Rails - Access controller variable from model

半腔热情 提交于 2019-11-27 04:24:37
I am trying to access an instance variable which is set in the controller in the model. The controller is the products controller and the model is the products model. The instance variable is a instance of another model called account. The instance variable is @current_account When I run the code nothing happens, I do not get an error. Does anyone know where I can find something read about access instance variables set in the controller from the model? Thanks Eef You shouldn't generally try to access the controller from the model for high-minded issues I won't go into. I solved a similar

How to remove(unregister) registered instance from Unity mapping?

倖福魔咒の 提交于 2019-11-27 04:17:34
问题 I meet one problem that i can't solve now. I have the following: UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance); where UnityHelper.DefaultContainer is my helper for getting unity container with loaded configuration. here I registered instance as an instance of IMyInterface . So anywhere( some time after using) I want to remove this mapping. Remove it at all. How I can do it? I have tried: UnityHelper.DefaultContainer.Teardown(instance) but is was

An enclosing instance that contains <my reference> is required

核能气质少年 提交于 2019-11-27 04:01:41
An enclosing instance that contains is required Below is the code. positionObj is the object that I am trying to use and it is giving me the above error. It's unclear why. package toolBox; import toolBox.Secretary.positionObj; public class PositionManagement { public static HashMap<String, Secretary.positionObj> main(String vArg){ positionObj newPosition=new positionObj(); } } You're trying to use the non-static inner positionObj class without an instance of Secretary for it to belong to. A non-static inner class must belong to an instance of its parent class You should probably change

Dynamically invoking any function by passing function name as string

笑着哭i 提交于 2019-11-27 03:58:56
问题 How do I automate the process of getting an instance created and its function executed dynamically? Thanks Edit: Need an option to pass parameters too. Thanks 回答1: Do you just want to call a parameterless constructor to create the instance? Is the type specified as a string as well, or can you make it a generic method? For example: // All error checking omitted. In particular, check the results // of Type.GetType, and make sure you call it with a fully qualified // type name, including the

Easiest way to copy/clone a mongoose document instance?

醉酒当歌 提交于 2019-11-27 03:19:24
问题 My approach would be to get the document instance, and create a new one from the instance fields. I am sure there is a better way to do it. 回答1: Can you clarify what you mean by "copy/clone"? Are you going trying to create a duplicate document in the database? Or are you just trying to have two var s in your program that have duplicate data? If you just do: Model.findById(yourid).exec( function(err, doc) { var x = doc; Model.findById(yourid).exec( function(err, doc2) { var y = doc2; // right

Java Reflection: get instances of a given class found by entering its name?

白昼怎懂夜的黑 提交于 2019-11-27 03:13:33
问题 Is it possible to get all instances of a class by entering this class's name as a string? Something like this? var instances = Reflection.findClass("com.someone.MyClass").getInstances(); Any feedback is appreciated. Thanks. 回答1: No, there's nothing like that available. If you hook into the debugging API you may be able to do it, but not when running "normally". 回答2: I don't know of a way of doing this at runtime, but, if you are happy doing it 'offline', you can do the following: Take a heap

Getting all instances of a class [duplicate]

丶灬走出姿态 提交于 2019-11-27 02:40:31
问题 Possible Duplicate: Is there a simple way of obtaining all object instances of a specific class in Java In java, is there any possible way to get all the instances of a certain class? 回答1: You can use a Factory static initializer when you instantiate your class (Singleton pattern) and then add each generated instance in the factory constructor to a List ... Something like this : class MyObject { private static List instances = new ArrayList(); public static MyObject createMyObject() {

java StackOverflowError when local and instance objects creation

女生的网名这么多〃 提交于 2019-11-27 02:18:49
Hi can anybody please explain me why is this code snippet giving me StackOverflowError I really appreciate if you can explain what is happening when instanceObj initializing and calling ObjectTest constructor and java.lang.Object constructor. It seems to me ObjectTest constructor loop over and over.But I don't know exact reason? So any suggestion... public class ObjectTest { public ObjectTest() { } ObjectTest instanceObj = new ObjectTest(); public static void main(String[] args) { ObjectTest localObj = new ObjectTest(); } } Colin Hebert Let's see what will be executed : main() create a new