|
It has to do with variable scope. You are passing by Reference which is different than passing by value. You are basically allowing the function ABC to modify the actual variable i.
For this to act as you expect you need to assign the return value from ABC to i like this: i = abc(i) and remove the Ref keyword. There is a little more to it but see below.
Look here at a StackOverflow answer[^] and here at the Microsoft Ref keyword.[^]
Jack of all trades, master of none, though often times better than master of one.
|
|
|
|
|
Thank you so much for your reply. But my question is that when i write statement "return j", it is returning value of j. This part I got but when I write "return 0", why it is not giving answer as 0? As I am new, I am not getting this. Please explain me in a little bit detail. Thank you
|
|
|
|
|
The problem you have is that you are mixing two things up here. Let's break this method down:
public static int abc(ref int j)
{
j = 100;
return 0;
} The method signature indicates that you are accepting a variable that you are going to populate in this method. It also indicates that you are returning a type of int as the return type from the method (the bit that goes int abc ). This means that you have told the compiler that you have two methods of setting values here. Now, suppose you changed your call to this:
int output = abc(ref i); You now have two values coming back from the method. The value in output would be 0, as that is the value coming back from the return statement. The value in i would be 100 because that is the value you set j to in abc .
This space for rent
|
|
|
|
|
Thank you so much for your helpful explaination. Now I got the actual concept of return statement. Thank you so much.
|
|
|
|
|
You are most welcome.
This space for rent
|
|
|
|
|
You aren't actually using the return statement in your code. What is happening here is that the ref statement is the thing that is actually setting the value of the variable. If you want some fun, try to predict what the value of this statement is (changing your code slightly):
int i = 0;
i = abc(ref i);
Edited with the correct statement on line 2.
This space for rent
modified 11-Sep-18 14:47pm.
|
|
|
|
|
Fortunately, you'll get a compiler error:
Error CS1510 A ref or out value must be an assignable variable
But ... back in the seventies I worked with a FORTRAN compiler that didn't issue an error, and would happily change the value of constants. Imagine if this C# code worked:
void xx(ref int i)
{
i += 10;
}
static int main()
{
int i = 666;
xx(666);
int j = 666;
Console.WriteLine($"{i}, {j}");
} And printed 666, 676
Now debug that in a 1,000,000 line project without an IDE!
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!
|
|
|
|
|
Doh, it was meant to be
i = abc(ref i); That's what I get when I type directly into the code editor.
This space for rent
|
|
|
|
|
|
This is not an error, is a Conflict Information.
Search the error in the Error List with the red icon on the left (I didn't download the source, so I didn't revise more than I told it).
|
|
|
|
|
Dear All,
i want to encrypt / decrypt a string that can be of any length to maximum 10 random characters only,
i tried a lots of encryption / decryption algorithms but all gives very long encrypted string, appreciate if anyone can share to limit the encryption string to only 10 characters provided these characters to be random each time.
Many Thanks in Advance.....
|
|
|
|
|
Can't be done.
Think about this. Encryption means that not a single bit of data can be lost. Every bit of it must be represented. Encryption is lossless.
Now, you said you want to pack any string of any length into no more than 80 bits. How on earth are you going to pack a "string of any length", lets just say a 3MB string, into 80 bits?
Encryption is NOT compression.
You may be able to cryptographic HASH a string into 80 bits, though that's going to limit your possible hash values. With such few bits (80 of them), the chance of a collision (2 strings coming up with the same hash value) is higher than normal. There is no way to reverse the process and get the original string back. A cryptographic hash cannot be reversed.
|
|
|
|
|
One-time-pads.
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.
|
|
|
|
|
You cannot encrypt a string to any specific maximum length: when you encrypt a string you are adding information not compressing it - and the encrypted data is likely to be less compressible than the original, or the encryption method includes too much repetitiveness in it;s output and may be easily cracked!
You can make a value from your input which has a fixed size, but that is by a process called Hashing - which is similar to encryption but is not reversible; you cannot retrieve the original input from a hashed value, while you can with encrypted data.
Why do you think you need this? What are you trying to do? There may be a better way ...
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!
|
|
|
|
|
@
OriginalGriff
Hi, I am getting a value in a field which i needs to hide from user for sometime, and once the workflow completed i need to show back the orignal string in the same field.
Thats it.
The issue is if user inputs in a field of 10 characters, encryption string goes to more then 10 characters which the field cannot accommodate hence it cannot be decrypted, so basically issue reside with decryption.
|
|
|
|
|
So store the user input away somewhere - a DB maybe - and use a "place holder" value to indicate it. When they want to retrieve it, you cross reference the string back to the original store and you have the string they started with.
The problem is that 10 characters is unusual, most hashes these days start at 128 bits, which won't fit in 10 chars no matter what you do with it.
Suggestion: if you use A-Z and 0-9 that's 36 possible characters - that's a total of 3E15 combinations (36^10) which fits in a 64 bit integer. So ... if you use a random number generator to generate a value in the range 0 to 3E15 (and check it isn't used, if it is regenerate) you could convert that to a 10 character string very easily, and that can be converted back to a big integer and used to lookup the actual string to return.
Make sense?
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!
|
|
|
|
|
Ya that was the workaround i already done, but was looking the possibility for limiting the encryption which now i understand couldn't happen so i will stick with the workaround you advise and already been using. Many thanks for your time... This gives me confident for what i am doing is correct. Thanks.
|
|
|
|
|
You're welcome!
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!
|
|
|
|
|
I have a problem to save userControl to PNG By Drop & Drag.
I can save it with a Button_Click But, I try to use an easy way to save UserControl to PNG with drop & drag
What to do am I, I don't know to get path Windows Explorer or The desktop where mouse drop file? Thanks for suggesting.
I use this Button_Click()
<pre>private void saveToPng_Click(object sender, RoutedEventArgs e)
{
string filePath = Environment.CurrentDirectory;
var control = this.Control;
RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32);
Rect bounds = VisualTreeHelper.GetDescendantBounds(control);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(control);
ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}
rtb.Render(dv);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fileStream = new FileStream(filePath+"test.png", FileMode.Create))
{
png.Save(fileStream);
}
}
|
|
|
|
|
|
thanks, Eddy Vluggend
But, I need Drag UserControl from my-app and Drop it to Windows-Explorer or Desktop to Saving Format PNG.
Do U understand my problem?
I want to save UI of UserControl BY Drop-Drag as Format PNG
|
|
|
|
|
|
I use WPF-XAML and the code was about windows-Form, I can't understand that.
Can U help me with a sample of WPF-XAML?
please use WPF-XAML
Thank
|
|
|
|
|
I have no experience with XAML. Google is your friend, I imagine the proces to be very similar
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.
|
|
|
|
|
Hi. I am used to visual basic so I need help with a C# conversion. I have a for that allows opening a choice of other forms. I am used to the form.show approach.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void cmdLotto_Click(object sender, EventArgs e)
{
Form = new Form(LottoConverter);
Form.ShowDialog();
}
private void cmdEuro_Click(object sender, EventArgs e)
{
Form = new Form(EuroConverter);
Form.ShowDialog();
|
|
|
|