Here is my dictionary datatype:
private Dictionary<String, List<PriceAndStandard>>
PriceAndStandard is a structure:
struct PriceAndStandard
{
public double mePrice;
public int? meStandard;
}
I want to be able to grab mePrice and meStandard for each item.
Error I am recieving
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Collections.Generic.List<AspDotNetStorefront.showproduct.PriceAndStandard>
I would be able to grab the value if it were a basic key- string value pair with:
shaftModelDictionary.Values.ToList();
But because I have a list I really need to flatten out the data so it can be stored.
Working Code:
foreach (string option in product.AvailableClubOptions.ShaftModel)
{
string shaftModelText = option;
if (minPriceForShaftMaterial > 0 && !string.IsNullOrEmpty(option))
{
List<PriceAndStandard> Paul = shaftModelDictionary.Keys.ToList();
double priceForShaftModel = shaftModelDictionary.[option];
double priceForSelectedShaftModel = !String.IsNullOrEmpty(shaftModel) ? shaftModelDictionary[shaftModel] : 0;
Update:
It seems like there is a problem with option since that is a string and not compatible with the the values in my dictionary being lists.
Update 2:
foreach (string option in product.AvailableClubOptions.ShaftModel)
{
string shaftModelText = option;
if (minPriceForShaftMaterial > 0 && !string.IsNullOrEmpty(option))
{
double priceForShaftModel = shaftModelDictionary[option].mePrice;
double priceForSelectedShaftModel = !String.IsNullOrEmpty(shaftModel) ? shaftModelDictionary[shaftModel].mePrice : 0m;
double priceDelta = 0.0d;
double MinimumPrice = Convert.ToDouble(ltOurPrice.InnerText.Replace("$", ""));
'List' does not contain a definition for 'mePrice' and no extension method 'mePrice' accepting a first argument of type 'List'
try:
double priceForSelectedShaftModel = !String.IsNullOrEmpty(shaftModel) ? shaftModelDictionary[shaftModel].first().mePrice : 0d;
Edit:
My first answer was incorrect, sorry. The values in your dictionary are Lists. So when you retrieve a value (which is a list of PriceAndStandard objects), you need to either work with that list or find a specific PriceAndStandard object in the list to work with. In the example I show I use the first() method to find a the first object in the list. You may want to read up on LINQ and use the where() method or something similar.
I'm not sure what you are trying to accomplish... are you sure you want the values in your dict to be Lists? There is nothing wrong with that except it is a bit unusual. Perhaps you were thinking of this?:
private Dictionary<String, PriceAndStandard>
Also 0m is decimal, 0d is double. My bad.
Related
I am writing a code for which I need to declare an array of around 200 indexes. Now to search a specific index I at least need to travel the array for a defined time or until desired value is achieved - hence at times I might need to travel 200 times if needed - for 200 value row.
This is exactly I wish to ignore so I landed coding it below way:
enum Index{ salary, age };
static const Datas Mydata [] =
{
[Index::one] = {"hello", function_call_ptr_1, function_call_ptr_2},
[Index::two] = "hekllo1", function_call_ptr_1, function_call_ptr_2}
};
Hence in my code I can directly seek it like below:
Mydata [Index::age]
Assuming that access to above structure is to be done inside a function - hence the function should receive Index value as argument to itself. But still what if arguments passed was wrong like:
age = 0;
fun(age);
Is there a better way to access Mydata so that its desired row can easily be accessed without any flaw?
List<Issue> issues = issueSearcher.get().getAppropriateIssues();
From the above line of code I will be getting two values. 1. Project 2. count
How to save these two values in a single variable? with collections or any other concepts
As far as I understand:
getAppropriateIssues() will return a List<Issue>. With Count you probably mean the number of Issue in the List. Where does the Project come from?
In any way, if you want to store two variables into one, you can either
use a Map<K,V> and store the two values there
or (probably the better way) is to use a Tuple / Pair class. You can either create one yourself or use the Apache Commons Pair.
If you want to implement a Pair class on your own it could look roughly like this:
public class Pair<A,B> {
private A left;
private B right;
public Pair(A left, B right) {
// assign
}
// getters + setters
}
You can use HashMap for storing the first value 'Project' as key and second value 'count' as value. Split the value returned by your query and put to hashmap.
HashMap<String, Integer> resultMap = new HashMap<String, Integer>();
If there is the following vector1:
vector<string*> cont; // cont[0] == "0"
where pointers to strings are named either l or r; are sequentially added like so:
string r* = new string("1");
cont.emplace_back(r);
or:
string l* = new string("-1");
cont.emplace_back(l);
For example: if there is a direction to a node given like: "lrlrrr".
Is there a way to search through the vector using the string names, l and r, as "element id" rather than string content2?
Note: I've researched finding a vector element by native property, however, I'm interested if there is alternative way.
1. The vector stores sequentially (level by level) the nodes of a binary tree, where each left node's,l, value is: parent value - 1 and each right node's, r, value is: parent value + 1.
2. Comparing current and previous node values determines if current node left or right.
It is generally weird to use pointers to string in C++, since string internally contains a pointer to char giving a double indexation. But in this use case, it could make sense, if you store pointers to the same constant objects:
static string _r = "1";
static string _l = "-1";
const string * r = &_r;
const string * l = &_l;
then you could do
cont.emplace_back(r);
or
cont.emplace_back(l);
Because when iterating the vector of pointers you can do if (cont[i] == r) ...
If you really build new different objects on each step, storing pointers would only make sense if you need polymorphism, but it would be hard to test as identity if you do not have a know set of possible objects.
Is there a way to search through the vector using the string names?
No, because the names, l or r, are local variables1 that get destroyed after the string is stored in the vector. Once stored, the vector indexes become "names" of the stored strings.
1. l and r are rvalues and their addresses can not be obtained after the execution of the function that contains them.
I just wonder if I can use a "complicated" map as the value of another map. I have self-defined several structs as follow:
typedef std::vector<std::string> pattern;
typedef std::map<int, std::vector<pattern>> dimPatternsMap;
typedef std::map<int, dimPatternsMap> supportDimMapMap;
OK let me explain these things...pattern is a vector of strings. For the "smaller" map dimPatternsMap, the key is an integer which is the dimension of pattern (the size of that vector containing strings) and the value is vector containing patterns (which is a vector of vectors...).
The "bigger" map supportDimMapMap also use an integer as the key value, but use dimPatternsMap as its value. The key means "support count".
Now I begin to construct this "complicated" map:
supportDimMapMap currReverseMap;
pattern p = getItFromSomePlace(); //I just omit the process I got pattern and its support
int support = getItFromSomePlaceToo();
if(currReverseMap.find(support) == currReverseMap.end()) {
dimPatternsMap newDpm;
std::vector<pattern> newPatterns;
newPatterns.push_back(currPattern);
newDpm[dim] = newPatterns;
currReverseMap[support] = newDpm;
} else{
dimPatternsMap currDpm = currReverseMap[support];
if(currDpm.find(dim) == currDpm.end()) {
std::vector<pattern> currDimPatterns;
currDimPatterns.push_back(currPattern);
currDpm[dim] = currDimPatterns;
} else {
currDpm[dim].push_back(currPattern);
}
}
Forgive me the code is really a mass...
But then as I want to traverse the map like:
for(supportDimMapMap::iterator iter = currReverseMap.begin(); iter != currReverseMap.end(); ++iter) {
int support = iter->first;
dimPatternsMap dpm = iter->second;
for(dimPatternsMap::iterator ittt = dpm.begin(); ittt != dpm.end(); ++ittt) {
int dim = ittt->first;
std::vector<pattern> patterns = ittt->second;
int s = patterns.size();
}
}
I found the value s is always 1, which means that for each unique support value and for each dimension of that support value, there is only one pattern! But as I debug my code in the map constructing process, I indeed found that the size is not 1 - I actually added the new patterns into the map successfully...But when it comes to traversing, all the sizes become 1 and I don't know why...
Any suggestions or explanations will be greatly appreciated! Thanks!!
dimPatternsMap currDpm = currReverseMap[support];
currDpm is a copy of currReverseMap[support]. It is not the same object. So then when you make changes to currDpm, nothing within currReverseMap changes.
On the other hand, if you use a reference:
dimPatternsMap& currDpm = currReverseMap[support];
then currDpm and currReverseMap[support] really are the same object, so later statements using currDpm will really be changing a value within currReverseMap.
There are a few other places where your code could benefit from references too.
My guess: you should use a reference in your else:
dimPatternsMap& currDpm = currReverseMap[support];
Your current code creates a copy instead of using the original map.
Your problem is this line:
dimPatternsMap currDpm = currReverseMap[support];
Based on the code following it, it wants to read like this:
dimPatternsMap& currDpm = currReverseMap[support];
Without the & you modify a copy of the entry rather than the existing entry.
Your code is making several copies of the objects underneath, try using more references and iterators (find() already gives you an element if it was found, for example).
For example, dimPatternsMap currDpm = currReverseMap[support]; actually makes a copy of a map in your structure and adds an element to it (not to the original). Try using a reference instead.
I am having problems translating C++ data structures to Scala. Scala is really different from C++, but I like a lot of it.
I have the following Code fragment in C++:
struct Output
{
double point;
double solution[6];
};
struct Coeff
{
double rcont1[6];
double rcont2[6];
double rcont3[6];
double rcont4[6];
double rcont5[6];
double rcont6[6];
};
std::list<Output> output;
std::list<Coeff> coeff;
I now fill the list in a while loop with data
while(n<nmax) {
if step successfull
Output out;
out.point = some values;
out.solution[0] = some value;
output.push_back(out);
}
I tried creating a simple class in Scala to hold the data.
class Output
{
var point: Double
var solution: Array[Double] = new Array(6)
}
But this doens't work since point is not initialized. Is there a way around this? I just want to define the variable but not initialize it.
Another quick thing. I am looking for an equivalent to stl::lower_bound.
Is finds the right position to insert an element in an sorted container to maintain the order.
Thanks for helping a Scala beginner
Why don't you want to initialize it? For efficiency? I'm afraid that the JVM doesn't let you get away with having random junk in your variables based on whatever was there originally. So since you have to initialize it anyway, why not specify what your "uninitialized" value is?
class Output {
var point = 0.0
var solution = new Array[Double](6)
}
(You could use Double.NaN and check for point.isNaN if you later need to see whether the value has been initialized or not.)
You could use _ as the default initialization, but unless you use it in generic code:
class Holder[T] {
var held: T = _
}
then you're just obscuring what the value really will be set to. (Or you are announcing "I really don't care what goes here, it could be anything"--which could be useful.)
I just found the answer to the intialistion:
class Output
{
var point: Double = _
var solution: Array[Double] = Array(6)
}
Puh Scala has a lot of syntx to get used to :-)
Anyone have a solution for the lower_bound equivalent ?
It's hard to translate effectively, as you've left a lot of unknowns hidden behind pseudo code, but I'd advocate something along these lines:
// type alias
type Coeff = Seq[Seq[Double]]
// parameters passed to a case class become member fields
case class Output (point: Double, solution: Seq[Double])
val outputs = (0 to nmax) map { n =>
Output(generatePoint(n), generateSolution(n))
}
If you can flesh out your sample code a bit more fully, I'll be able to give a better translation.