|
Both points aren't suitable to my specific scenario.
No, because this is from a NoteVisual class, which is base for e.g. QuarterNoteVisual. Every child class (quarter, half, full etc) use just one of these methods and does not do any switch.
It is possible to design a deeper inheritance
Note
abstract ~RenderHead
FilledNote
+RenderHead
QuarterNote
and
Note
abstract ~RenderHead
EmptyNote
+RenderHead
HalfNote, FullNote
But this would make the code too javaish -- superdeep inheritance which quickly become hard to maintain.
Greetings - Jacek
|
|
|
|
|
Ah, ok, so if I'm to understand, its more like:
HalfNote.Render -> call base.Func1()
QuaterNote.Render -> call base.Func2()
In that case, I'd *still* say the 2nd method is preferable
1) you have one base function to maintain vs two (or more)
2) base.Func(HeadTypes.Filled) and base.Func(HeadTypes.Unfilled) looks and feels cleaner to me
On the other hand, if you are drawing a *lot* (like 10's of thousands) of these notes and need to squeeze out every single iota of performance then the first method would probably be better since you can get rid of the switch and its associated jumps. From the code you posted, you have a *bit* of room for other improvements too, but getting rid of the switch for performance reasons will only come into play if you are calling these functions a *lot* (like 10's of thousands of times).
|
|
|
|
|
I agree with you in general, I wanted to make some adnotations to your post. BTW, I think that it would have to be *much more* than 10's of thousands to observe a differnce between switch vs override. My post was about coding style, not performance. BTW max number of calls will be ~50.
Greetings - Jacek
|
|
|
|
|
Hello,
There Service07.Eso9PortClient class that contains method getOrders () which returns an array of orders.
Each order contains information (address, folders, etc.).
I need to convert these orders into an array or collection so that I could browse and handle these orders.
Could someone advise me how to do it?
/ / Order to convert the array or collection / /
/ / Orders
Service07.Eso9PortClient orderClient07 Service07.Eso9PortClient = new ();
Service07.Order [] orders = orderClient07.getOrders ();
foreach (Service07.Order order in orders)
{
/ / Order addresses
Service07.Address [] addresses = order.Addresses;
foreach (Service07.Address address in addresses)
{
/ / Addresses attributes
Service07.Attribute [] = addressAttributes address.Attributes;
foreach (Service07.Attribute addressAttribute in addressAttributes)
{
string = addressAttributeName addressAttribute.Name;
string = addressAttributeValue addressAttribute.Value;
}
string = addressCity address.City;
string = addressCompany address.Company;
string = addressCountry address.Country;
}
/ / Order attributes
Service07.Attribute [] = orderAttributes order.Attributes;
foreach (Service07.Attribute orderAttribute in orderAttributes)
{
string = orderAttributeName orderAttribute.Name;
string = orderAttributeValue orderAttribute.Value;
}
/ / Order Coupon
string = orderCouponCode order.Coupon! = null? order.Coupon.Code: null;
string = orderCuponValue order.Coupon! = null? order.Coupon.Value: null;
/ / Order Created
string = orderCreated order.Created;
/ / Order items
Service07.OrderItem [] items = order.Items;
foreach (Service07.OrderItem item in items)
{
string itemName = item.Name;
string = itemNote item.Note;
}
}
// Result should look something like this
string ... myArray = new ...
foreach (string in order myArray)
{
foreach (string address in myArray...)
{
....
}
}
Unfortunately I do not know how to declare a field and set
Thank you for your answer
|
|
|
|
|
Please format the code properly; it is unreadable in this way and try to provide the code examples that will actually compile (and not as this string = addressAttributeName addressAttribute.Name; )!
|
|
|
|
|
Dear All,
How can i read values from Byte Array?
Regards,
DJ.
|
|
|
|
|
The easiest way to read the data is to loop over the array. Here's a typical method for doing this:
for (int i = 0; i < myArray.Count; i++)
{
byte value = myArray[i];
}
|
|
|
|
|
|
You can read larger base types from a byte array with BitConverter[^].
|
|
|
|
|
Be careful when using BitConverter. It uses the endianness of your CPU, so if you're byte array is coming from a hardware device with a different endianness, your BitConverter results will be messed up.
See BitConverter.IsLittleEndian[^]
Dybs
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
Indeed. Most platforms agree on endianness but particularly if you deal with embedded systems this can be a concern.
|
|
|
|
|
Actually i don't want its size. I need to get the value.
When i iterating it displays the size.
How can i get the value behind each array item?
|
|
|
|
|
Since your question mentioned a byte array, the size of each item is 1 byte.
Pete O'Hanlon mentioned the item count within the array. One could call that a size, too.
DJ245 wrote: When i iterating it displays the size. What size exactly do you mean by this?
DJ245 wrote: How can i get the value behind each array item? Each item's value is exactly what Pete O'Hanlon's answer is concerning with.
Ciao,
luker
|
|
|
|
|
Please check the screenshot
http://doubts.posterous.com/bytearray-doubt[^]
Actual value in the byteArray[0] is Name of an employee, [1] is code and son on...
When iterating it returns only its size.. How can i get the Name of Employee from byteArray[0]?
|
|
|
|
|
I store two kinds of data types (Bar1 and Bar2) in the same dictionary. Both data types dervies from Foo class. I would like to extract only the Bar2 elements from the dictionary.
Here is what I got:
abstract class Foo
{
}
class Bar1 : Foo
{
}
class Bar2 : Foo
{
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, Foo> foos = new Dictionary<string, Foo>();
for(int i = 0; i < 5; i++)
foos.Add("Bar1." + i, new Bar1());
for (int i = 0; i < 10; i++)
foos.Add("Bar2." + i, new Bar2());
List<Bar2> bar2List = new List<Bar2>();
foreach (Foo foo in foos.Values)
{
if(foo is Bar2)
{
bar2List.Add((Bar2)foo);
}
}
}
}
Are there any other method to extract the Bar2 elements from the dictionary into the Bar2 array (bar2List) instead of using foreach-loop and if-statment?
|
|
|
|
|
could give LINQ a whirl...
using System.Linq;
List<Bar2> bar2List = (List<Bar2>)foos.Values.Where(f => f is Bar2).ToList();
...it compiles but I didn't run it
If my jokes make me laugh, then I have already succeeded with 100% of my target audience
|
|
|
|
|
Didn't work: InvalidCastException...
Unable to cast object of type 'WhereEnumerableIterator`1[ConsoleApplication1.Foo]' to type 'System.Collections.Generic.List`1[ConsoleApplication1.Bar2]'.
Some other solution?
|
|
|
|
|
I think
List<Bar2> bar2List = foos.Values.Where(f => f is Bar2).ToList<Bar2>();
... should do it.
Linq-to-objects like this is essentially doing the same thing as the foreach loop in your original suggestion, so performance wise you will gain nothing, but some people will say that the Linqed code is more readable (and in this case I think I'd agree with that).
(Ed: putting the type parameter on Where doesn't work, it tries to cast items before checking them.)
|
|
|
|
|
You cando this with LINQ
List<Bar2> bar2s =
(from entry in foos
where entry.Value is Bar2
select entry.Value as Bar2).ToList();
If you don't care about the list being of Bar2, you can just do this:
var bar2s = from entry in foos
where entry.Value is Bar2
select entry.Value;
Not sure what the performance comparision is like, but my gut instinct is to go with the cleaner code unless I know performance is a concern.
|
|
|
|
|
Do they really have to come from that combined dictionary? Couldn't you keep a separate list of bar2?
|
|
|
|
|
List<Bar2> bar2List = foos.Values.OfType<Bar2>().ToList();
Of course, internally it's pretty much just doing the same foreach loop you're doing, so don't expect it to be much more efficient.
|
|
|
|
|
Whats wrong with a foreach loop? Do you care about speed / performance or do you care about writing elitist LINQ code or some other fancy method to impress somebody? No matter what method you use, at some level, its going to have to do a foreach loop, so why not cut out the middle man for performance reasons? People who use LINQ for every little thing often end up ripping it out down the road cuz its often much slower then just writing "normal" code. True Story.
|
|
|
|
|
Hear hear!
And the foreach is more readable.
|
|
|
|
|
SledgeHammer01 wrote: Whats wrong with a foreach loop?
I <now> find the List<>.Where syntax in 1 line much more readable than 5 lines of a well laid out foreach code.
I don't consider Linq elitist, then again I only use the simplest Linq and will resort to normal style if it gets too complex or it looks like taking too long to work out the Linq syntax.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yeah, but my 5 line foreach loop is 2x to 3x faster then your 1 line LINQ . Don't get me wrong, I sometimes use LINQ myself. I was just pointing out that in this case, it is silly of the OP to use LINQ over a basic foreach.
|
|
|
|