Initialize Two Variables on Same Line

Initializing multiple variables to the same value in Java

String one, two, three;
one = two = three = "";

This should work with immutable objects. It doesn't make any sense for mutable objects for example:

Person firstPerson, secondPerson, thirdPerson;
firstPerson = secondPerson = thirdPerson = new Person();

All the variables would be pointing to the same instance. Probably what you would need in that case is:

Person firstPerson = new Person();
Person secondPerson = new Person();
Person thirdPerson = new Person();

Or better yet use an array or a Collection.

Initializing multiple variables on the same line

You may use destructing declaration for this (works for up to 5 variables declaration, if you need more see https://stackoverflow.com/a/55891181/13968673):

inline fun <reified T> nulls() = List<T?>(5) { null }

var (etxtUserName, etxtContact, etxtPassword) = nulls<EditText?>()

How to initialize multiple variables of some type in one line

Two options in Swift: commas or tuples.

With commas separated statements:

var a = MyType(), b = MyType(), c = MyType()

With a tuple statement:

var (a, b, c) = (MyType(), MyType(), MyType())

Also, but discouraged, you can make multiple statements on one line using semi-colons:

var a = MyType(); var b = MyType(); var c = MyType()

C++ declaring multiple variables in the same line

The compiler will issue an error for such declarations

int a, b, c = 10, 15, 20; 

The only idea that comes to my head is the following :)

int a, b, c = ( a = 10, b = 15, 20 ); 

Or you could make these names data members of a structure

struct { int a, b, c; } s = { 10, 20, 30 };

EDIT: Is it possible with the overloading operator =?

There is not used the copy asssignment operator. It is a declaration. The copy assignment operator is used with objects that are already defined.:)

How to define multiple variables in single statement

This is not possible, but you also don't need to call parseFile twice.

Write your code like this:

int [] temp = parseFile(file);
start = temp[0];
stop = temp[1];

Python (I believe) supports multiple return values. Java obeys C conventions, and so doesn't permit it. Since that isn't part of the language, the syntax for it isn't either, meaning slightly gross hacks like the temp array are needed if you're doing multiple returns.

Declaring multiple variables on one line out of order

No, the first code snippet only initializes pos with -1 and leaves ch uninitialized. The second one does it the other way round leaving pos uninitialized and ch with the value -1. But in either case, both ch and pos will be created and you will be able to set or update their values.

How to Set multiple Variables value in one Line in C#?

The best I know is this.

int currentYearSavingDays, currentYearSavingXMins, currentYearSavingNXMins, currentYearUsedDays, currentYearUsedXMins, yearLimitRemainingDays, yearLimitRemainingXMins, totalYearlyDays, totalYearlyXMins, totalSavingDays, totalSavingMins, totalUsableDays, totalUsableMins, nextYearTotalUsableDays, nextYearTotalUsableMins, dayWorkMinutes, previousYearRemainingDays, previousYearRemainingMins;
currentYearSavingDays = currentYearSavingXMins = currentYearSavingNXMins = currentYearUsedDays = currentYearUsedXMins = yearLimitRemainingDays = yearLimitRemainingXMins = totalYearlyDays = totalYearlyXMins = totalSavingDays = totalSavingMins = totalUsableDays = totalUsableMins = nextYearTotalUsableDays = nextYearTotalUsableMins = dayWorkMinutes = previousYearRemainingDays = previousYearRemainingMins = 0;


Related Topics



Leave a reply



Submit