|
JSOP what benefit do you get from declaring the class like this
public class CharCounter : List<CharData>
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I do that all the time because then it lets me black-box stuff that I want the list to be able to do on its own (like parse the array of characters), which has the added benefit of simplifying usage. Just habit more than anything else.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I have a class called move. I have instantiations of move in a List Collection called playerMoves. I have an instantiation of the class move called tempMove.
MOVE CLASS:
public class move
{
public string displayName = string.Empty;
public string element = string.Empty;
public int damage = 0;
public int enemyDamageSubtraction = 0;
public int minimumLevel = 0;
}
I am running through playerMoves using a foreach statement (it is in another foreach statement, which is part of the code that gets the data for tempMove), and trying to check if my collection contains a move instantiation with the same data in it.
How do I do that? I researched, but I didn't find anything except for some very complex NuGet Packages that make my head spin!
|
|
|
|
|
Start by implementing IEquatable<T> and overriding GetHashCode and Equals :
How to: Define Value Equality for a Type (C# Programming Guide) | Microsoft Docs[^]
public class Move : IEquatable<Move>
{
...
public override int GetHashCode()
{
unchecked
{
int result = 17;
result = result * 23 + StringComparer.Ordinal.GetHashCode(displayName ?? string.Empty);
result = result * 23 + StringComparer.Ordinal.GetHashCode(element ?? string.Empty);
result = result * 23 + damage.GetHashCode();
result = result * 23 + enemyDamageSubtraction.GetHashCode();
result = result * 23 + minimumLevel.GetHashCode();
return result;
}
}
public override bool Equals(object obj)
{
return Equals(obj as Move);
}
public bool Equals(Move other)
{
if (ReferenceEquals(other, null)) return false;
return string.Equals(displayName, other.displayName, StringComparison.Ordinal)
&& string.Equals(element, other.element, StringComparison.Ordinal)
&& damage == other.damage
&& enemyDamageSubtraction == other.enemyDamageSubtraction
&& minimumLevel == other.minimumLevel;
}
public static bool operator ==(Move left, Move right)
{
return Equals(left, right);
}
public static bool operator !=(Move left, Move right)
{
return !Equals(left, right);
}
}
You can then use the Contains method[^] to see if your list contains an instance with the same values as your temp instance:
Move tempMove = new Move
{
displayName = "...",
element = "...",
damage = ...,
enemyDamageSubtraction = ...,
minimumLevel = ...,
};
bool alreadyInList = playerMoves.Contains(tempMove);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
While I am sure Richard Deeming's excellent answer here will meet your needs ... it's a perfect example of the need for, and use of, 'IEquatable ...
I think it's a good idea to consider the strategy of never allowing duplicates to occur.
I suggest you look closely at your code that instantiates the 'Move class: do you really want to allow duplicate 'displayname properties ? How about 'element ?
If you keep 'displayname values unique, you might consider making a 'struct containing the three 'int properties, and using a Dictionary<string, intdata> to get hash-based look-up.
For issues around use of Strings in a struct, see: <[^].
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hello,
Some Turkish characters (ı,ş..) are not showing up at .pdf. what is wrong at below BaseFont:
BaseFont bF = BaseFont.CreateFont("C:\\windows\\fonts\\timesbd.ttf", "iso-8859-9", BaseFont.EMBEDDED);
iTextSharp.text.Font f = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.NORMAL);
modified 13-Feb-19 21:02pm.
|
|
|
|
|
Hi,
I don't know, however this page[^] would make me try BaseFont.CP1254 instead of "iso-8859-9"
|
|
|
|
|
I want to open with a click on a button, some sort of dialog which contains text in a new window.
The problem is that i want to have both forms active, so the user can click on both. Or at least have the dialog form locked but you can still close it, and original form opened.
I have already created a pop up form but when it pops up, you cant use the original form.
Any suggestions?
|
|
|
|
|
Both usable at once:
MyOtherForm mof = new MyOtherForm();
mof.Show(); Close and continue:
MyOtherForm mof = new MyOtherForm();
if (mof.ShowDialog() == DialogResult.OK)
{
...
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Awesome, thanks it works. I have used ShowDialog instead of just Show. Thats way first form was locked.
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
When you set the 'Owner property of a Form to another Form, then you guarantee that when you 'Show the owned Form, it will be visible in front of (higher in the z-order) the owner Form: [^]
Both Forms will be movable, visible, usable.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hi all,
I'm EXTREMELY new to c# visual studios and honestly don't understand it at all but have to do a piece of coursework.
I have to create a controller for a washing machine and on the user interface I'd like to have a button labelled 'temperature' that when clicked will scroll through multiple options, like on a real washing machine.
So I've inserted a button and I have a checkedlistbox with all of the available temperatures but really don't know where to go from there.
Is there any way I can have the button click once to select 30C, then when clicked again selects 40C and when clicked again selects 60C (then any clicks after that just repeat the cycle)??
I'd really like to learn how to do this please!
|
|
|
|
|
You're going to need to start by telling us what UI framework you're using. Is it Windows Forms? WPF? ASP.NET WebForms? ASP.NET MVC? ASP.NET Core? Something else?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Although knowing what environment you are working in would certainly help, the principle is the same in all environment, just the details will change.
The way I'd do it* is to create an array of strings:
private string[] Temperatures = { "30C", "40C", "60C", "90C" }; And a current setting variable:
private int currentTemp = 0; Then each time the button was clicked I'd change it:
private void butTemp_Click(object sender, EventArgs e)
{
currentTemp++;
if (currentTemp >= Temperatures.Length)
{
currentTemp = 0;
}
butTemp.Text = Temperatures[currentTemp];
}
* Well, not really, I'd use a "TemperatureSetting" class instead of strings and temps to hold it all together - but that's the more advanced stuff.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That would result in an awkward UI: whenever I want anything but 30C, I would need to click the 30C button
I'd rather have a label showing the current temperature setting, and the button saying "Change temperature".
How about a bunch of RadioButtons inside a GroupBox? or a user-defined scrollbar? or ...
|
|
|
|
|
I'd agree, but then, I don't design washing machines ... if I did, they'd not grow mould, would show you how long they had before they were finished, could be told to run on cheap rate electricity, and be a damn sight quieter.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Can I pre-order one?
|
|
|
|
|
I'll put you at the top of the list if I ever go into the industry!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
"have to do a piece of coursework."
If your class/teacher has not equipped you, so far, to start writing code, then something is wrong here.
Is everyone else in the class confused ? If that's true, then something may be wrong with the teacher, or the course content. If the other students are not having difficultly, perhaps ask your teacher's help to figure out why you are having difficulty.
If we write code for you, you wiil learn nothing.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
There are many ways that you could do this. A simple way to do this would be to simulate a circular Linked List with the last entry pointing back to the first entry. That way, you would only have to walk through the list.
This space for rent
|
|
|
|
|
Hello,,i wrote a code for finding the nth term in the series an print the result. So, I used a while loop in that,where I used 2 variables named tem1,tem2. When I print after the while loop,it is snot printing the updated value.Can anyone suggest a solution.
public class ArmstrongExample
{
public static void Main(string[] args)
{
int ip1, ip2, ip3, ip4, res, tem2 = 0;
ip1 = Int32.Parse(Console.ReadLine());
ip2 = Int32.Parse(Console.ReadLine());
ip3 = Int32.Parse(Console.ReadLine());
ip4 = Int32.Parse(Console.ReadLine());
res = summeet(ip1, ip2, ip3, ip4);
Console.WriteLine(res);
int summeet(int p1, int p2, int p3, int p4)
{
int a, b, c, n, tem1, i = 0, d1, d2;
a = p1;
b = p2;
c = p3;
n = p4;
while(i>n-3)
{
d1 = b - a;
tem1 = d1 + c;
d2 = c - b;
tem2 = tem1 + d2;
i++;
tem1 = tem2;
}
return tem2;
}
}
}
|
|
|
|
|
That won't compile. Why not? You don't declare tem2, and if you do, you need to give it a default value or you can't return it.
In addition there is no good reason for a local function here: it just makes the code harder to read.
If it doesn't compile, it doesn;t generate an executable file, so the version you run is the last "compile error free" version of your source code.
Fix the compilation errors, and it may work better:
public static void Main(string[] args)
{
int ip1, ip2, ip3, ip4, res, tem2 = 0;
ip1 = Int32.Parse(Console.ReadLine());
ip2 = Int32.Parse(Console.ReadLine());
ip3 = Int32.Parse(Console.ReadLine());
ip4 = Int32.Parse(Console.ReadLine());
res = summeet(ip1, ip2, ip3, ip4);
Console.WriteLine(res);
}
private static int summeet(int p1, int p2, int p3, int p4)
{
int a, b, c, n, tem1, tem2 = 0, i = 0, d1, d2;
a = p1;
b = p2;
c = p3;
n = p4;
while (i > n - 3)
{
d1 = b - a;
tem1 = d1 + c;
d2 = c - b;
tem2 = tem1 + d2;
i++;
tem1 = tem2;
}
return tem2;
}
But do yourself a favour: stop using as-short-as-possible names for things: it may seem like a waste of time and more work, but using descriptive names makes your code document itself, and that means it's more readable. It also means it's a lot easier to tell when you used the wrong variable, and that makes your code more reliable and easier to debug. the variables a, b, c, and n are unnecessary, you can use the parameters directly instead; and give them sensible names instead of "p1", "p2" and so on. That way, when you want to use the method later, Visual Studio will even prompt you as to what value belongs in what parameter!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You really need to learn how to use a debugger. If you had used one (as well as given us the code where tem2 was declared), you would see that your while condition would never finish. So, why won't it finish? Well, you have declared a variable called n which gets a default value of 0; you haven't actually allocated anything to it. Now, when your program runs, you will see that i (which starts at 0) will always be greater than n-3, so it never terminates. Honestly, you could have found this out in a minute with the debugger.
This space for rent
|
|
|
|
|
Hi,
I fail to see what your loop is trying to achieve; the variable i will be incremented on each iteration, however the five other variables will never change once the first iteration has finished...
Furthermore, when the loop tests for larger than, I expect the control variable to be lowered inside the loop, and not incremented. Otherwise you may end up waiting for an “integer overflow”.
modified 10-May-18 5:09am.
|
|
|
|
|