|
I completely agree that a variable of type int is not immutable, but that doesn't really matter since immutability is a property of a type, not of a variable of that type (obviously a variable is, well, variable).
Funny that you should mention strings, they're less immutable than integers, but most people agree that they should be called immutable anyway because the only way to mutate an instance is by cheating (reflection or unsafe). You can't even mutate an instance of an int by cheating.
modified 17-Feb-14 6:30am.
|
|
|
|
|
OriginalGriff wrote: That's not the way I see immutable:
Similar problem exists with 'const' in C++.
In terms of semantics for that I differentiate between 'binary constant' and 'logical constant'.
The first means that the structure of the memory for the data will not change.
The second means that for a user of the entity that it will not change. And example of this is a class that represents application properties that cannot be changed by the application itself but which use a memory cache which might be refreshed for various reasons.
|
|
|
|
|
+5 very useful response !
I find it hard to conceptualize structs as "immutable" when I can do stuff like that shown in the code for 'TestStruct below; and, so do other people:
public struct MyStruct
{
public string Name;
public int X;
public int Y;
public int Z { set; get; }
public MyStruct(int z) : this() { Z = z; }
}
public void increment(ref MyStruct aStruct, int inc )
{
aStruct.X += inc;
}
private void WriteSValues(params MyStruct[] theStructs)
{
foreach (var theStruct in theStructs)
{
Console.WriteLine("Name: {0} X: {1} Y: {2} X: {3}",
theStruct.Name,
theStruct.X,
theStruct.Y,
theStruct.Z);
}
Console.WriteLine();
}
private void TestStruct()
{
MyStruct ms1 = new MyStruct(300) {Name = "ms1", X = 100, Y = 200};
ms1.X += -300;
MyStruct ms2 = ms1;
ms2.Name = "ms2";
WriteSValues(ms1, ms2);
increment(ref ms1, 200);
WriteSValues(ms1, ms2);
ms1.X += 500;
WriteSValues(ms1, ms2);
MyStruct ms3 = ms2;
ms3.Name = "ms3";
WriteSValues(ms1, ms2, ms3);
increment(ref ms3, 1000);
WriteSValues(ms1, ms2, ms3);
} The above is a diagnostic quiz I wrote for some supposedly "intermediate" C# students (not students of mine): their task was to describe the values for each struct created, at each line of the code, and explain why the values were what they were. About half understood that assigning an instance of a struct to another instance of a struct of the same Type created a copy, but they all were quite confused by the other bits in the code. The quiz was administered by their regular teacher, so I don't think the results exhibit bias due to shyness in the presence of someone from outside their social process.
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
Hi David,
I'm writing up my response to your question as a simple article, and with your permission, I'd like to include an extract from your question in that:
David C# Hobbyist. wrote: As a self taught ameture in c#, I have never found a reason to implement a struct instead of a class.
Since I am self taught (No formal education) I would like to know what advantage would be gained by using a struct in lieu of a class? More importantly why when and where would it benefit my programs?
If you agree, I can tidy up the spelling, and either credit you or leave it anonymous - whichever you prefer - but I think it would help the "flow" of the article to explain why it exists.
If you don't agree for whatever reason (or none!), that's no problem either, I just won't reference you and will come up with a different introduction!
Please do reply whichever way you decide - but if you don't I will of course assume you are refusing permission and not reference you in any way.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Absolutely and thanks for the long winded reply. I am just now reading it as well as the info in the links posted by @Peter-Leow.
I have some studying to do.
Thank You for the help in understanding the difference.
[Edit] Please inform me when it is posted I would like to read it.[/Edit]
David
modified 17-Feb-14 17:20pm.
|
|
|
|
|
Thank you -and you're welcome!
I certainly will.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
It's published: Using struct and class - what's that all about?[^]
It's much the same as the answer, but with some added material to cover embedded structs, and arrays.
Thank you again!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Thanks, I read it this morning. I found a couple of times in my code where a struct would have been a better choice. But "If it aint broke".
David
|
|
|
|
|
..."don't fix it"
Wise decision!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I have the following task to design an interface not the classes for an authorization class it should account for the following conditions can people recommend changes to the class or how could i better it according to the following rules
Administrative users of the application manage users and permissions. Managers can view and edit information about employees reporting to them. Employees can view their information but do not have access to other employees' information. Employees in the Human Resources department have access to information about all employees, but don't have access to the information of others in HR.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NaviNetHrContext.Context
{
public interface IAuthorisation
{
IEnumerable<Sallary> GetSallaryRecordById(int employeeId);
IEnumerable<Vacation> GetVacationRecordsById(int employeeId);
IEnumerable<Employee> GetEmployeesByManagerId(int employeeId);
IEnumerable<Employee> GetEmployees(int employeeId);
Boolean IsInRole(string employeeId, int groupRole);
Boolean DeleteRole(string employeeId);
Boolean EditRole(string employeeId, Boolean deleteRights, Boolean editRights, Boolean viewRights, int groupRole);
string CreateUserInRole(string employeeId, Boolean deleteRights, Boolean editRights, Boolean viewRights, int groupRole);
string CreateRole(Boolean deleteRights, Boolean editRights, Boolean viewRights, int groupRole);
}
}
|
|
|
|
|
Authorization has nothing to do with one's salary. I agree that one has to check whether on is authorized before giving out that info, but I wouldn't expect it to be part of the authorization interface; one would simply check if someone is in the role of HR. Same goes for vacation
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello all. I recently finished a homework assignment which involves creating a fully functioning calculator in c# which can also take the integers which are being displayed and convert them to binary, and then vice versa via radio controls.
My convert2decimal function isn't working correctly though. Say I input 999 into the window, it will output the correct binary value of
1111100111 But then when i click the radio button to convert that value from the text box back to decimal, instead if 999 i get 927.
Also yes i am aware that i could simply declare a variable to hold the original decimal value, but i doubt my instructor would look upon that fondly.
Here is the offending code snippet
private void convert2Decimal(string value)
{
String binaryDigits = value;
char [] Binary = binaryDigits.ToCharArray();
long dValue = 0;
int superscript = display.Text.Length - 1;
int bValue = 1;
if (superscript > 0)
{
foreach (char element in Binary )
{
Console.WriteLine((long)char.GetNumericValue(element));
if ((long)char.GetNumericValue(element) == 1)
{
Console.WriteLine( dValue += ((long)char.GetNumericValue(element)) * bValue);
}bValue *= 2;
}
display.Text =""+ dValue;
}
Even more strange behavior is that, when converting the wrong value of 927 back to binary via the binary conversion, i get the correct binary value of 927, but when i finally click the decimal conversion, it shows a value of 999...!
|
|
|
|
|
This is a guess because I haven't tried your code, but I think the problem is that when you convert binaryDigits to a char array, the resulting array is in the wrong order.
In other words, the array is left to right, but when you're increasing bValue by saying bValue *= 2 , you're going from right to left.
To fix it, you either need to reverse the char array, OR loop through the char array in reverse order.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I figured id have an issue when I dumped my manual for loop for the foreach. Thanks a bundle!
|
|
|
|
|
Too much redundant code in the above; try this:
private long convert2Decimal(string value)
{
long result = 0;
foreach (char c in value)
{
result <<= 1;
if (c == '1')
result += 1;
}
return result;
}
Veni, vidi, abiit domum
|
|
|
|
|
Well, if you're going for brevity:
private long convert2Decimal(string value)
{
return Convert.ToInt64(value, 2);
}
However, I doubt the teacher would accept that as a homework answer.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
No, I was trying to show OP how to do the conversion. The brevity came about because a) I had no idea what all the extra code was there for, and b) that's all it takes.
|
|
|
|
|
Thanks so much! I need to study how that operator works
|
|
|
|
|
The left shift operator is equivalent to multiplying by a power of two. So left shifting the total by 1 bit, multiplies it by two. An alternative way of looking at it is that you are just putting the binary ones and zeros into a number in the same order as the characters in the string.
|
|
|
|
|
I wanted to add X button for each tab. The drawMode is OwnerDrawFixed. it works fine if it left to right. Once I allow RightToLeftLayout = true and RightToLeft = true, it does not look good because that he still adds the string from left to right, while that tab add from right to left.
How do I make it that string will also be right to left?
Thanks.
private void addCloseButton(object sender, DrawItemEventArgs e)
{
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15 , e.Bounds.Top +4 );
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left+4, e.Bounds.Top+4);
e.DrawFocusRectangle();
}
private void actionClose(object sender, MouseEventArgs e)
{
for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
{
Rectangle r = tabControl1.GetTabRect(i);
Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 12, 10);
if (closeButton.Contains(e.Location))
{
if (MessageBox.Show("?האם אתה רוצה לסגור טאב זה", "אישור", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.tabControl1.TabPages.RemoveAt(i);
break;
}
}
}
}
|
|
|
|
|
I want to open a form in an existing tab page by pressing on a button outside of the tabcontrol. If I opened a few tabs and to example the third tab is currently open and I click on a button to open a form, then the form will open in the current tab, that is third tab. Also the tab gets the name of the form that opens. The new form will replace the existing form in the current tab page.
How do I do this?
Thanks.
|
|
|
|
|
i found the answer.
NewForm childForm = new NewForm ();
childForm.TopLevel = false;
int curr = tabControl1.SelectedIndex;
TabPage tbp = tabControl1.TabPages[curr];
tabControl1.TabPages.Contains(tbp);
tabControl1.TabPages[curr].Text = "name of new form";
tbp.Controls.Add(childForm);
childForm.WindowState = FormWindowState.Maximized;
childForm.Show();
Refresh();
|
|
|
|
|
What is the purpose of this statement?
tabControl1.TabPages.Contains(tbp);
/ravi
|
|
|
|
|
hello hello sir i want to make a sms application in c# using AT command but while i plugged the GSM modem into the computer it doesnot work properly what should i do sir??
|
|
|
|
|
aawajaarughat wrote: doesnot work properly
It's not clear what do you mean by that! Do you have some error or wrong behavior? Pleas add more details...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|