|
Good grief, you don't let the chauffeur drive the Jag old bean.
This space for rent
|
|
|
|
|
Hi:
How to define a recursive lambda function using C#?
By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles = Odd(a, (double x) => x + 2.0);
Any ideas?
Ieshua Carroll
Systems Engineer
|
|
|
|
|
|
It is an useful link. Thanks !
|
|
|
|
|
yw
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
However one should read the following and understand it.
StackOverflowException Class (System)[^]
Especially understand that it will terminate the AppDomain absolutely.
And read the following...
"if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant. "
|
|
|
|
|
If you're using a recent version of the C# compiler, you might want to consider local functions:
Local functions (C# Programming Guide) | Microsoft Docs[^]
Using the function from the blog post that Eddy linked to, it works almost without change:
int Fib(int n) => n > 1 ? Fib(n - 1) + Fib(n - 2) : n;
Console.WriteLine(Fib(6));
(Of course, the best solution for calculating Fibonacci numbers is to avoid recursion. )
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,savers.
i can move my row but after binding it will back to the previous state. how can i update it to my database e.g the second row become third row in both datagridview and database and also the ID column become update (resort assending from 1 to the end and 2become3 and 3become2) in my database(i can update that in datagridview by a foreach loop).
please help me.....
please guide me by your codes .
thank you in advance.
|
|
|
|
|
Add a column to the table and specify a value to determine the order, or create an index. Don't try to "order" rows in a database-table. A database is not meant for direct viewing, and the order in which it is stored is not related to the way it is shown.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thank you so much for your answer.
i have to sort them in my database because some calculation will perform in the DBS that data sorting and ID Numbers are very important.
thank you any way to care about my question.
i'll be grateful if you add your comments if anything else ocured to your mind.
|
|
|
|
|
Member 13325846 wrote: i have to sort them in my database Rows in a database have no particular order. You can specify the order when fetching the rows, or if you want to save a user-defined "order", you add that column. Don't muck with the order that the rows are saved in.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
thank you for you guide my friend.
best wishes.
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table.
thank you in advance.
here is my code:
OleDBConnectio con=new OleDBConnection(conectionString);
con.Open();
OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con);
OleDBDataReader dr;
dr=com.ExecuteReader();
While(dr.read())
{
Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString());
}
dr.Close();
con.Close();
i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically.
thanks a lot.
|
|
|
|
|
Dgv1.Rows.Clear();
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi, thank you legitimate programmer.
thanks a million for your help
|
|
|
|
|
Member 13325846 wrote: new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)
That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name.
Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have Id and Name columns.
It would also be better to only load the specific columns you need, rather than using SELECT * FROM ...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
Thank You For your care.
Yes your right and this was just an example and I am using from a cboBox.
Thanks a million
|
|
|
|
|
Good idea ? Bad idea ? Would you ever use in your own code ? Code smells ?
A recent thread here [^] asked for help with (one-way) binding a group of TextBoxes to integer valus in an Array. I responded with an example of using 'ObservableCollection.
The first version I hard-coded (for POC) to match the thread cited's requirement; then, I had an idea that with such a simple one-way binding scenario a Class inheriting from List<T> ... where the generic parameter represented the Type of objects to be synced ...with a custom indexer ... could work for similar situations.
The next version, I made objects to be synced to the array generic, and put List value change code in a Func template to do the work of whatever a change in the Array/List values required. Here's the code:
public class TUpDater<T> : List<int>
{
public TUpDater(Func<int, int, T, bool> mapfunc, params T [] syncobjects)
{
MapFunc = mapfunc;
SyncObjects = new List<T>();
for (int i = 0; i < syncobjects.Length; i++)
{
SyncObjects.Add(syncobjects[i]);
}
}
public Func<int, int, T, bool> MapFunc { set; get; }
public List<T> SyncObjects { get; }
public new int this[int ndx]
{
set
{
base[ndx] = value;
if (MapFunc?.Invoke(ndx, value, SyncObjects[ndx]) != null)
{
}
}
get
{
return base[ndx];
}
}
} Here's a usage example:
TUpDater<TextBox> tba;
List<TextBox> TBoxes = new List<TextBox>();
List<int> Ints { set; get; }
private void Form1_Load(object sender, System.EventArgs e)
{
tba = new TUpDater<TextBox>(
(ndx, val, tbx) =>
{
tbx.Text = val.ToString();
return true;
}
);
Ints = new List<int> { 11, 22, 33, 44, 55 };
tba.AddRange(Ints);
for (int i = 0; i < 5; i++)
{
TextBox tb = new TextBox { Name = "Ints" + (i + 10), Text = i.ToString() };
tba.SyncObjects.Add(tb);
}
tba[3] = 1000;
var xxx = tba[3];
} Comment: if I take this further, I think I'd provide an optional CTor overload where the user could pass both List Int values, and T objects.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
I don't think it makes sense to inherit from List<T> . It would be almost impossible to ensure that the SyncObjects list had the same number of items as the outer list.
I was also slightly confused by the value stored in the list, until I realised it was meant to be the value of the control at the specified index. It would probably be better to make that a generic type parameter as well.
Perhaps something like this?
public class ControlUpdater<TControl, TValue> : IReadOnlyList<TValue>
{
private readonly List<(TControl control, TValue value)> _items = new List<(TControl, TValue)>();
private readonly Func<TControl, TValue, bool> _setValue;
public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<(TControl, TValue)> items)
{
_setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
if (items != null) AddRange(items);
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, params (TControl, TValue)[] items) : this(setValue, items.AsEnumerable())
{
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, IEnumerable<TControl> items)
{
_setValue = setValue ?? throw new ArgumentNullException(nameof(setValue));
if (items != null) AddRange(items);
}
public ControlUpdater(Func<TControl, TValue, bool> setValue, params TControl[] items) : this(setValue, items.AsEnumerable())
{
}
public ControlUpdater(Func<TControl, TValue, bool> setValue) : this(setValue, default(IEnumerable<(TControl, TValue)>))
{
}
public void AddRange(IEnumerable<(TControl, TValue)> items) => _items.AddRange(items);
public void AddRange(IEnumerable<TControl> items, TValue value = default(TValue)) => _items.AddRange(items.Select(c => (c, value)));
public void Add((TControl, TValue) item) => _items.Add(item);
public void Add(TControl control, TValue value = default(TValue)) => _items.Add((control, value));
public void Insert(int index, (TControl, TValue) item) => _items.Insert(index, item);
public void Insert(int index, TControl control, TValue value = default(TValue)) => _items.Insert(index, (control, value));
public void RemoveAt(int index) => _items.RemoveAt(index);
public int Count => _items.Count;
public TValue this[int index]
{
get
{
var (_, value) = _items[index];
return value;
}
set
{
var (control, _) = _items[index];
if (_setValue(control, value))
{
_items[index] = (control, value);
}
}
}
public IEnumerator<TValue> GetEnumerator()
{
foreach (var (_, value) in _items)
{
yield return value;
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
} Usage:
var tba = new ControlUpdater<TextBox, int>(
(tbx, value) =>
{
tbx.Text = value.ToString();
return true;
});
var controls = Enumerable.Range(0, 5).Select(i => new TextBox { Name = $"Ints{i + 10}" });
tba.AddRange(controls);
tba[3] = 1000;
var xxx = tba[3];
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard ! I look forward to studying your code.
The issue of guaranteeing one T for each Index is also on my mind: they, imho, should be tightly coupled.
In another version of this, I made the 'CTor accept a Param array Type of KeyValuePair<T, int>. With a KeyValuePair, of course, if you change an 'Int value, you get a copy back, and have to re-assign the copy: there goes read-only !
I am aware there are some implementations of an 'ObservableDictionary out there (including one in MS Microsoft ParallelExtensionsExtras) that might come in handy. was also slightly confused by the value stored in the list, until I realised it was meant to be the value of the control at the specified index. It would probably be better to make that a generic type parameter as well. Agreed: it's funny how my mind kept needing to be refreshed about how that worked
I struggle to evaluate the utility of a code strategy like this; part of me says it's too clever, or, of very limited use. I think of a bright student: wouldn't it be better to teach them how to implement INotify ... or use Observable Collection ... in terms of them learning a more generalizable method ?
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: Good idea ? Bad idea ? Under what circumstance, and for whom?
It's a good idea at least to post it, as it is always good to learn alternative ways of doing things.
Based on the title I'd compare it to classical databinding; a oneliner that doesn't generate the UI (textBox1.Bindings.Add ). This example feels more like a PropertyEditor for (int) collections - something that may make prototyping a bit quicker
What was the question again? Code-review; you don't want that. It is a short example meant to demonstrate an idea, not something to nit-pick at
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
I linked to the thread here that "spawned" this post in the first paragraph of this post. Oh, I do want a code review ... I can only benefit from whatever my peers, and mentors, here, say !Eddy Vluggen wrote: I'd compare it to classical databinding; a oneliner that doesn't generate the UI (textBox1.Bindings.Add Good point: here the issue is binding a group of TextBoxes' 'Text property to elements in a List of Int32. Can you post a short example of doing this ? My bias against using WinForm databinding goes way back, and I'd be happy to get rid of it.
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
BillWoodruff wrote: Oh, I do want a code review ... I can only benefit from whatever my peers, and mentors, here, say ! Too bad you got a reaction of me instead
The boolean in the mapfunc is for obvious for future use, but TBoxes is an abbreviation (hope I spelled that right). Since it is a private property, I wonder whether it should be a private field, an internal/public property, or a read-only property.
BillWoodruff wrote: here the issue is binding a group of TextBoxes' 'Text property to elements in a List of Int32. Can you post a short example of doing this ? A textbox is for text; for an int I prefer a numericUpDown-control.
for (int i=0; i<myIntList.Count)
myNumericUpDownList[i].DataBindings.Add("Text", myIntList[i], "Port");
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Appreciate your comments !Eddy Vluggen wrote: TBoxes is an abbreviation (hope I spelled that right). Since it is a private property, I wonder whether it should be a private field, an internal/public property, or a read-only property. The goal is to keep that list of TextBoxes local, unchanged. In fact, in the code for the Class I use a read-only Property: public List<T> SyncObjects { get; } to store the TextBoxes. In the usage example the property is private, but, yes, it would be better to make that read-only.
Re: your code example: what does "Port" refer to ? Mapping a Property of one Control to another in WinForm is one of the simpler databinding tasks, but, in this case the tricky part is that the List elements change, not the Control ... am I missing the point ?
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|