default-value

Why can't an out parameter have a default value?

懵懂的女人 提交于 2019-11-28 03:11:28
问题 Currently when trying to do something in a method that takes an out parameter, I need to assign the value of the out parameter in the method body, e.g. public static void TryDoSomething(int value, out bool itWorkerd) { itWorkerd = true; if (someFavourableCondition) { // if I didn't assign itWorked variable before this point, // I get an error: "Parameter itWorked must be assigned upon exit. return; } // try to do thing itWorkerd = // success of attempt to do thing } I'd like to be able to set

Default member values best practice

醉酒当歌 提交于 2019-11-28 03:08:06
Is it good practice when writing C++11 code to set default values for class members in the header file of the class? Or is it better to do this in the constructor of the class? EDIT: I mean: foo.h : #include <string> using std::string; class Foo{ private: string greet = "hello"; public: Foo(); }; VS foo.cpp (of course with the necessary header file, but without the in-class initialization): Foo::Foo(){ greet = "hello"; } Which one is better and why? If a class member is always initialized with the same initial value, then you should make the initializer inline, so as to avoid duplication. If

Bind a column default value to a function in SQL 2005

♀尐吖头ヾ 提交于 2019-11-28 00:49:16
I have a column containing items that can be sorted by the user: DOC_ID DOC_Order DOC_Name 1 1 aaa 2 3 bbb 3 2 ccc I'm trying to figure out a way to properly initialize DOC_Order when the entry is created. A good value would either be the corresponding DO-CID (since it is autoassigned), or MAX(DOC-ORDER) + 1 After a bit of googling I saw it was possible to assign a scalar function's return to the default column. CREATE FUNCTION [dbo].[NEWDOC_Order] ( ) RETURNS int AS BEGIN RETURN (SELECT MAX(DOC_ORDER) + 1 FROM DOC_Documents) END But each of my tries using MS SQL Management studio ended in a

Preparing a MySQL INSERT/UPDATE statement with DEFAULT values

▼魔方 西西 提交于 2019-11-27 23:34:38
Quoting MySQL INSERT manual - same goes for UPDATE: Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list. So in short if I write INSERT INTO table1 (column1,column2) values ('value1',DEFAULT); A new row with column2 set as its default value -

In Python, can I specify a function argument's default in terms of other arguments?

Deadly 提交于 2019-11-27 21:53:18
Suppose I have a python function that takes two arguments, but I want the second arg to be optional, with the default being whatever was passed as the first argument. So, I want to do something like this: def myfunc(arg1, arg2=arg1): print (arg1, arg2) Except that doesn't work. The only workaround I can think of is this: def myfunc(arg1, arg2=None): if arg2 is None: arg2 = arg1 print (arg1, arg2) Is there a better way to do this? marcog As @Ignacio says, you can't do this. In your latter example, you might have a situation where None is a valid value for arg2 . If this is the case, you can use

Android CheckBoxPreference Default Value

假如想象 提交于 2019-11-27 20:37:27
I have the following XML code for my CheckBoxPreference : <CheckBoxPreference android:key="pref_boot_startup" android:title="Auto start" android:defaultValue="true" /> But when I retrieve the preference in code the value is false . sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); boolean autoStart = sharedPreferences.getBoolean("pref_boot_startup", true); My autoStart variable returns false . Is there a specific reason for this? Am I missing a step to set the default value to true ? junkdog You have to set the defaults first: @Override protected void onCreate() {

Why isn't SQLAlchemy default column value available before object is committed?

試著忘記壹切 提交于 2019-11-27 20:27:19
Recently I figured out that SQLAlchemy's Column default doesn't work as I expect it to: >>> Base = declarative_base() >>> class TestModel(Base): ... __tablename__ = 'tmodel' ... id = sa.Column(sa.Integer, primary_key=True) ... foo = sa.Column(sa.Integer, default=0) ... >>> tmodel_instance = TestModel() >>> print tmodel_instance.foo None >>> session.add(tmodel_instance) >>> print tmodel_instance.foo None >>> session.commit() >>> print tmodel_instance.foo 0 I want tmodel_instance.foo to equal 0 right after object instantiation, but it seems that the default value is only used when executing

Display a default DataTemplate in a ContentControl when its content is null or empty?

我的未来我决定 提交于 2019-11-27 20:03:57
问题 I would think this is possible, but the obvious way isn't working. Currently, I'm doing this: <ContentControl Content="{Binding HurfView.EditedPart}"> <ContentControl.Resources> <Style TargetType="ContentControl" x:Key="emptytemplate"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}" Value="{x:Null}"> <Setter Property="ContentControl.Template"> <Setter.Value> <ControlTemplate> <Grid HorizontalAlignment="Stretch" VerticalAlignment=

Best way to give a variable a default value (simulate Perl ||, ||= )

柔情痞子 提交于 2019-11-27 19:49:57
问题 I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You also have $foo ||= $bletch which will only assign $bletch to $foo if $foo is not defined or empty. The ternary operator in this situation is tedious and tiresome. Surely there's a simple, elegant method available in PHP? Or is the only answer a custom function that uses isset()? 回答1: PHP 5.3 has a shorthand ?: operator: $foo = $bar ?: $baz; Which assigns $bar if it's not an

int array initialization

半城伤御伤魂 提交于 2019-11-27 18:34:31
I have here a simple question related to Java. Let's say you have an int array as instance variable: int[] in = new int[5]; So, now by default it contains 5 zeros. But what if you have the same array as local variable. Does it get initialized to zeros? That is not a homework, I am learning Java language. Best regards First thing to understand is that, local varibles are stored on stack which are not initialized explicitly with their default values. While instance variables are stored on Heap , and they are by default initialized with their default value . Also, objects are also created on Heap