|
Try passing the mainmenuitem to the addchilditems and then add the childitem to the mainmenuitems collection of menuitems.
You are currently adding the childitem to the mainmenu
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Would it be possible for you to point to some examples on CodeProject or provide a C# example, showing me what I would need to do, to correct the code I currently have so far. Any help would be greatly appropriated.
|
|
|
|
|
Scratching my head on this.
I have a list of inventory items, and I want to add how many has sold from orders. I can get the inventory, and then loop that inventory to get the "itemNumber", and query the orders to extract only orders that contain that item. But each order can have multiple items sold, so I need to get the "items sold array" within the order.
This is written for MongoDB, and is not really a MongoDB question. I have no clue how to write this in pure Mongo C# driver.
OrderId<br />
OrderNumber<br />
OrderItems = > ItemId<br />
OrderItems = > ItemNumber<br />
OrderItems = > Qty<br />
OrderItems = > Price
I wrote code to get all my inventory items, and used a for each of each item to query the orders. But I can't figure out the GroupBy part, or perhaps GroupBy is not the right solution.
The var in purple below is suggesting the order model, and not the OrderItems within the Order model. I know I constructed the GroupBy wrong, or using GroupBy is just plain wrong.
var start = (page * show) - show;
var query = _context.MarketPlaceInventory.Find(mp => mp.MarketChannel.Equals(marketChannel)).SortBy(s => s.ItemNumber);
var totalTask = query.CountDocumentsAsync();
var inventoryTask = query.Skip(start).Limit(show).ToListAsync();
await Task.WhenAll(totalTask, inventoryTask);
foreach (var product in inventoryTask.Result)
{
var queryFilter = Builders<MarketPlaceOrders>.Filter.ElemMatch(oi => oi.OrderItems, item => item.ItemNumber == product.ItemNumber);
var ordersTask = _context.MarketPlaceOrders.Find(queryFilter).ToListAsync();
await Task.WhenAll(ordersTask);
var orderItems = ordersTask.Result.GroupBy(x => x.OrderItems).Select(item => new MarketPlaceOrderItems() {
ItemQty = item.Sum(qty => <code>qty.ItemQty</code>)
});
Console.Write(orderItems);
}
return new Get_MarketPlaceInventory
{
Count = totalTask.Result,
Inventory = inventoryTask.Result,
Page = page,
Show = show
};
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Inventory: Item #, Quantity on hand (QTY).
Order Item: Item #, Quantity sold (-QTY).
Merge and sum to get the inventory balance (or stock "not committed").
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi!
I try to show dynamically a Xamarin CollectionView (as a visual list of multiple notes), by following this tuto: https://medium.com/a-developer-in-making/how-to-work-with-collectionview-in-xamarin-forms-5dc65c50b419[^]
Everything's work fine, until I try to return a list made of data from a Realm database.
So, that code from the tuto is working fine:
return new List<Note>
{
new Note() { Category = listFolders[0].IDCategories, Title = listFolders[0].Title, ContentNote = listFolders[0].ContentNote, Image = listFolders[0].Icon },
new Note() { Category = listFolders[1].IDCategories, Title = listFolders[1].Title, ContentNote = listFolders[1].ContentNote, Image = listFolders[1].Icon },
new Note() { Category = listFolders[2].IDCategories, Title = listFolders[2].Title, ContentNote = listFolders[2].ContentNote, Image = listFolders[2].Icon },
};
And it doesn't work when I try to do the same thing, from a Realm database. Something like that:
(there is no errors, it's just show nothing during the debug).
List<Note> list = new List<Note>();
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
return list;
And I tried that too:
return new List<Note>
{
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
}
But just after the first bracket, VS Studio say: " } awaited " and " ; awaited ". But I don't know where precisly... And there is no correction suggested.
I'm sure that's probably simple, but as a beginner, I still miss some things.
Thanks!
|
|
|
|
|
Member 14992862 wrote: but with code errors...
We cannot guess what code errors you get or where they occur. Please edit your question and add the full details.
|
|
|
|
|
Because it was just to expose something that I've tried, without thinking especially that the solution is there, but maybe:
return new List<Note>
{
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
}
Just after the first bracket, VS Studio say: " } awaited " and " ; awaited ". But I don't know where precisly... And there is no correction suggested.
Thanks!
|
|
|
|
|
You can't write a collection initializer like that: it's a list of properties, but you can't use loops in there at all as it uses the Add method on each instance in the list: Object and Collection Initializers - C# Programming Guide | Microsoft Docs[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You never actually add the new Note to the List (call it something else eg NoteList)
for...
Note X = new Note()...
NoteList.Add(x)
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Aaah, yes, I got it! Thanks to you both!
So, the solution is:
List<Note> list = new List<Note>();
for (int i = 0; i < countFolders; i++)
{
Note AddAllContent = new Note(list) { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
list.Add(AddAllContent);
}
return list;
|
|
|
|
|
I'm a C# user. I did some search in Internet but I couldn't find any proper solution for my problem.
I have a DataGridView which adds rows and fill their value only by pressing a button. The input values is achieved from some Textboxes. My last Column is a Dropbox which has 3 defined items. I want to select first Item as a default value when the button is pressed. User can change it manually later.
How can I do this programmatically?
I tried this code for the button (some part of the code):
Int32 LastRow = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[LastRow].Cells[9].Value = new DataGridViewComboBoxCell { };
The code after "=" is wrong. I don'y know what to write inside curley brackets.
|
|
|
|
|
You need parentheses not braces to instantiate an object:
dataGridView1.Rows[LastRow].Cells[9].Value = new DataGridViewComboBoxCell();
You can then add items to the cell. Although it is probably a better idea to create the cell and populate it before you add it to the DataGridView.
Sorry, I am well out of date. Time to go back and put some study time in.
modified 13-Nov-20 8:15am.
|
|
|
|
|
|
Thanks for reminding me; some re-reading to do.
|
|
|
|
|
Can you please give me the correct code for this purpose?
|
|
|
|
|
You know what's in the combobox, since you created it. If you want to "default" a new row to a particular value, load the "value"; don't go around trying to manipulate the CB, because it doesn't seem to be supported in this case.
And you would make it easier if you specified at the outset if this is Windows Forms or WPF.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Commented code in c# forms does it affect the performance in any way as i have been told to apply sonarqube in project,while compiling with sonarqube we get errors asking to get the comments in the forms removed.
|
|
|
|
|
No. Comments do not affect performance, they are are all removed prior to the EXE file being produced.
Since they do not make it into the EXE they can have no affect on performance whatsoever.
This does not apply just to C#, but every compiled language.
Comments are vital to developers, but they never reach the end user (which some of us are extremely grateful for) except in interpreted languages such as Python where the entire source code is released and parsed as it executes.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Today, even "interpreted" languages are frequently JIT-precompiled into some "bytecode" which is then interpreted. In this step, comments are peeled off, and do not affect interpretation of bytecodes. Startup (i.e. the JIT compilation) may be a couple milliseconds slower if the source has many huge comment blocks, but that is a one-time operation; it is not repeated a thousand times if the comment was placed in a loop executed a thousand times.
Some systems cache the bytecode (e.g. for Python: in a .pyc file), so for the next execution of the program, you have no startup delay. (This resembles .net processing where the first use of a CIL assembly invokes a JIT compiler to build native code, which is cached. In the .net case, comments are peeled off in the initial compilation to CIL, and the JIT compilation is from an intermediate language (sort of like bytecode, but different) to native code.)
I guess there are still a number of systems that interpret source code directly, line by line. If a loop is iterated a thousand times, each source line is broken up into tokens, and their semantics determined, a thousand times. But such systems are getting fewer and fewer. Today, even Javascript / HTML is usually converted to some intermediate bytecode before execution.
I was working with Tcl (/Tk) at the time when JIT compilation was introduced, and made some timing tests, comparing the old directly-from-source interpretation against the JIT bytecode alternative. For loops executed a few thousand times, execution were in many cases 5-7 times faster with the bytecodes.
|
|
|
|
|
"Compiling" with Sonarqube ...
It's NOT a compiler.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Greetings,
I'm new to C# and currently I'm trying to make 2 simple textboxes for weather, one to have the temperature in Celsius, and the other textbox have the temperature in Fahrenheit, what I wanna do is make the user type the temperature in any textbox and the other will calculate and convert it like from Celsius to Fahrenheit and vice versa,
but I keep getting errors:
"The name 'TXTF" does not exist in the current context"
"The name 'TXTC" does not exist in the current context"
here is my code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MainWeb2
{
public partial class Weather : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
String C = TextBox1.Text;
int TXTC = Int32.Parse(C);
string TXTC1 = TXTC.ToString();
TXTC = (TXTF - 32) * (5 / 9);
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
String F = TextBox2.Text;
int TXTF = Int32.Parse(F);
string TXTF1 = TXTF.ToString();
TXTF = (TXTC) * (9 / 5) + 32;
}
}
}
What should I do?
|
|
|
|
|
darkdxd wrote:
String C = TextBox1.Text;
int TXTC = Int32.Parse(C);
string TXTC1 = TXTC.ToString();
TXTC = (TXTF - 32) * (5 / 9); Look at the variables you have declared here:
C - a local variable of type string ;TXTC - a local variable of type int ;TXTC1 - an unused local variable of type string ;TextBox1 - a field of type TextBox ;
You then try to perform a calculation using the variable TXTF , which you haven't declared anywhere within this method.
Some other problems:
- You're using
Int32.Parse , which will throw an exception if the user types in something that's not a number. You should use Int32.TryParse[^] instead. - You convert the integer back to a string for no reason;
- You're performing integer arithmetic, which isn't going to work - for example,
5 / 9 will return 0 ; - Based on the variables names, it looks like you've got the calculations the wrong way round;
- You don't do anything with the result of the calculation;
You're also still using the default control names assigned by the Visual Studio designer. You should give your controls more meaningful names instead.
An improved example:
protected void CelsiusTextBox_TextChanged(object sender, EventArgs e)
{
string celsiusText = CelsiusTextBox.Text;
double celsius;
if (!double.TryParse(celsiusText, out celsius))
{
FahrenheitTextBox.Text = "Please enter a valid temperature";
return;
}
double fahrenheit = celsius * (9D / 5D) + 32D;
FahrenheitTextBox.Text = fahrenheit.ToString();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
TXTC is declared and exists only within TextBox1_TextChanged, so you cannot reference it from TextBox2_TextChanged. TXTF is similar, only the other way around.
Declare both variables outside the two functions (but within the class!); then both variables will be available in both functions.
|
|
|
|
|
It is because TXTC and TXTF are both local to the methods that they are created in. You need to create them at the class level so they are visible to both methods, thus:
public partial class Weather : System.Web.UI.Page
{
int TXTC;
int TXTF;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
String C = TextBox1.Text;
TXTC = Int32.Parse(C);
string TXTC1 = TXTC.ToString();
TXTC = (TXTF - 32) * (5 / 9);
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
String F = TextBox2.Text;
TXTF = Int32.Parse(F);
string TXTF1 = TXTF.ToString();
TXTF = (TXTC) * (9 / 5) + 32;
}
}
You should read up on the concept of "scope" in C# (and other languages) for a fuller explanation.
|
|
|
|
|
all objects in C# have what is called scope - they only exist within the area of code delimited by curly brackets - in this case the function.
So if I write this:
private int aClassVariable = 666;
private void foo(int aParameter)
{
int aVaraiable = 999;
if (aParameter > 0)
{
int anotherVariable = aParameter / 2;
}
} Then the scope of the variables is as follows:
aClassVariable is available to all code in the class.
aParameter is available to all code in the method foo, but not outside it.
aVaraiable is available to all code in the method foo, but not outside it.
anotherVariable is available to code only within the if block
Since TXTC is is declared within the TextBox1_TextChanged method of your code, it is only available within that method, and does not exist for any other code.
What can you do about it? Move TXTC to class level: declare it outside any method, and mark it as private .
But ... you will have to give it a default value or your code will probably not compile, and you probably need to check in TextBox2_TextChanged to see if it's got a "real value" before you use it in case the user types in the boxes the wrong way round!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|