|
study these: [^] [^] ... and ask yourself what numeric types it makes sense to convert the values you describe ... to.
then study this: [^]
and ... start experimenting !
come back with specific questions.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Thank you so much.
Any have created my own class for converting any base to any base including decimal places (Ex:20.1234).
Will share it.
|
|
|
|
|
BillWoodruff wrote: ask yourself what numeric types it makes sense to convert the values you describe Why wouldn't it make sense? If b >= 10, you obviously have to define 'digits' for 10+; using A-Z for 10-36 is quite common. Equally obvious: The result of conversion must be treated as a digit string; there is no other way to handle, say, a base 13 number in a binary computer.
In decimal, the digits to the left of the decimal point give the number of ones, then number of tens, then ten**2s, ten**3s, ten**4s, ... To the right of the decimal point digits give the number of 1/10s, then the number of 1/(10**2)s, 1/(10**3)s, 1/(10**4)s, ...
For base b, the digits to the left of the point give the number of ones, then number of bs, then b**2s, b**3s, b**4s, ... To the right of the point, digits give the number of 1/bs, then the number of 1/(b**2)s, 1/(b**3)s, 1/(b**4)s, ...
I guess it would be somewhat confusing to call it a decimal point, though.
"Octal is just like decimal ... if you are missing two fingers" (Tom Lehrer)
To the OP: I would have split the number on the point, treating the whole and the fractional parts separately, converting then to binary integers, and then iterated each over a divide/remainder down to zero. Note that for the fractional part, you must set a reasonable limit for the number of digits - in, say, base 13 you cannot represent 879/1000 (that is from your first example) as any finite series of fractional digits f1/13 + f2/169 + f3/2197 + ... + fn/(13**n), except for a few special cases. (That goes for decimal .879 or .345 as well; they are not represented by those exact values in double format.)
|
|
|
|
|
to the flow of such off-topic ramblings, one can only hope the kaopectate of silence will ... help.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 25-Dec-22 11:06am.
|
|
|
|
|
Thank you all.
I have created my own class for number conversion from one base to another base including decimal places.
Will share it if any body wants it.
I am GouseSakhalean, having 17 years of software development experience.
|
|
|
|
|
Namaskara, "17 years in software development"
Then, surely, you are aware that a deep understanding of numeric types in a strongly-typed language, like C#, is essential to competence. In C#, understanding when you can cast, and when you must invoke a conversion method is essential.
"rahi gulzar to phool khilenge" Kabir
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Sakhalean wrote: I am GouseSakhalean, having 17 years of software development experience. OK, but then I am surprised that you ask for help with this problem. I consider it to be quite trivial, like an exercise you would be given early in your studies.
Furthermore, I can see no practical use for it - except to make a student really look into floating point formats, and to understand the concept of an arbitrary base. In which application do you need to express a double in an arbitrary base?
So both the problem and your questioning made it look like some student asking for help to do his homework assignment. I am getting curious to know which real world application (rather than a student exercise) has a need for the conversion function you have described.
|
|
|
|
|
This is for Aerospcase domain project purpose. For Aircraft project where we need for Aircraft parameters to be converted into Deciamal, Octal, hex , binary and engineerig formats.
|
|
|
|
|
I have an old Linq-To-Sql DAL that I'm converting to a EF Core 6. I'm using the Repostory pattern.
I've looked at this article and others like it, and the all seem to do it the same way...
Interface
public interface IRepository<T> where T : class
{
void Add(T entity);
void AddRange(IEnumerable<T> entities);
void Delete(T entity);
void DeleteRange(IEnumerable<T> entities);
T Get(int id);
IEnumerable<T> Get(Expression<Func<T, bool>> expression);
IEnumerable<T> GetAll();
}
Base Repository
public class BaseRepository<T> : IRepository<T> where T : class
{
protected readonly DbContext _context;
public BaseRepository(DbContext context)
{
_context = context;
}
}
Customer Repository
public class CustomerRepository : BaseRepository<CustomerEntity>, ICustomerRepository
{
public CustomerRepository(DbContext context)
:base(context)
{
}
public bool IsCustomerActive(int id)
{
return true;
}
}
Usage
public class CustomerController : ControllerBase
{
public bool CheckIfCustomerIsActive(int id)
{
var connString = "";
var dc = new SqlDataContext(connString);
ICustomerRepository repository = new CustomerRepository(dc);
return repository.IsCustomerActive(id);
}
}
Issues I See
Everything about this seems clean and maintainable, except
1. The Connnection String is in the controller. I see this in a lot of examples.
2. By design, the controller must create and pass the data context to the repos.
It seems to me that these 2 issues create a tight coupling between the API and the DAL.Why would a controller ever need to know the conn string OR DBContext? Those are both specific to the DAL.
Why not do this instead?
public class BaseRepository<T> : IRepository<T> where T : class
{
protected readonly DbContext _context;
public BaseRepository()
{
var connString = Properties.Settings.Default.ConnectionString;
_context = new SqlDataContext(connString);
}
}
Doing it this way, the controllers could use DI to get the repos they need, and the controllers never need to know about the connection string or the DBContext. I could swap out the entire data layer without ever opening a controller.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 23-Dec-22 15:29pm.
|
|
|
|
|
Are you using Dependency Injection? Ideally, the DbContext and repository classes should be registered as "scoped" dependencies. The context would be injected into the repository, and the repository would be injected into the controller.
The connection string would usually be read from the web.config / appSettings.json file by convention:
Connection strings and models - EF6 | Microsoft Learn[^]
If you need to manage the connection string differently, then you should be able to add it to the DI registration. For example, using the Microsoft DI framework:
services.AddScoped<DbContext>(_ =>
{
string connString = Properties.Settings.Default.ConnectionString;
return new SqlDataContext(connString);
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Are you using Dependency Injection? Ideally, the DbContext and repository classes should be registered as "scoped" dependencies. The context would be injected into the repository, and the repository would be injected into the controller.
Yes, that's exactly what I was thinking. Many of the examples you find out there show the DbContext referenced in the controller.
Richard Deeming wrote: f you need to manage the connection string differently,
It seems to me that the ConnectionString belongs in the Repository project. The Controller get the repo, and the repo knows how to contact the DB.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: It seems to me that the ConnectionString belongs in the Repository project.
The connection string shouldn't be hard-coded anywhere. It belongs in the configuration settings of the application - in this case, the web site.
For .NET Framework, that would usually be the <connectionStrings> section of the web.config or YourApp.exe.config file.
For .NET Core/5/6/7/..., that usually would be the "ConnectionStrings" section of the appSettings.json file, although you could use any custom section you want. The .NET configuration system is much more flexible, so you could also choose to store the connection string in any of the configuration sources your application uses - environment variables, Azure secrets, etc.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: The connection string shouldn't be hard-coded anywhere. It belongs in the configuration settings of the application - in this case, the web site.
I never said hard coded. I said it belongs in the Repository project, in the app.config for THAT project. Why would the web site nedd to know it? As far as the web site knows, it simply gets a repo injected into it. The Repo gets the connstring from its app.config.
This was my question all along. I don't get why the controller knows anything about the DB.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Class libraries don't have a separate configuration file. They get their configuration from the application that uses them - in this case, the web site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I a'm reading values from raspberry pi mysql database with visual studio c#
If the database is not available and I am trying to read from it, the c# app
freezes for about 15 seconds.
Is it possible to make it not to freeze when checking if the database is available?
Her is the code
<pre lang="C#">string connString = "SERVER='192.168.86.41';DATABASE='spaceinformation';UID='******';PASSWORD='******'";
private void FormMeasurement_Load(object sender, EventArgs e)
{
try
{
using (var connection = new MySqlConnection(connString))
{
connection.Open();
query = "select * from weather";
using (var command = new MySqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
listBoxMeasurement.Items.Add(string.Format("Temperature: {0} Humidity: {1} Date: {2}", reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));
}
}
}
connection.Close();
}
}
catch
{
MessageBox.Show("Database is not available for moment!");
listBoxMeasurement.Enabled = false;
}
}
|
|
|
|
|
Having used neither raspberry pi and/or mysql EVER, I might be chasing down a pot-o-fool's gold here (Raspberry Pi? That's expired pharmacy my friend) but I see THAT connString and always say to myself "config issue/networking port" issue.
I might open up Task Manager and set the display to anything that resembles the realtime CPU use, probably where there's a network monitoring graph that one can set the sample speed FASTER, then go through the motions which cause this "freeze" ... and see if the two things align in time.
Come to think of it, VS (C#? Unlikely slow (blazes fast eh?)) ... leaves frozen footprints sometimes when running debug. Now, there's an idea!
Run debug while under VS gravitational pull.
|
|
|
|
|
That "freeze" is the connection object giving the server time to respond to the connection request. When that times out, that's when you get the exception message.
What do you do about that? Set the Connect Timeout[^] in the connection string.
|
|
|
|
|
Assuming the MySql connector supports it, you could try using an async method:
private void FormMeasurement_Load(object sender, EventArgs e)
{
listBoxMeasurement.Enabled = false;
_ = LoadFormAsync();
}
private async Task LoadFormAsync()
{
try
{
using (var connection = new MySqlConnection(connString))
{
await connection.OpenAsync();
const string query = "select * from weather";
using (var command = new MySqlCommand(query, connection))
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
listBoxMeasurement.Items.Add(string.Format("Temperature: {0} Humidity: {1} Date: {2}",
reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));
}
}
}
listBoxMeasurement.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("Database is not available for moment!\n" + ex.Message);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
When debugging a multi-threaded process, is there any way to make Visual Studio step through only one of the threads at a time, so that when you're stepping, it's not jumping from thread to thread? The jumping from one thread to another makes following one specific thread almost impossible.
I've tried the thread flagging feature, but one must still keep choosing the desired thread from the dropdown every time it jumps.
Is there any help from VS to focus on just one thread when stepping?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Seems one would fully test with one thread; then employ multiple threads. At that point, trying to isolate a single thread would seem redundant. When in doubt "lock" critical code and / or use concurrent objects.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Well when one is employing BackgroundWorker threads as part of the basic design, I don't see how it's possible to test that code with only one thread.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The backgroundWorker (BGW) is one thread. I have used multiple BGW workers. Some doing the same work; some different. I often use a wrapper for each BGW I launch; so in effect they have their own scope; maybe that's the difference.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Hi,
You can use the "Threads" window while single-stepping and right-click to Freeze/Suspend all the other threads. The "Parallel Watch" window also has this feature.
|
|
|
|
|
Bingo! I suspected there had to be a way. Thanks!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No problem,
Debugging is a skill that everyone should learn.
Btw, Visual Studio sucks for debugging, spend a few months (few hundred hours) using WinDbg and you'll never need to ask questions in an internet forum.
|
|
|
|
|