Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare and assign multiple string variables at the same time

I'm declaring some strings that are empty, so it won't throw errors later on.

I've read that this was the proper way:

string Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid = Draaiomschrijving = Orderleverdatum = Regeltaakkode = Gebruiksvoorkeur = Regelcamprog = Regeltijd = Orderrelease = ""; 

But that doesn't work. I get this error: Klantnr does not exist in the current context.

What did I do wrong?

like image 965
Mathlight Avatar asked Nov 14 '12 07:11

Mathlight


People also ask

How do I assign multiple values to multiple variables?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

Can you initialize multiple variables at once?

Example - Declaring multiple variables in a statementIf your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

How do you declare multiple strings in C++?

Using + operator The most common approach to concatenate multiple strings in C++ is to use the + operator, which is overloaded for string objects.


2 Answers

You can do it like:

string Camnr, Klantnr, Ordernr, Bonnr, Volgnr;// and so on. Camnr = Klantnr = Ordernr = Bonnr = Volgnr = string.Empty; 

First you have to define the variables and then you can use them.

like image 73
Habib Avatar answered Sep 19 '22 23:09

Habib


You can to do it this way:

string Camnr = "", Klantnr = "", ... // or String.Empty 

Or you could declare them all first and then in the next line use your way.

like image 27
Botz3000 Avatar answered Sep 19 '22 23:09

Botz3000