I have a BindingList I want to update certain items,but in order to use the Foreach available only for the List<> I have to initialize a new List with the BindingList items. like this:
new List<ScanData>(ScanDataList)
.FindAll(i => i.Badge == badge)
.ForEach(x =>x.EmpName = empname);
And that's the simplest way I found to do it, but I don't want to start with the New keyword, is there any other simpler way to Iterate over the BindingList items and update them using a one-liner like the above? (I put it in three lines for readability).
Id like just to remove the New keyword but that just doesn't work,
if a new function helps that is also acceptable, if its generic for any BindingList would be perfect.
Note: Im using compact framework 2.0
I don't want to initialize a variable Im not gonna use.
Thanks.
This question is a bit silly. There's no reason you have to do it in one line of code and avoid declaring a variable. If you use the new operator you are initializing an instance of an object, regardless of whether you are declaring a variable for it or not.
That being said, I do not know what your ScanDataList is... There's a linq expression equivalent to FindAll called Where which may be more efficient than FindAll (because it doesn't have to create a new list, it just lazily iterates). If your ScanDataList is already IEnumerable then you can probably do something like this...
ScanDataList.Where(i => i.Badge == badge).ToList().ForEach(x=>x.EmpName = empname);
Even if your ScanDataList is not enumerable, you could implement an extension method of your own to help you accomplish this, but it seems like a lot of work for something that can easily be achieved without arbitrary unnecessary constraints (no new, no variables, etc).
So to clarify, I would probably use .Where LINQ expression because it is probably a bit more efficient because it doesn't need to create a new List. However, using that same logic, I'd probably avoid ToList() and separate your code into two lines with something like.
foreach(Employee emp in ScanDataList.Where(i => i.badge == badge))
emp.EmpName = empname;
This way, no additional list is created.
Related
I would like to put some tensor in a list, and I know if I would like to put nn.Module class into a list, I must use ModuleList to wrap that list.
So, Is there anything like 'TensorList’ in pytorch, that I must use to wrap the list containing tensors?
What are these tensors? Are these tensors parameters of your nn.Module? If so, you need to use the proper container.
For example, using nn.ParameterList. This way calling your module's .paramters() methods will yield these tensors as well. Otherwise you'll get errors like this one.
While doing a game engine that uses .lua files in order to read parameter values, I got stuck when I had to read these values and assign them to the parameters of each component in C++. I tried to investigate the way Unity does it, but I didn't find it (and I'm starting to doubt that Unity has to do it at all).
I want the parameters to be initialized automatically, without the user having to do the process of
myComponentParameter = readFromLuaFile("myParameterName")
for each one of the parameters.
My initial idea is to use the std::variant type, and storing an array of variants in order to read them automatically. My problems with this are:
First of all, I don't know how to know the type that std::variant is storing at the moment (tried with std::variant::type, but it didn't work for the template), in order to cast from the untyped .lua value to the C++ value. For reference, my component initialization looks like this:
bool init(luabridge::LuaRef parameterTable)
{
myIntParameter = readVariable<int>(parameterTable, "myIntParameter");
myStringParameter = readVariable<std::string>(parameterTable, "myStringParameter");
return true;
}
(readVariable function is already written in this question, in case you're curious)
The second problem is that the user would have to write std::get(myIntParameter); whenever they want to access to the value stored by the variant, and that sounds like something worse than making the user read the parameter value.
The third problem is that I can't create an array of std::variant<any type>, which is what I would like to do in order to automatically initialize the parameters.
Is there any good solution for this kind of situation where I want the init function to not be necessary, and the user doesn't need to manually set up the parameter values?
Thanks in advance.
Let's expand my comment. In a nutshell, you need to get from
"I have some things entered by the user in some file"
to:
"the client code can read the value without std::get"
…which roughly translates to:
"input validation was done, and values are ready for direct use."
…which implies you do not store your variables in variants.
In the end it is a design question. One module somewhere must have the knowledge of which variable names exist, and the type of each, and the valid values.
The input of that module will be unverified values.
The output of the module will probably be some regular c++ struct.
And the body of that module will likely have a bunch of those:
config.foo = readVariable<int>("foo");
config.bar = readVariable<std::string>("bar");
// you also want to validate values there - all ints may not be valid values for foo,
// maybe bar must follow some specific rules, etc
assuming somewhere else it was defined as:
struct Configuration {
int fooVariable;
std::string bar;
};
Where that module lives depends on your application. If all expected types are known, there is no reason to ever use a variant, just parse right away.
You would read to variants if some things do not make sense until later. For instance if you want to read configuration values that will be used by plugins, so you cannot make sense of them yet.
(actually even then simply re-parsing the file later, or just saving values as text for later parsing would work)
I have two QTextEdit objects. In my first QTextEdit object, I have set the text. In my second QTextEdit object, I have to type in the text.
I want to compare the two texts something like this:
if(ui->textEdit2->toPlainText() == ui->textEdit1->???)
My problem is that I don't know which method to use.
if(ui->textEdit2->toPlainText() == ui->textEdit1->toPlainText())
Q: My problem is that I don't know which function I need to use....
I am not entirely sure what makes you think this would require a different method call than for your other `textEdit2. You have at least two ways to achieve this depending on your need.
The first variant would be to simply use the same method call for textEdit1 as the for textEdit2, namely:
if (ui->textEdit1->toPlainText() == ui->textEdit2->toPlainText())
Note that I swapped the order as I think it reads better and it is more comprehensive that way. It may be just my personal style, so pardon me.
If you would like to do case insensitive comparison, then you could also write the following using the static compare method of the QString class.
if (!QString::compare(ui->textEdit1->toPlainText(), ui->textEdit2->toPlainText(), Qt::CaseInsensitive))
The first solution would be too limited to do a case insensitive comparison, so pick your solution based on your exact desire.
I am doing some conditional coding in scala template.
just tell me how to write following java logic into scala.html template.
String temp = "";
if(!cityName.equals(temp)){
temp=cityName;
}
else{
//do something..
}
Scala views allows you to define some variables with #defining block (see Reausable values), however it doesn't allow you to re-assignate it, so your pseudocode won't work as expected.
In such case you need to write custom getter in your model, which will return a valid value, instead doing it with temporary values in the views. You can also access any static Java method which will process your incoming string according to some conditions.
I must to say, that I have no idea what exactly you want to achieve, however I think, that can be solved with solutions proposed above.
I wrote a file parser for a game I'm writing to make it easy for myself to change various aspects of the game (things like the character/stage/collision data). For example, I might have a character class like this:
class Character
{
public:
int x, y; // Character's location
Character* teammate;
}
I set up my parser to read in from a file the data structure with syntax similar to C++
Character Sidekick
{
X = 12
Y = 0
}
Character AwesomeDude
{
X = 10
Y = 50
Teammate = Sidekick
}
This will create two data structures and put them in a map<std::string, Character*>, where the key string is whatever name I gave it (in this case Sidekick and AwesomeDude). When my parser sees a pointer to a class, like the teammate pointer, it's smart enough to look up in the map to fetch the pointer to that data structure. The problem is that I can't declare Sidekick's teammate to be AwesomeDude because it hasn't been placed into the Character map yet.
I'm trying to find the best way to solve this so that I can have my data structures reference objects that haven't yet been added to the map. The two easiest solutions that I can think of are (a) add the ability to forward declare data structures or (b) have the parser read through the file twice, once to populate the map with pointers to empty data structures and a second time to go through and fill them in.
The problem with (a) is that I also can decide which constructor to call on a class, and if I forward declare something I'd have to have the constructor be apart from the rest of the data, which could be confusing. The problem with (b) is that I might want to declare Sidekick and AwesomeDude in their own files. I'd have to make my parser be able to take a list of files to read rather than just one at a time (this isn't so bad I guess, although sometimes I might want to get a list of files to read from a file). (b) also has the drawback of not being able to use data structures declared later in the constructor itself, but I don't think that's a huge deal.
Which way sounds like a better approach? Is there a third option I haven't thought of? It seems like there ought to be some clever solution to this with pointer references or binding or something... :-/ I suppose this is somewhat subjective based on what features I want to give myself, but any input is welcome.
When you encounter the reference the first time, simply store it as a reference. Then, you can put the character, or the reference, or whatever on a list of "references that need to be resolved later".
When the file is done, run through those that have references and resolve them.
Well, you asked for a third option. You don't have to use XML, but if you follow the following structure, it would be very simple to use a SAX parser to build your data structure.
At any rate, instead of referencing a teammate, each character references a team (Blue team in this case). This will decouple the circular reference issue. Just make sure you list the teams before the characters.
<team>Blue</team>
<character>
<name>Sidekick</name>
<X>12</X>
<Y>0</Y>
<teamref>Blue</teamref>
</character>
<character>
<name>Sidekick</name>
<X>10</X>
<Y>50</Y>
<teamref>Blue</teamref>
</character>
Personally, I'd go with b). Splitting your code into Parser and Validator classes, both operating on the same data structure. The Parser will read and parse a file, filling the data structure and storing any object references as their textual names, leaving the real pointer null in your structure for now.
When you are finished loading the files, use the Validator class to validate and resolve any references, filling in the "real" pointers. You will want to consider how to structure your data to make these lookups nice and fast.
Will said exactly what I was about to write. Just keep a list or something with the unsolved references.
And don't forget to throw an error if there are unsolved references once you finish reading the file =P
Instead of storing Character object in your map, store a proxy for Character. The proxy will than contain a pointer to the actual Character object when the object is loaded. The type of Character::teammate will be changed to this proxy type. When you read in a reference that is not already in your map, you create a proxy and use the proxy. When you load an character which you already have an empty proxy in the map, populate it with your newly loaded character. You may also want to add a counter to keep track of how many empty proxy you have in the map so you know when all referenced characters have been loaded.
Another layer of indirection....it always make programming easier and slower.
One option would be to reverse the obligation. The Map is responsible for filling in the reference
template<T> class SymbolMap // I never could rememeber C++ template syntax
{
...
/// fill in target with thing name
/// if no name yet, add it to the list of thing that will be name
void Set(T& target, std::string name);
/// define name as target
/// go back and fill in anything that needs to be name
void Define(T target, std::string name);
/// make sure everything is resolved
~SymbolMap()
}
that won't interact well with value/moving semantics but I suspect that not much will.