|
You can't: a string does not allow you to access variables, it's just a sequence of characters and compilation is well and truly over by the time it gets a value at all!
It might be possible to access some class level fields by using reflection, but even then, you couldn't just pass Reflection a string containing two variables and two array indexes and expect it to give you the value.
I don't know why you think you need this, but I'd suggest that you have over thought this code and are going down entirely the wrong route ...
"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'm using DevExpress in my project. I have written a query builder class that creates SQLite queries dynamically based on 3 comboBoxEdit controls with multi-selection properties, and 6 TextBox controls.
I get my data from SQLite file based on those 9 controls which the user select and then the filtered data are placed into a DataTable for further calculation. I have 4 CheckBox controls. These CheckBox controls determine the final calculations. First CheckBox is related to column 5 of DataTable, the second CheckBox is related to column 7 of DataTable, the third CheckBox is related to column 10 of DataTable, and the fourth CheckBox is related to column 14 of DataTable. I want to make a string expression from these selected checkboxes (using Join()) and use it inside a For Loop. I have written a code that can add checked items to a list.
My code is complicated and I cannot bring all of them here.
My problem is that I cannot make a dynamic expression inside For Loop based on those 4 CheckBoxes.
I need expresions like
dt.Rows[i][7].ToString()
|
|
|
|
|
Instead, why not make a List of CheckBoxes, and put the associated column number into the CheckBox.Tag property?
Inside the loop, you retrieve the checkbox check it's Checked state, and if set, cast the Tag to an integer to access the column? Or better, put the column name in the Tag as string and use that instead as it's more obvious and maintainable?
Passing crap around as strings and then "decomposing" them when you need to use them is generally a bad idea ...
"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!
|
|
|
|
|
Thanks. I used Tag property and also Switch statement to solve this issue.
|
|
|
|
|
Why is the switch necessary?
Off topic: You may like to know that you are having questions asked about you ... or perhaps not ... strange "lack of pattern" by one poster in the C# language forum: abuse ? or ?[^] - I don't know where that is going to end up, but Bill is not wrong. These questions do reveal a fundamental lack of understanding of the basics.
"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!
|
|
|
|
|
It is necessary because I need to join some strings based on selected CheckBoxes and use it as a Dictionary<> key. I want to use those selected checkboxes to find the summing of Column11 (in DataTable) values.
modified 13-Aug-21 13:59pm.
|
|
|
|
|
@OriginalGriff if I'm bothering you and others in this forum, please tell me. I will go if you don't like me and my questions. But, I say again, I need your help.
modified 13-Aug-21 13:42pm.
|
|
|
|
|
Quote: I'm trying hard to learn and have had some successes yet. I'm working on a reporting project. Sometimes I need help to solve my problems during the developing process. I use YouTube, StackOverFlow, Codeproject, Google, etc. and finally find my answer and learn the points. Microsoft documentation is great for learning but there are moments you need to get help from others about how and with what tools you can solve your problem. And this website has helped me a lot. Now, At the same time, I'm learning ASP.NET using YouTube tutorials. I can achieve my goals. One day, I will be a good programmer. I promise.
I didn't reply immediately, because this is difficult - and probably going to be more so for you than I. So I'll apologise in advance if I cause any offence, and say that you should view this as constructive criticism rather than getting upset if you can.
You aren't learning.
That's blunt, but it's pretty much true. What you are learning is not the right stuff - you are making the same mistakes, ignoring the same things, following the wrong path; this shows in your questions both in QA and here. What you are do is picking up solutions to specific problems, most of which exist only because you have created them because you don't know any better.
And that's because you are trying to learn the wrong way.
Let's ignore computers and think about becoming a surgeon. Nobody starts off on day one being gowned up, masked, up, gloved up, given a scalpel and an unconscious patient then told "fix his hernia" with a mobile number to call if you get stuck while your teacher is off to the golf course.
They have to learn from books, be shown what goes on inside the body, practice on corpses, and probably a whole load of other gloopy things we'd rather not think about while eating.
That takes time: lots of it. And one of the first things they are allowed to do on an actual body - and have to practice a lot - is "opening and closing": making the initial incision, and sewing it back up again. In short, the basics. Getting a feel for what they are supposed to do (and weeding out the ones who will vomit at eteh first sign of blood)
And software is the same: you need to have the basics down pat or you don't really know what you are doing.
And you don't. Not even close. Your question on strings recently for example: How to split an string and remove extra spaces - C# Discussion Boards[^]
If you had a good grasp of the basics of strings you would never have asked that - you would know that Trim existed (even if you couldn't remember it's name) and looked for it instead of spending nearly an hour waiting for someone else to help you!
Your whole approach to learning is wrong.
Youtube - generally useless, because most of the tutorials you will find are produced by people who want "likes and subscribes" for the money they bring, while having as little idea how to do things as the people they are "teaching". There are good ones out there I'm sure - but they are well hidden in a huge mountain of dross!
SO, CP, Google - good resources for problem help, but not teaching basics!
MS documentation is OK, and comprehensive - but unless you know what you are looking for it's pure luck if you can find it!
SO, CP, MS are all very handy - but what you need to do is learn how to do it properly. And that means (in order of descending quality):
1) A course. Having a human tutor means he can rephrase and reexplain things if you don't understand. You also get homework which grades you progress and let's him and more importantly you know when you are going wrong. Local colleges may have classes.
2) A book. Addison Wesley, Wrox, Microsoft Press all do excellent ones, and provide tests for you to try and implement to both reinforce and check learning.
3) Guess and hope. Throw some code down, hope it works. When it doesn't try to fix it and then give up and ask someone else to fix it for you.
4) Youtube tutorials. Learn to make the same mistakes as other people on code samples that don't even compile half the time! Make money for idiots and spend a lot of time doing it!
At the moment, you are using a combination of 3 & 4 - and it shows in your questions and the "structure" of the code and design behind them.
Please, do yourself a favour, and think about this for a while before you explode and reply.
This is intended as constructive criticism, not to upset you - and I'm sorry in advance if it did.
"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!
|
|
|
|
|
The class I wrote for this purpose:
class ActivityTimeSum
{
public string settings { get; set; }
public string dataTableName { get; set; }
public DataTable selectDT { get; set; }
public CheckEdit checkBox1 { get; set; }
public CheckEdit checkBox2 { get; set; }
public CheckEdit checkBox3 { get; set; }
public CheckEdit checkBox4 { get; set; }
Dictionary<string, int> myDic = new Dictionary<string, int>();
List<string> keys = new List<string>();
List<int> values = new List<int>();
List<CheckEdit> checkBoxes = new List<CheckEdit>();
List<string> selectedCheckBox = new List<string>();
public Dictionary<string, int> CalculateTime()
{
checkBoxes.Add(checkBox1);
checkBoxes.Add(checkBox2);
checkBoxes.Add(checkBox3);
checkBoxes.Add(checkBox4);
foreach (var c in checkBoxes)
{
if (c.Checked)
{
selectedCheckBox.Add(c.Tag.ToString());
}
}
for (int i = 0; i < selectDT.Rows.Count; i++)
{
switch (selectedCheckBox.Count)
{
case 1:
keys.Add(selectDT.Rows[i][Int32.Parse(selectedCheckBox[0])].ToString());
break;
case 2:
keys.Add(string.Join(", ", selectDT.Rows[i][Int32.Parse(selectedCheckBox[0])].ToString(),
selectDT.Rows[i][Int32.Parse(selectedCheckBox[1])].ToString()));
break;
case 3:
keys.Add(string.Join(", ", selectDT.Rows[i][Int32.Parse(selectedCheckBox[0])].ToString(),
selectDT.Rows[i][Int32.Parse(selectedCheckBox[1])].ToString(), selectDT.Rows[i][Int32.Parse(selectedCheckBox[2])].ToString()));
break;
case 4:
keys.Add(string.Join(", ", selectDT.Rows[i][Int32.Parse(selectedCheckBox[0])].ToString(),
selectDT.Rows[i][Int32.Parse(selectedCheckBox[1])].ToString(), selectDT.Rows[i][Int32.Parse(selectedCheckBox[2])].ToString(),
selectDT.Rows[i][Int32.Parse(selectedCheckBox[3])].ToString()));
break;
}
values.Add(Int32.Parse(selectDT.Rows[i][11].ToString()));
}
for (int i = 0; i < selectDT.Rows.Count; i++)
{
if (myDic.ContainsKey(keys[i]))
{
myDic[keys[i]] += values[i];
}
else
{
myDic[keys[i]] = values [i];
}
}
return myDic;
modified 13-Aug-21 13:50pm.
|
|
|
|
|
Hi,
I want to split this string:
"text 1, text 2, text 3, text 4"
I want the result to be like this:
"text 1"
"text 2"
"text 3"
I tried this code:
string.split(',').ToList();
The problem is that there is an extra space in list items:
"text 1"
" text 2"
" text 3"
How can I remove those extra spaces?
modified 11-Aug-21 5:21am.
|
|
|
|
|
You need to call Trim() on each item before saving it. something like:
List<string> words = source.Split(',').Select(s => s.Trim()).ToList();
|
|
|
|
|
You could always do
string.Split(", ", StringSplitOptions.None);
|
|
|
|
|
Are you sure?
Quote: I want to split this string:
"text 1, text 2, text 3, text 4"
I want the result to be like this:
"text 1"
"text 2"
"text 3"
Ignore me - I assumed it would treat the string as a char array and split on each letter ...
"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 this method that returns a List<jobstartdate>. Each start date can have 1-n revisions. So after I get the start dates I then loop through them all and get the revisions for each one.
My question is this... is it possible to somehow do this all in one query without having to loop and get the child records? Can I somehow select the joined records into entities in the same query as the start dates?
<pre>public List<JobStartDateEntity> GetJobStartDates(int jobId)
{
using (var dc = GetDataContext())
{
var results = (from jsd in dc.JobStartDates
where jsd.JobId == jobId
select new JobStartDateEntity
{
Id = jsd.Id,
StartDateId = jsd.StartDateId ?? 0,
JobId = jobId,
ProjectStartDateId = jsd.ProjectStartDateId,
Caption = jsd.Caption,
Description = jsd.Description,
Sequence = jsd.Sequence,
DaysOffset = jsd.DaysOffset,
StartDate = jsd.StartDate
}).ToList();
foreach (var result in results)
{
var startDateRevisions = (from sdr in dc.JobStartDateRevisions
where sdr.Id == result.Id
select new JobStartDateRevisionEntity
{
Id = sdr.Id,
JobId = sdr.JobId,
StartDate = sdr.StartDate,
Revision = sdr.Revision
}).OrderByDescending(x => x.Revision).ToList();
result.StartDateRevisions = startDateRevisions;
}
return results;
}
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Assuming you have a navigation property, you may be able to do something like this:
var jobStartDates = dc.JobStartDates.AsNoTracking().Include(jsd => jsd.Revisions).Where(jsd => jsd.JobId == jobId).ToList();
var results = jobStartDates.Select(jsd => new JobStartDateEntity
{
Id = jsd.Id,
...
StartDateRevisions = jsd.Revisions.Select(sdr => new JobStartDateRevisionEntry
{
Id = sdr.Id,
...
}).OrderByDescending(sdr => sdr.Revision).ToList(),
});
Alternatively, you can do it with two queries, rather than N+1 queries:
var results = (from jsd in dc.JobStartDates
where jsd.JobId == jobId
select new JobStartDateEntity
{
...
}).ToList();
var jobStartDateIds = results.Select(jsd => jsd.Id).ToList();
var revisions = dc.JobStartDateRevisions
.Where(sdr => jobStartDateIds.Contains(sdr.Id))
.Select(sdr => new JobStartDateRevisionEntity { ... })
.ToList();
forech (var result in results)
{
result.StartDateRevisions = revisions
.Where(sdr => sdr.Id == result.Id)
.OrderByDescending(sdr => sdr.Revision)
.ToList();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Without a navigation property, wouldn't a join be easier and faster?
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Hi,
I want to check Persian date in these formats:
yyyy/MM/dd ---- example: 1400/05/17
yyyy/M/d ---- example: 1400/5/17
yy/MM/dd ---- example: 00/05/17
yy/M/d ---- example: 00/5/17
The input date should be in one of those formats. I want to create True/False expression based on the input string.
Please guide me
|
|
|
|
|
|
I coded this class:
public string dateText { get; set; }
DateTime date;
string[] formats = {"yyyy/MM/dd", "yyyy/M/d", "yy/MM/dd", "yy/M/d" };
DateTime result;
public bool CheckFormat()
{
if (!DateTime.TryParseExact(dateText, formats, CultureInfo.InvariantCulture,DateTimeStyles.None, out result))
{
return false;
}
else
{
return true;
}
}
|
|
|
|
|
|
Nope, I don't get any.
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
If you want help, you need to explain what you have tried, where you are stuck, and what help you need. Otherwise, we can't work out what the heck you are talking about and really can't help you.
Typing as little as possible mean you don't get useful answers!
"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 have written a simple class for extracting numbers from a text:
using System;
namespace NumExtract
{
public class Extraction
{
public string text { get; set; }
string b;
public int value;
public int NumberExtract()
{
for (int i = 0; i < text.Length; i++)
{
if (char.IsDigit(text[i]))
{
b += text[i];
}
}
value = int.Parse(b);
return value;
}
}
}
When I use this dll file as a reference in my project, this error appears:
Quote: System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'
This error appears in Program.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.UserSkins.BonusSkins.Register();
Application.Run(new Form1());
}
How can I fix it?
|
|
|
|
|
|
I have made a dll class library and imported it to my references in visual studio. I can use its namespace but it causes runtime error during the compiling.
|
|
|
|
|
Finally, I found the problem. I tried to use C# class library (NetFrameWork) instead of C# class library core.
|
|
|
|
|