|
I conducted some more rigorous experiments. This explanation doesn't match the experimental evidence.
I now feel somewhat certain, based on evidence from a million randomly generated numbers, that there is something uniquely, and inexplicably, terrible about float ->double conversions.
I qualify it with "somewhat", because I refuse to be completely wrong, about the same thing, twice in a single day
|
|
|
|
|
This discussion from 2011 looks relevant:
What you get in the more precise representation (past a certain point) is just garbage. If you were to cast it back to a float FROM a double, you would have the exact same precision as you did before.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So i have a bunch of files in a folder inside of my project. I need all the names of those files to go in an array so i can choose a random one with a randol index. I searched already on google but i could only find solutions for when you are trying to reach a directory that is not inside of the current project.
|
|
|
|
|
|
Perhaps I want something that is impossible... but thought I would ask anyway because I cannot find a way to do this... and it seems like it should be possible.
I want to force the definition of a method in all subclasses without that method being virtual. (I don't want a virtual table and lookup to be necessary). Normally one would do this with an interface. The problem is that I also need to define a property with an explicit definition. Normally that is done with an abstract class (which is the only reason I need to define one) An example is illustrated below. The problem I am running into is that if I inherit the interface in the abstract class, none of the sub-classes require the "NeededFunction" to be implemented, which is counter to what I want. If I don't implement the method in the abstract class however, it gives me an error. Is there a way to do this... basically I want the derived classes to implement the "NeededFunction" rather than the abstract class (without them being virtual for performance reasons). I know I can have every derived class implement the interface instead but that seems needlessly redundant.
interface IMyFunctionInterface
{
void NeededFunction(object state);
}
internal abstract class MyBaseImplementation : IMyFunctionInterface
{
internal MyRelevantProperty { get; protected set; }
public void NeededFunction(object state)
{
}
}
internal class Implementation1 : MyBaseImplementation
{
public void NeededFunction(object state)...
}
modified 27-Feb-19 13:05pm.
|
|
|
|
|
AFAIK, the only way to force the derived class to implement the method is to mark it as abstract . However, that would still be a virtual method.
internal abstract class MyBaseImplementation : IMyFunctionInterface
{
internal Foo MyRelevantProperty { get; protected set; }
public abstract void NeededFunction(object state);
}
internal class Implementation1: MyBaseImplementation
{
public sealed override void NeededFunction(object state) { ... }
}
If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.
pr1mem0ver wrote: without them being virtual for performance reasons
Do you have any real performance data from your application to justify that requirement? Particularly since method calls on an interface will always be virtual anyway...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: If your base class doesn't include an interface member, then it doesn't implement the interface. All of the derived classes would need to implement the interface instead.
Yeah... I have seen this suggestion elsewhere (older posts) but I get an error that the abstract class doesn't implement the method required for the interface. Is this error specific to newer versions of .NET?
|
|
|
|
|
The "doesn't implement the method" error appears when you declare that your class implements the interface, but it doesn't provide a declaration for one or more members defined by the interface.
This has always been the case since .NET 1.0, and has not changed with newer versions.
There will be a semi-related change with C# 8, which will only be available to .NET Core projects. But not in the way you've described:
C# 8: Default Interface Methods Implementation - CodeJourney.net[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You want to "force" a method override, but do not want to implement the interface ... because it "seems redundant".
How can it be "redundant" if you want to "force" the implementation of the method? You still need to add the method to the derived class and "know" if you didn't; so which part is "redundant"?
In any event, you can use "new" to override a non-virtual method and complain about it using reflection.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
You are not following what I mean by redundant. I mean redundant in that I have to add the Interface to every single derived class rather than just the base class.
You also don't seem to get that part of the point of doing things this way is because conceptually it is more robust. This is one use of CRTP's in C++.
|
|
|
|
|
Hello,
my name is Nikol, and currently, I am learning to code with C#. I am solving a problem, and I am getting stuck on this step. I read a sequence of numbers and a special "bomb" number and its power. For example if the sequence is:
{1, 2, 2, 4, 2, 2, 2, 9} with "bomb" number 4 and power: 3
we have to find the index of the bomb number (3) and remove 'power' count numbers from its left (remove 3 numbers from its left). We should receive the sequence:
{2, 2, 2, 9}.
Can you see my code and give me some advice? Are all cases covered? The else which I added is for the cases when the power is bigger than the numbers we have to its left. Btw, I set the elements to 0 because at the end I need the sum of the elements in the list and it doesn't matter. Thank you in advance! https://pastebin.com/mPeg5Xku
|
|
|
|
|
Some points to consider:
- You don't display any instructions to the user. Will they know what they're expected to type in?
- What happens if the user types in something which isn't an integer? Eg:
1 2 wibble - What happens if the user only types a single number on the second line?
- You're skipping the last number in the list (
i < numbers.Count - 1 ). What if that's the "bomb" number? - You're not actually removing the numbers from the list; you're just setting them to
0 . Depending on the wording of the assignment, that may not be correct. - Your
if..else block has almost identical code. The only difference is the starting value of j . Can you combine the branches?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I export my data from a DGV and align them with a space delimiter and it is working good for me. i also export the headers before the cells values. the thing that i want to do but i can not is aligning and adjusting the cells with headers to see them as a table that its members are separated by a delimiter (here space). my means is i want to see cells values right below the headers (if they have smaller or equal lengths) and headers text right on the top of the cells values if header length is less than cells values lengths), in the other word, Keeping cells values and their related headers together.(i am using Microsoft.Office.Interop.Word.)
thanks in advance.
What i have been tried:
int RowsCount = DataTable.Rows.Count;
int ColumnsCount = DataTable.Columns.Count;
var newline = System.Environment.NewLine;
var delimiter = " ";
var copied= new StringBuilder();
List<int> clmnLocation = new List<int>() {};
string header = "";
for (int c = 0; c < DataTable.Columns.Count; c++)
{
header = header + Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ", "") + delimiter;
clmnLocation.Add(header.Length);
}
for (int c = 0; c < DataTable.Columns.Count; c++) copied.Append(Convert.ToString(DataTable.Columns[c].ColumnName).Replace(" ","") + delimiter);
clipboard_string.Append(newline);
for (int i = 0; i <RowsCount; i++)
{
for (int j = 0; j <ColumnsCount; j++)
{
if (j > 0)
copied.Append(DataTable.Rows[i][j].ToString().PadRight(clmnLocation[j]) + delimiter);
else
copied.Append(DataTable.Rows[i][j].ToString() + delimiter);
}
copied.Append(newline);
}
WordDoc.Content.Text = copied.ToString();
|
|
|
|
|
If you want "headers" aligned in a (mono-spaced) text file, you will first need to determine the "maximum length / width" of each value in each column (plus a space for a column separator, if not "tabbing").
Besides aligning the headers, you will also need to align the columns.
Easier to just open in Excel as a CSV if you want to "look" at it.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
|
I am trying to set the caret at the end of a textbox so it scrolls up as new lines get added. These are added by:
this.textBox1.Text += ThisText + System.Environment.NewLine;
However even though the value of this.textBox1.SelectionStart is correct (i.e. a large number), when I get to these lines:-
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.Refresh();
The textbox doesn't scroll.
Can someone tell me what I am missing?
|
|
|
|
|
As easy way to do this is to just call
textBox1.AppendText(ThisText + Environment.NewLine);
Stop putting this on everything. You don't need it as it's already implied.
|
|
|
|
|
|
Dave Kreskowiak wrote: Stop putting this on everything. That was my idea to remind him where the control lives (he was creating a new form and lost the plot)
Implied can be confusing the this newbie, thought it might help.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
|
This is a swamp I have waded in, before.
The cheap solution: put code to update the TextBox caret in the Form's 'Shown event handler.
But, if you want to make the TextBox as independent as possible of its container, and, assuming:
1. this is a WinForm app with a TextBox set to show a vertical scrollbar
2. you will set the Text property at design time, or in the 'Load event of its container.
3. when the Form is shown, you want the TextBox scrolled to the end, and the TextBox selected/focused so if you start typing, what you type is added to the TextBox:
using System;
using System.Windows.Forms;
namespace YOURNAMESPACE
{
public partial class AutoScrolledTbx : UserControl
{
public AutoScrolledTbx()
{
InitializeComponent();
}
public AutoScrolledTbx(string txt)
{
textBox1.Text = txt;
this.textBox1.Select(this.textBox1.TextLength + 1, 0);
this.textBox1.ScrollToCaret();
this.textBox1.Capture = true;
this.textBox1.Focus();
}
private void AutoScrolledTbx_Load(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)this.ForceScroll);
}
private void ForceScroll()
{
this.Invoke(
(MethodInvoker)delegate
{
this.textBox1.Select(this.textBox1.TextLength + 1, 0);
this.textBox1.ScrollToCaret();
this.textBox1.Focus();
ActiveControl = this.textBox1;
}
);
}
public void AppendText(string txt)
{
this.textBox1.AppendText(txt);
}
}
} Discussion:
When a Form is being 'Loaded, then displayed, Controls may not appear in an expected state. By pushing a task onto the UI thread using Marc Gravell's technique shown here, you have achieved the equivalent of executing the update code in the Form's 'Shown event.
In case you wonder why a UserControl is used here, rather than a sub-classed TextBox: a TextBox offers no 'Load or other initialization event to manipulate.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 24-Feb-19 10:28am.
|
|
|
|
|
Hi experts, I am a student and trying to learn to display bitcoin and cryptocurrency price via an API from CoinGecko API[^]
How do I accomplish that in C#? Is Net Http or some library where i should be starting?
Thanks you
|
|
|
|
|
Please don't repost if your message does not appear immediately - both of these were sent by the automated system to moderation as "possible spam" and required a human decision to "let through" or "ban the spammy poster".
When you post multiple times, it increases the chances that you are taken as a spammer, and means that I have to chase down the duplicates and tidy up after you.
I've deleted the "spare".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Go to that page, scroll down, download the .NET client as an example.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I am trying to convert a piece of PHP cURL code to C# using the WebClient but I can't figure out how to implement the CurlFile parameter from the array.
Here is the original PHP code:
$post_data = array('password'=>'pswd',
'username'=>'usr',
'textfile'=>new CURLFile('C:\data_stats.txt'),
);
$curl_obj = curl_init('https://www.website.com');
curl_setopt($curl_obj, CURLOPT_POST, true);
curl_setopt($curl_obj, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl_obj);
And there is the C# code I came up with (I managed to send the password and username parameters but I don't know how to send the file with the key 'textfile' along with those parameters)
using (var client = new WebClient())
{
var post_data = new NameValueCollection();
post_data["password"] = "pswd";
post_data["username"] = "usr";
var response = client.UploadValues("www.website.com", post_data);
}
|
|
|
|