|
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
|
|
|
|
|
the area how much intersected that part need to be code and change the boarder color allow user to choose in c# windows appication
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"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!
|
|
|
|
|
I have run into an issue using "LINQ Where" on a tree structure, to create a list of leaf nodes. After I delete those nodes in a loop, I would expect "leaf nodes" to be an empty list. Instead it contains 2 nodes. Can anyone explain this behavior?
Specifically the application does the following:
- convert a list of folder paths to a simple tree structure
- use LINQ to select terminating paths (folders with no sub-folders)
- convert/copy the linq iterator to a List<node> using "toList()"
- loop over the list and delete these nodes from the tree
- After processing, the LINQ iterator contains 2 nodes instead of 0 nodes
See 100 line example below...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>
{
@"\base",
@"\base\001",
@"\base\001\cfg",
@"\base\001\cfg\network",
@"\base\001\cfg\network\mgmnt",
@"\base\001\cfg\network\users",
@"\base\002",
@"\base\002\copy",
@"\base\002\copy\cfg",
@"\base\002\copy\cfg\network",
@"\base\002\copy\cfg\network\mgmnt",
@"\base\002\copy\cfg\network\users",
};
Tree tree = new Tree(list[0]);
foreach (string item in list)
{
tree.Add(item);
}
var nodes_to_delete_iter = tree.Root.Descendants().Where(d => d.Children.Count == 0);
var nodes_to_delete_list = nodes_to_delete_iter.ToList();
int start_count = nodes_to_delete_iter.Count();
for (int index = nodes_to_delete_list.Count - 1; index > -1; index--)
{
Node leaf = nodes_to_delete_list[index];
Console.WriteLine("Removing " + leaf.Path);
nodes_to_delete_list.Remove(leaf);
tree.Remove(leaf);
}
int final_count = nodes_to_delete_iter.Count();
Console.ReadKey();
}
}
public class Tree
{
public Node Root { get; private set; }
public Tree(string root_path)
{
Root = new Node(root_path);
}
public Node FindParent(string node_path)
{
int index = node_path.LastIndexOf("\\");
string parent_path = node_path.Substring(0, index);
Node parent_node = Root.Descendants().FirstOrDefault(n => n.Path == parent_path);
return parent_node;
}
public Node Add(string node_path)
{
Node parent_node = FindParent(node_path);
Node node = new Node(node_path);
if (parent_node != null) { parent_node.Children.Add(node); }
return node;
}
public void Remove(Node node)
{
Node parent_node = FindParent(node.Path);
parent_node.Children.Remove(node);
node = null;
}
}
public class Node
{
public string Path { get; private set; }
public List<Node> Children { get; private set; }
public Node(string value)
{
Children = new List<Node>();
Path = value;
}
public IEnumerable<Node> Descendants()
{
return new[] { this }.Concat(Children.SelectMany(child => child.Descendants()));
}
public override string ToString()
{
return Path;
}
}
modified 9-Nov-20 20:02pm.
|
|
|
|
|
You're manipulating a (realized) "list" that contains references to the contents of a (dynamic) LINQ query; and then trying to make sense of the count of the query result. You're expecting too much from a scenario that has quite likely been corrupted.
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
|
|
|
|
|
Thank you Gerry!
After reading your response, I followed up by searching for "c# modify ienumerable" which reinforced your point that these collections should not be modified, or if they are, then the results should not be interpreted.
c# - Update item in IEnumerable - Stack Overflow
Thanks again.
|
|
|
|
|
You're welcome! Glad I could help.
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
|
|
|
|
|
Stellar Developer wrote:
public IEnumerable<Node> Descendants()
{
return new[] { this }.Concat(Children.SelectMany(child => child.Descendants()));
} There's no need to create a new array here. Just use an iterator method.
public IEnumerable<Node> Descendants()
{
yield return this;
foreach (Node child in Children)
{
foreach (Node descendant in child.Descendants())
{
yield return descendant;
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm creating a RestSharp wrapper class. It has a generic ExecuteAsync() method:
public async Task<APIResponse<T>> ExecuteAsync<T>()
{
var task = await Task.Run(() =>
{
var apiResponse = new APIResponse<T>
{
Success = true
};
IRestResponse<T> response = client.Execute<T>(request);
if (response.StatusCode == HttpStatusCode.OK)
{
apiResponse.Result = response.Data;
return apiResponse.Result;
}
else
{
apiResponse.Success = false;
LogError(new Uri(baseURL), request, response);
return default(T);
}
});
return task;
}
It's won't compile on the the "return default(T)" line with
"Cannot implicitly convert type 'T' to 'APIResponse<t>'"
Here's the response class
public class APIResponse<T> : EventArgs
{
public APIResponse()
{
Messages = new List<ReponseMessage>();
}
public List<ReponseMessage> Messages { get; set; }
public T Result { get; set; }
public bool Success { get; set; }
}
public class ReponseMessage
{
public string Message { get; set; }
public Exception Exception { get; set; }
}
I'm calling it like this:
public APIResponse<Disclaimer> GetDisclaimer(int id)
{
APIExecutor webAPIExecutor = new APIExecutor("Disclaimer/GetDisclaimer");
Task<APIResponse<Disclaimer>> results = webAPIExecutor.ExecuteAsync<Disclaimer>();
return results.Result;
}
I think there's some confusion with the T parameter in the method name and the return type.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Your method defines the return type to be Task<apiresponse<t>> but on failure you try to return default(T) instead of a Task<apiresponse<t>>
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
Ignoring the Task , since this is an async method, your return type is declared as APIResponse<T> . However, you are returning APIResponse<T>.Result , which is most likely of type T .
It's not clear why you're using Task.Run instead of RestSharp's built-in async support.
public async Task<APIResponse<T>> ExecuteAsync<T>()
{
IRestResponse<T> response = client.ExecuteTaskAsync<T>(request);
if (response.StatusCode == HttpStatusCode.OK)
{
return new APIResponse<T>
{
Success = true,
Result = response.Data,
};
}
LogError(new Uri(baseURL), request, response);
return default;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard. That did it
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Hello
I am planning to make an app which could be used between defined times and if the time is not between the defined time then the app would give a warning and close the app.
Originally thinking about making a parental control app for parents and their kids so the parents can tell when it is enough.
I am using C# to make everything possible and I am using a database (MYSQL) to get the details.
The following are my questions:
Which way I should save in the database for different parents the timezone?
How I make the app to use that timezone which the parents defined?
If the parent will change the timezone while the app working and the time when the app can work then how the app will recognize that the data changed in the database?
I am newbie and I would be thankful for example or open source and some explanation if it is possible.
Thank you in advance all of the answers.
|
|
|
|