|
|
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!
|
|
|
|
|
Hi,
I'm trying to use Persian calendar in my application. I use a component (.dll file) for Persian date picker.
All my information including dates are stored in rows and columns of a DataGridView (named dataGridView1). For example, I have due dates in column 7 of each row. I want to find remaining days (from current day) of the corresponding due dates in each row and write them in column 8. All the process described above are done by clicking a button.
What code should I write for btn.click event?
modified 10-Nov-20 14:23pm.
|
|
|
|
|
|
How can I retrieve date information from each row of column 7 in DataGridView? What method can extract text from those cells? Please give me a sample code.
|
|
|
|
|
How can I provide sample code for something that I have no information on? Where is the source of the data in column 7 and what type is it? If it is a DateTime then subtracting today's date from that to get a TimeSpan is a couple of lines of C#. If it is a string then you need to parse it into a DateTime object. Either way it is not complicated.
|
|
|
|
|
Column "8" is a "calculated" column whose value is calculated at run time. You subtract DateTime.Now from the due date to get a TimeSpan, and putting .TotalDays from it into column 8 when you add a row or update the due date.
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
|
|
|
|
|
Can you please write a sample code so I can study it and implement in my own application?
My problem is that I cannot retrieve information from each cells in column 7 as string (.Text).
I think I need to retrieve information from those cells as string and then use Split() method to put the date in an array.
|
|
|
|
|
How did you load the grid in the first place? I said: calculate column 8 when you load 7. Nothing to do with "retrieving".
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
|
|
|
|
|
As Gerry pointed out you should work with the underlying data not the DGV. So add an additional field/property to your source collection, iterate through the items in the collection calculating the date difference and populating the new field/property, this field/property should be bound to your DGV (column 8).
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I'm creating a program that has multiple independent processes that grab JSON from different sites. So I copy the JSON results and Paste Special into my project and it creates the associated class info. So the problem.
I paste in the first one and get like:
public class Rootobject
{
public Datum[] data { get; set; }
public Meta meta { get; set; }
public Links links { get; set; }
}
public class Meta
{
public int count { get; set; }
public string version { get; set; }
}
public class Links
{
public string self { get; set; }
public string first { get; set; }
public string last { get; set; }
public string next { get; set; }
}
public class Datum
{
public string type { get; set; }
public string id { get; set; }
public Attributes attributes { get; set; }
public Relationships relationships { get; set; }
public Links4 links { get; set; }
}
Then I go to the other source and get:
public class Rootobject
{
public Result result { get; set; }
public bool success { get; set; }
}
public class Result
{
public string title { get; set; }
public Datum[] data { get; set; }
public int total { get; set; }
}
public class Datum
{
public string type { get; set; }
public string title { get; set; }
public string series_title { get; set; }
public string label { get; set; }
public string content_id { get; set; }
public string airdate { get; set; }
public long airdate_ts { get; set; }
public DateTime airdate_iso { get; set; }
public string expiredate_raw { get; set; }
public string season_number { get; set; }
public string episode_number { get; set; }
public string duration { get; set; }
public int duration_raw { get; set; }
public string rating { get; set; }
public Regionalratings regionalRatings { get; set; }
public string description { get; set; }
public Thumb thumb { get; set; }
public string url { get; set; }
public string app_url { get; set; }
public string amazon_est_url { get; set; }
public string itunes_est_url { get; set; }
public string streaming_url { get; set; }
public string live_streaming_url { get; set; }
public string tms_program_id { get; set; }
public object show_id { get; set; }
public string asset_type { get; set; }
public string status { get; set; }
public string expiry_date { get; set; }
public bool is_paid_content { get; set; }
public string ios_available_status { get; set; }
public string ios_available_date { get; set; }
public string android_available_status { get; set; }
public string android_available_date { get; set; }
public long tracking_media_id { get; set; }
public object signature { get; set; }
public object media_type { get; set; }
public object vtag { get; set; }
public bool is_live { get; set; }
public int medTime { get; set; }
public string genre { get; set; }
public Metadata metaData { get; set; }
public string brand { get; set; }
public string[] videoProperties { get; set; }
public string media_content_type { get; set; }
public object mpd_url { get; set; }
public object license_url { get; set; }
public object closed_captions { get; set; }
public int positionNum { get; set; }
public bool is_protected { get; set; }
public bool drm { get; set; }
public string raw_url { get; set; }
public string episode_title { get; set; }
public object pubdate_iso { get; set; }
public string thumbUrl { get; set; }
public bool isUserSubscriber { get; set; }
public string aaLink { get; set; }
public string dataTracking { get; set; }
public string displayTitle { get; set; }
}
There is more to both of these but basically Rootobject and Datum are duplicated. Any way to deal with this? They are basically two independent JSON sets so not related to each other just two calls being made to different API's in the same program but both loop thru things independent of each other.
Thanks.
JR
|
|
|
|
|
Your create a separate "name space" for each set that has duplicates; then specify the appropriate namespace and class when you deserialize.
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
|
|
|
|
|
Ahhh, fantastic. Thanks for that.
|
|
|
|
|
You're welcome!
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
|
|
|
|