|
You can get what you need with generic programming and OOP.
This is a raw example:
public class MultiVar<T>
{
private List<T> Values = new List<T>();
private int ValuePointer = -1;
public MultiVar()
{
}
public MultiVar(T FirstValue)
{
Values.Add(FirstValue);
}
public void AddValue(T Value)
{
Values.Add(Value);
}
public T GetValue()
{
if (Values.Count < 1) throw new ApplicationException("Empty value set.");
if (++ValuePointer >= Values.Count) ValuePointer = 0;
return Values[ValuePointer];
}
public static implicit operator T(MultiVar<T> Object)
{
return Object.GetValue();
}
}
You can check it with:
MultiVar<int> mymultivar = new MultiVar<int>();
for (int i = 10; i < 110; i += 10) mymultivar.AddValue(i);
for (int i = 0; i < 15; i++)
{
Console.Write(" ");
Console.Write((int)mymultivar);
}
Console.WriteLine();
The result is:
10 20 30 40 50 60 70 80 90 100 10 20 30 40 50
Just add code to remove values, if you need it, and customize as needed.
Enjoy!
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
So.. your implicit cast operator has side effects
|
|
|
|
|
And might throw an Exception.
|
|
|
|
|
Yes, and this is perfectly ok too!
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Yes, and this is perfectly ok.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Well I'm going to disagree, but let's see what other people think..
Just because it works does not mean it's ok - this is IMO worse than a getter of a property with side effects. A "funny hack" in this case, but still.
|
|
|
|
|
It's not that I disagree with you, I see your point.
All I'm saying is IMO what I suggested was a good starting point for working out a real world solution for the proposed problem.
He can decide if he likes/wants operator overloading and exception throwing, or he can just use GetValue() (which btw is what I would do in a real world project).
My point is, I just wanted to give him all the possible approaches I could think of to solve his problem.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
At least make it explicit.
"
However, because implicit conversions do not require programmers to explicitly cast from one type to the other, care must be taken to prevent unexpected results. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit. For more information, see Using Conversion Operators.
"
I would, however, agree that as long as the behaviour is well documented, it may be accepable within a limited scope.
On the other hand, there are better solutions in this case.
|
|
|
|
|
PIEBALDconsult wrote: would, however, agree that as long as the behaviour is well documented, it may be accepable within a limited scope.
Exactly my point.
PIEBALDconsult wrote: In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness.
And thanks for pointing that out explicitly while I didn't.
You are absolutely right on that being important ofc!
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
You don't "call" a variable, so you probably need either a method or a property. Put the code to return the correct value inside the method or property.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
Wogboiii wrote: Is there a way to assign a variable a series of values?
Use a collection.
|
|
|
|
|
Yes, if you make the variable a property of a (static) class that provides the behavior you want.
Console.WriteLine (Variable.Value.ToString());
Console.WriteLine (Variable.Value.ToString());
Console.WriteLine (Variable.Value.ToString());
Console.WriteLine (Variable.Value.ToString());
will print
5
9
46
152
/ravi
|
|
|
|
|
Somewhere in the Microsoft guidelines it sure is stated one should not use properties to return changing data; properties should be pretty constant, such as Environment.NewLine . One of the wildest but acceptable exceptions being DateTime.Now .
When real data is to be offered, a method should be used; that is why Directory.GetFiles() and Random.Next() are methods and not properties.
|
|
|
|
|
Luc Pattyn wrote: Somewhere in the Microsoft guidelines it sure is stated one should not use properties to return changing data; properties should be pretty constant,
Yes, that's CA rule #1024[^].
I used a property for ease of understanding. It could of course be replaced by the method .Value() .
/ravi
|
|
|
|
|
You don't need ToString with WriteLine.
|
|
|
|
|
Imho, it's good practice to use ToString() for at least 2 reasons: (1) it provides deterministic formatting and (2) it takes advantage of a custom object's display format. (1) makes it easier to perform regression testing and (2) allows meaningful information to be displayed vs. just the object's fully qualified type name.
/ravi
|
|
|
|
|
What do you think WriteLine will do if you leave it off?
|
|
|
|
|
You're right in saying you don't need to ToString() an int (and many other types) because WriteLine() will display an acceptable form of the type without it.
But you seem to have missed the point I was trying to make.
/ravi
|
|
|
|
|
You may have missed the point <i>I</i> was trying to make...
Ravi Bhavnani wrote:
WriteLine() will display an acceptable form of the type without it.
... by calling ToString.
|
|
|
|
|
Yes, you are correct. As I said in my previous post.
/ravi
|
|
|
|
|
hi everyone, When debugger works, it comes to a procedure, enters in, runs a few lines of code and goes out without completing whole procedure and finishes debugging without any error. Your valuable comments are welcomed, thanks
|
|
|
|
|
Is it an OnPaint or something like that? There are sneaky hidden try/catches around some the of WinForms methods that "eat" your exceptions before you see them (you can work around that a bit by putting a try/catch around the code and put a breakpoint in the catch)
|
|
|
|
|
|
If you're debugging a multithreaded app, this could happen.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
I have a custom control containing a movable Thumb (similar to ScrollBar Thumb), drawn using a System.Drawing.Rectangle which can be clicked and dragged etc
As I am showing ToolTips for other controls within this custom control, I would like to be able to show a similar style ToolTip for the Thumb (and other similar pseudo controls) .
My understanding is that as the Thumb is not based on Control, the ToolTip class cannot be used to do this.
Do I need to make my Thumb a Thumb class based on Control, to attach ToolTips?
Is there a way to attach a ToolTip to Rectangles etc in another fashion?
Thanks for any help or suggestions.
AussieLew
PS.. I have done a fair bit of searching on this and have found nothing helpful even though I thought it would be a common question.
|
|
|
|