assignment

Why are assignment operators (=) invalid in a foreach loop?

Why are assignment operators (=) invalid in a foreach loop? I'm using C#, but I would assume that the argument is the same for other languages that support foreach (e.g. PHP). For example, if I do something like this: string[] sArray = new string[5]; foreach (string item in sArray) { item = "Some assignment.\r\n"; } I get an err...

assignin sockaddr to another changes the addr?!?

While trying the following the address in the second sockaddr changes: /*Stuff*/ sockaddr add1, add2; recvfrom(/*socket*/, /*buffer*/, /*count*/, /*flag*/, &add1, /*fromlen*/); add2 = add1; //The sa_data - part changes O_o... /*Stuff*/ Anyone knows why?... EDIT: 1.I changed the sockaddr to sockaddr_storage which definetly has enou...

copying sockaddr_storage to another sockaddr_storage changes address

Hey... As in a recent question (nobody did react on the last changes) I have a problem with assigning a sockaddr structure filled by recvfrom. As I have been advised , I did change my sockaddr to sockaddr_storage and casted it in the last moment to be sure of having enough space for the address... But the problem of sockaddr_storage ...

Guarantying assignment to a function's return value in OCaml

Coming to OCaml from Lisp, I find myself very confused about when functions return and when they don't. I miss my magic Quote! Thankfully, most of the time, OCaml appears to automagicly know when I want a function evaluated and when I don't. However, I frequently find myself trying to assign the return value of a function in a let exp...

Is there a cleaner way to add "else if" to assignment conditional in Awk, etc.?

Certain languages like awk script allow for conditional assignments. For example, say you had a list file in the format: <item name, no spaces> <price as float> e.g. Grape 4.99 JuicyFruitGum 0.45 Candles 5.99 And you wanted to tax everything over $1... you could use the awk script: awk '{a=($2>1.00)?$2*1.06:$2; print a}' prices.d...

Assigning and Conditional Testing Simultaneously in C++

I have three functions that return integer error codes, e.g. int my_function_1(const int my_int_param); int my_function_2(const int my_int_param); int my_function_3(const int my_int_param); I want to assign and test for error at the same time for brevity. Will the following work and be portable? int error=0; ... if ( error || (...

Assign derived class to base class

Is it safe to do the following or is it undefined behaviour: class Base { private: int a; }; class Derived : public Base { private: int b; }; Base x; Derived y; x = y; // safe? Do the extra bits in derived classes just get sliced off? ...

C# assignment question

In the following snippet: long frameRate = (long)(_frameCounter / this._stopwatch.Elapsed.TotalSeconds); Why is there an additional (long)(...) to the right of the assignment operator? ...

How can I use assign to change variables within a dataframe in R?

I tried to do something like this: x <- data.frame(1:20) attach(x) assign("x2",1:20,pos="x") However, x$x2 gives me NULL. With x2 I get what I want but it is not part of the data.frame. Attaching x2 to x manually would work in this simple case but not in the more complex one I need. I try to assign in a loop where I loop over...

Shortest way to assign a default value to a variable?

I have this right now to use a cookie value if exists otherwise use a default value: $default_carat_min = "0.25"; if($_COOKIE["diamond-search_caratMin"]) { $default_carat_min = $_COOKIE["diamond-search_caratMin"]; } I am going to have to do this with a lot of variables, and its going to get really cluttered/ugly. So I am trying to...

How can I assign a child class to a base class?

I know there are solved questions related to this issue, but I still can't figure out how to resolve my problem. I have something like this: class Base { static Base* createBase() { Base *b = new Base(); ... //does a lot of weird things return b; } } class Child : public Base { static Child* createChild()...

c string basics, why unassigned?

Hi, I am trying to learn the basics, I would think that declaring a char[] and assigning a string to it would work. thanks int size = 100; char str[size]; str = "\x80\xbb\x00\xcd"; gives error "incompatible types in assignment". what's wrong? thanks ...

Why does this go into an infinite loop?

Hi, I'm a teacher, and yesterday a student wrote the following code: public class Tests { public static void main(String[] args) throws Exception { int x = 0; while(x<3) { x = x++; System.out.println(x); } } } we know he should have writen just x++ or x=x+1, but on x = x++; it s...

Why Does C++ Support Hex Assignment, But Lack Binary Assignment? How best to store flags?

I have a set of bit flags that are used in a program I am porting from C to C++. To begin... The flags in my program were previously defined as: /* Define feature flags for this DCD file */ #define DCD_IS_CHARMM 0x01 #define DCD_HAS_4DIMS 0x02 #define DCD_HAS_EXTRA_BLOCK 0x04 ...Now I've gather that #defines for constant...

VB.NET Using a custom timer in a lambda expression in its own "New" statement - is it okay?

This works, and I can't imagine how it might cause problems, but visual studio gives me an warning and that makes me sad. I'm just wondering if doing something like this might ever cause problems: I have a custom timer that acts like a Wait for some number of milliseconds and then execute a function. It looks like this: Public Class ...

Ruby: OptionParser: String Arg & Hash Assignment

Using OptionParser for string argument input and hash assignment. What is the best way to read-in multiple variables for a single argument? How do I then assign those to a hash to reference? Here is what I have so far: large_skus = Hash.new small_skus = Hash.new OptionParser.new do |opts| opts.on("-b", "--brands bName1,bName2,bName...

Freemarker assign list length to local variable

The following freemarker code causes an exception <#assign i= it.getList().size()> <#list it.getList() as elem> <#if i==1> <li>${elem.name}</li> <#else> <li class="marked">${elem.name}</li> </#if> <#assign i = i-1> </#list> The following exception is thrown: Expected hash. it.getList() evaluated instead to ...

Convert javascript array handling to jQuery

Hi everyone I'm doing a javascript assignment and have just learned that I can do it in jQuery if I wish, rather than vanilla javascript. I thought I'd give it a go to see what it's like. This is the contents of my javascript function: rowsArray = $("#Table1 tr"); for (i=0;i<rowsArray.length;i++){ numSeatsInRow = rowsArray[i]...

C++ unrestricted union workaround

#include <stdio.h> struct B { int x,y; }; struct A : public B { // This whines about "copy assignment operator not allowed in union" //A& operator =(const A& a) { printf("A=A should do the exact same thing as A=B\n"); } A& operator =(const B& b) { printf("A = B\n"); } }; union U { A a; B b; }; int main(int argc, c...

problem updating data in a datagridview through a query update in access database 2007?

Hello Other time I need your help, I am developing an app in C# using an Access database(2007), the problem I think is the query updtate, I have searched over internet but nothing works, I have a datagridview and It has 3 columns the user needs to puts data to the last 2 columns(5 rows), I already do it but when I fill the columns in ...