static-classes

python nested dictionary comprehension in static class scope: variable not defined [duplicate]

断了今生、忘了曾经 提交于 2020-01-25 01:03:44
问题 This question already has answers here : Accessing class variables from a list comprehension in the class definition (5 answers) Closed 3 days ago . I've got this code: class Chars: char_to_number = { 'a': ['1'], 'b': ['2'], 'c': ['3', '6'], 'd': ['4', '5'], } number_to_char = { number: char for char in char_to_number for number in char_to_number[char] } Now this code returns an error as such: name 'char_to_number' is not defined Now it looks like python could not parse the nested dictionary

Variables in a Javascript module visible outside of it?

北慕城南 提交于 2020-01-15 04:55:21
问题 To start, I'm coming from a .NET world, where there are static classes (C#, also known as modules in VB) and instance classes - those you can instantiate. This question is about Javascript, where I'm trying to recreate the pattern I already know and make a module / static class. Here is the code: var MyModule = { variable1: 0, variable2: 1, method1: function() { //code goes here }, method2: function() { //mode code goes here }, method3: function() { //and some more just to prove the pattern }

Using an inner class name and an object name same in Java

◇◆丶佛笑我妖孽 提交于 2020-01-01 04:24:06
问题 In the following code snippet, presumably it appears that it should issue some compilation error but it doesn't: class Outer { public static class Inner { static String obj = "Inner"; } static Optional Inner = new Optional(); //The (inner) class name and the object name are same. } class Optional { String obj = "Optional"; } public class Main { public static void main(String[] args) { System.out.println(Outer.Inner.obj); //Refers to the string inside the optional class } } The class Outer has

How to access all specific versions of a generic static class in C#?

≯℡__Kan透↙ 提交于 2019-12-25 05:26:28
问题 Let's say I have the following static class: public static class Foo<T> { private static T Baz; /* other members */ public static void Bar() { //do something with Baz } } Is there a built-in way to call Bar() for all specific versions of the generic class without knowing the type parameters that were used? That is, if the consumer of Foo<T> has used it as Foo<Larry> , Foo<Curly> and Foo<Moe> , how can I call all the Foo<Larry>.Bar() , Foo<Curly>.Bar() etc. methods automatically without

Why are my static objects not being instantiated when first access to the static class is a static method on the base class?

烂漫一生 提交于 2019-12-24 04:05:34
问题 I have the following class: public class DocketType : Enumeration<DocketType, int, string> { public static DocketType ChangeOver = new DocketType(1, "Changeover"); public static DocketType Withdrawal = new DocketType(2, "Withdrawal"); public static DocketType Installation = new DocketType(3, "Installation"); private DocketType(int docketTypeId, string description) : base(docketTypeId, description) { } } With the following base class: public abstract class Enumeration<TEnum, X, Y> :

Is there a built in type in C# that contains a bool and a string for static method return?

泪湿孤枕 提交于 2019-12-23 21:32:15
问题 I'm looking for a type to return from a function or method, to convey first, whether it was successful or not, and then attache a message (as a string) to it to pass further information about why it failed. It might be something on the lines of an Application Exit Code; however, I don't to create a whole set of exit codes to index a particular error message. I do like the idea of a single "success" exit code so one can quickly check whether the call failed or not and work accordingly. I

How can I run a static initializer method in C# before the Main() method?

Deadly 提交于 2019-12-18 07:30:21
问题 Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is run before Main() ? The best I can think of is to add this to Foo : private class Initializer { private static bool isDone = false; public Initializer() { if (!isDone) { init(); isDone = true; } } } private static readonly Initializer initializer = new Initializer(); Will this work or are there some

Why does Android prefer static classes

拥有回忆 提交于 2019-12-17 17:28:27
问题 I see a lot of java code where android prefers to have developers use static inner classes. Particularly for patterns like the ViewHolder Pattern in custom ListAdapters. I'm not sure what the differences are between static and non-static classes. I've read about it but it doesn't seem to make sense when concerned with performance or memory-footprint. 回答1: It's not just Android developers... A non-static inner class always keeps an implicit reference to the enclosing object. If you don't need

The name 'temp' does not exist in the current context (C# desktop application)

倖福魔咒の 提交于 2019-12-13 16:15:49
问题 I am making a C# desktop application with following code: static class ClassA { public static string Process() { string temp = Functions.Test(); return temp; } } static class Functions { public static string Test() { return "ok"; } } Problem is the variable "temp" doesn't get any value from Test() funciton. When I try to check its value in Immediate Window, I get the message "The name 'temp' does not exist in the current context" Both ClassA and Functions are in separate class files but

Is it a good practice to use static classes to making the UI elements accessible from all the classes in .NET?

别来无恙 提交于 2019-12-13 15:23:02
问题 Please let me know which of the following is a good programming practise: 1. Using a static class and then using a reference to it from the class MainWindow constructor as shown: public partial class Mainwindow : Window { public MainWindow() { InitializeComponent(); UI.window = this; } private void button1_Click(object sender, RoutedEventArgs e) { Shutdownads attempt1 = new Shutdownads(); } } static class UI { public static MainWindow window; } /*and then refering to the wpf elements from