|
You need to understand variable scope. Since you're defining the variable inside the fi statement, they will only ever exist in that scope. No code outside of that scope will be able to see it.
Wrong:
if (PMorAM == "AM")
{
string string1 = "";
}
public void SomeMethod()
{
string string1 = "";
if (PMorAM == "AM")
{
string1 = "whatever";
}
}
If you want the variable to be visible to all code in the class, you have to define it at the class level.
public class SomeClass
{
private string1 = "";
public void SomeMethod()
{
if (PCorAM == "AM")
{
string1 = "whatever";
}
}
}
|
|
|
|
|
string1.Replace("1", "11");
... didn't work because .Replace "returns" a string; it doesn't "update" one.
string1 = string1.Replace("1", "11");
... would have gotten you further. Same applies to other "value" types like DateTime when it comes to adding days, etc.
"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
|
|
|
|
|
To add to Gerry's answer, the reason you can't just change a string in place is because of something called immutability. When a type is immutable (such as a string), this means that it cannot be changed. Instead, operations on an immutable type return a new instance of that type.
|
|
|
|
|
Is there a way to know if these 2 lines have the same size in pixels aka length? What code should I use to determine if they have the same length or not?
Just because these perpendicular lines created with ASCII, they differ in length size according to whether they were put next to one another or line by line under each other or not.
https://i.stack.imgur.com/gFgPx.png
|
|
|
|
|
Get a tape measure and measure your screen. Or use a screen ruler utility, such as the one included with Microsoft PowerToys[^].
Unless you have complete control over the end-user's system, you have no idea which font they're using for their console, so you have no way of knowing the actual width and height of text that you display on screen.
If you're looking for more advanced control over a command-line GUI, perhaps you should be using a toolkit? For example: Terminal.Gui: Cross Platform Terminal UI toolkit for .NET[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
To add to what Richard has said, most modern fonts are Proportional: older ones (like those used by DOS back in the day) are Non-proportional. In the latter all characters have the same width: 'i' is the same width as 'w':
iiiii
aaaaa
wwwww
In a Proportional font, they aren't:
iiiii
aaaaa
wwwww
And in modern fonts it gets really difficult to work out what the width is going to be, because text display systems also do something called "pair kerning" where they "slide" characters about if they can share space to improve the look of text: an uppercase A can "slide under" an uppercase "W" because the right hand side of each slopes the same way:
AWAWAW
But an uppercase H cannot:
HWHWHW
And then there is the point size, antialiasing, text effects, and the display pixel density, all of which can effect the final result of printing - for that you need the graphics context for the logical device you are printing the string onto! Oh, and two identical systems can be configured differently via a Zoom feature in Windows which makes text bigger or smaller to the user's preference and visual ability!
So to work out the length of a string as printed you need to know all that info, and pass it to the system to work out: there is a method to do that - Graphics.MeasureString Method (System.Drawing) | Microsoft Learn[^] - but be aware that it ... umm ... isn't too accurate under all circumstances. And there is also TextRenderer.MeasureText Method (System.Windows.Forms) | Microsoft Learn[^] which returns different numbers ...
And just to make life more complex, WPF adds its own drawing engine which will differ from both of those.
What I would suggest is to look at why you need to know the relative lengths and work around that, rather than assuming that it's going to be the same for all systems or even all applications - because it isn't!
"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 am trying to set Inheritance to disabled and to deny access to domain users on a directory (FullFolder). My code is:-
if (Directory.Exists(FullFolder))
{
DirectoryInfo di = new DirectoryInfo(FullFolder);
DirectorySecurity ds = di.GetAccessControl();
ds.SetAccessRuleProtection(true, true);
di.SetAccessControl(ds);
string DomainUsers = Environment.UserDomainName + @"\Users";
FileSystemAccessRule fsar = new FileSystemAccessRule(DomainUsers, FileSystemRights.FullControl, AccessControlType.Deny);
ds.AddAccessRule(fsar);
di.SetAccessControl(ds);
}
Disabled inheritance works fine.
However the line adding the AccessRule fsar give an error "System.Security.PrincipalNotMappedException"
Is the way I have set the user name wrong?
|
|
|
|
|
Shouldn't the group name be Domain Users rather than just Users ?
Try manually creating the rule on a folder, and then examining the ACL via code to see what the principal name looks like.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, yes looking at the ACE helped me see it.
|
|
|
|
|
I am using rdlc report and report viewer control.When i export the rdlc report to pdf the words written in hindi are not correctly displayed in the pdf.
|
|
|
|
|
Well, there's a secret error somewhere in your secret code then. You should fix that.
Seriously, how do you expect anyone to help you based on that description? The problem could be literally anything. You could be using a font that doesn't support Hindi characters. You could be storing or retrieving the data incorrectly. There could be a problem with the code that's exporting the report. We don't know, because you haven't told us anything other than "it's not working".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
Which is the best way to prevent this from throwing an exception, e.g. if it's null
(bool)(aDataTable.Rows[0]["aColumn"]))
Thanks
|
|
|
|
|
Take a look at Boolean.TryParse Method (System) method. That's what you are looking for.
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[ ^]
|
|
|
|
|
That is not conversion, it is casting, which is quite a different thing. The first thing to establish is what is the expected data in that column. If it is in fact some value that van be coerced to a bool type, then you should first check that it is not null . if it is some type that cannot be cast to bool then you need to look at exactly what that code is supposed to be doing.
|
|
|
|
|
If it is null, do you want it to be false or true?
Best way is to not have a null in your dataset.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I usually handle this in a very simple way with try and catch.
If it returns null, then use a default option (usually I use false), otherwise I use the value.
bool myRetValue = false;
try
{
myRetValue = (bool)(aDataTable.Rows[0]["aColumn"]));
}
catch
{}
//myRetValue variable holds the value of the item desired.
RGA
|
|
|
|
|
This is a terrible suggestion. You're not supposed to use exceptions to drive the flow of control. Also, doing this hides what could be a variety of exceptions that that code could cause.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Although both of the listBoxes **AllowDrop=true** listBox2 does not allow me to drop an item from listbox1 into listBox2.
VS 2022 does not give any error, warning or exception handling problem. The problem is that this code does not do what it supposed to do. It does not let me carry 1 item from listBox1 to listBox2.
C#:
namespace LISTBOX_fareileSURUKLEbirakDRAGDROP_Olaylari
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.AllowDrop = true;
listBox2.AllowDrop = true;
int i;
for(i = 0; i < 10; i++)
{
listBox1.Items.Add(i);
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count == 0) return;
string deger = listBox1.Items[listBox1.IndexFromPoint(e.X,e.Y)].ToString();
if (DoDragDrop(deger, DragDropEffects.All) == DragDropEffects.All)
listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
}
private void listBox2_DragOver(object sender,DragEventArgs e)
{
e.Effect= DragDropEffects.All;
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
listBox2.Items.Add(e.Data.GetData(DataFormats.StringFormat));
}
}
}
What do you think is the problem here?
modified 7-Oct-22 7:40am.
|
|
|
|
|
Probably, it's not a string.
Use the debugger to check the e.Data value and see exactly what is going on.
"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!
|
|
|
|
|
This simple code gives Exception Handling Error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Label_LinkLabel_TextboxKONTROLLERI
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
LinkLabel dynamicLinkLabel = new LinkLabel();
private void Form2_Load(object sender, EventArgs e)
{
dynamicLinkLabel.BackColor = Color.Red;
dynamicLinkLabel.ForeColor = Color.Blue;
dynamicLinkLabel.Text = "I am a Dynamic LinkLabel";
dynamicLinkLabel.TextAlign = (ContentAlignment)HorizontalAlignment.Center;
dynamicLinkLabel.Text += " Appended text";
dynamicLinkLabel.Name = "DynamicLinkLabel";
dynamicLinkLabel.Font = new Font("Georgia", 16);
Controls.Add(dynamicLinkLabel);
dynamicLinkLabel.Name = "DynamicLinkLabel";
string name = dynamicLinkLabel.Name;
dynamicLinkLabel.Location = new Point(20, 150);
dynamicLinkLabel.Height = 40;
dynamicLinkLabel.Width = 300;
dynamicLinkLabel.BackColor = Color.Red;
dynamicLinkLabel.ForeColor = Color.Blue;
dynamicLinkLabel.BorderStyle = BorderStyle.FixedSingle;
dynamicLinkLabel.ActiveLinkColor = Color.Orange;
dynamicLinkLabel.VisitedLinkColor = Color.Green;
dynamicLinkLabel.LinkColor = Color.RoyalBlue;
dynamicLinkLabel.DisabledLinkColor = Color.Gray;
dynamicLinkLabel.LinkArea = new LinkArea(0, 22);
dynamicLinkLabel.Links.Add(24, 9, "http://www.c-sharpcorner.com");
dynamicLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(LinkedLabelClicked);
}
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
dynamicLinkLabel.LinkVisited = true;
System.Diagnostics.Process.Start("http://www.c-sharpcorner.com");
}
}
}
The code above has been taken from this website: https:
Here is the exception:
System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'http:
Here is the CS8604 Warning: https:
I did not understand what causes this. How can I solve this problem?
|
|
|
|
|
A URL is not a process. Look at the documentation Process.Start Method (System.Diagnostics) | Microsoft Learn[^]
And if code from another site is "broken" then you should really be posting the question on the site you got it from
Edit: Assuming the code works for the original author, then I can only assume he has some default set so that his preferred browser automatically opens but you do not. Try using the name of your favourite browser as the process name, passing that URL as a parameter
|
|
|
|
|
Your errors suggest you're using .NET Core or .NET 5, 6, or 7. Unlike in the .NET Framework, .NET Core and .NET do not set the UseShellExecute to true by default - you need to specify it explicitly:
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
dynamicLinkLabel.LinkVisited = true;
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "http://www.c-sharpcorner.com",
UseShellExecute = true,
});
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Is there a free licensing solution for .NET?
It can be hosted on the cloud or self-hosted.
Requirements: works with C# and unlimited licenses.
Can be hosted anywhere, except in the app or in a website (Only the app can access the product keys)
Thanks!
|
|
|
|
|
Why would someone not charge if it had all those features?
This is simply Google legwork and has nothing to do with "programming in C#".
"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
|
|
|
|
|
Cloud-hosted will always cost money because cloud doesn't come free.
You host find a solution that you can host on your own servers, but that's not going to be free either. Power to run servers and ISP costs money.
"Can be hosted anywhere, except in the app or in a website", well, where the hell else are you going to host the servers that hold the licenses?
Basically, a "free" license server doesn't exist.
|
|
|
|