HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Multiple explicit cast operations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
operationsexplicitmultiplecast

Problem

This sample code works fine, but it looks awful. How would you improve this?

data.Add(((Adress)(((OwnerIDList)owner.Adresses.Value)[0].Adress.Value)).FirstName.Value.ToString());
data.Add(((Adress)(((OwnerIDList)owner.Adresses.Value)[0].Adress.Value)).LastName.Value.ToString());


Why do we use .Value in FirstName.Value.ToString()?

FirstName is a DTString object (implements a basic interface for all data types to be stored in data base).

Solution

at least I would extract a variable:

var address = (Adress)((OwnerIDList)owner.Adresses.Value)[0].Adress.Value;
data.Add(address.FirstName.Value.ToString());
data.Add(address.LastName.Value.ToString());


All these cast operations make me believe that your code is not that strongly typed. If it so then there is not that much you can do with readability in this code.

P.S.: Address in English has two d.

Code Snippets

var address = (Adress)((OwnerIDList)owner.Adresses.Value)[0].Adress.Value;
data.Add(address.FirstName.Value.ToString());
data.Add(address.LastName.Value.ToString());

Context

StackExchange Code Review Q#688, answer score: 8

Revisions (0)

No revisions yet.