Can you use an in-line method like this in C#?

  • Thread starter Thread starter Edward
  • Start date Start date
E

Edward

Can you do this in C#?

FKCreatedBy = (aspnet_Membership.Key != null ? aspnet_Membership.Key :
GetGuestGuid());

(this is the method GetGuestGuid)

public static Guid GetGuestGuid()
{
// Guest is declared as a private const elsewhere
aspnet_Membership am = aspnet_Membership.GetUser(Guest);
return am.UserId;
}

When I run the code in debug mode I can see that it's returning a GUID
in return am.UserId, but this is never assigned to FKCreatedBy.

Thoughts?

Thanks

Edward
 
And this FKCreatedBy line is called from ? My guess is that this line is
never executed...

The issue is still a bit unclear as the terminology seems a bit confusing
but you could just expose this the exact same way you exposed GetGuestGuid
and it should be fine... Else where is your FKCreatedBy statement ? (for now
it would appears to me you are trying to use this like a macro ???)
 
The overall syntax is valid, though "purists" will doubtless throw up their
arms in despair... ;-)

Why? It's quite economical.
Pull it to pieces and step through it - you'll soon see where the problem
is:

if (aspnet_Membership.Key != null)
{
    FKCreatedBy = aspnet_Membership.Key;}

else
{
    FKCreatedBy = GetGuestGuid();

}

How is FKCreatedBy defined...?

It's a column in a Linq mapping, used thus in the Submit from a record
entry form:

public void Submit(Customers record)
{
bool IsNewRecord = record.CustomersID == 0;

SalesPortalDataContext dc = new SalesPortalDataContext
();
Customer rec;
if (!IsNewRecord)
{
rec = dc.Customers.FirstOrDefault(p => p.CustomersID ==
record.CustomersID);
}
else
{
rec = new Customer
{
FKCreatedBy = (aspnet_Membership.Key != null ?
aspnet_Membership.Key : GetGuestGuid()),
Created = DateTime.Now
};
etc.
 
This just in;

Guid temp = (Guid)(aspnet_Membership.Key != null ?
aspnet_Membership.Key : GetGuestGuid());

The variable temp contains the correct value when the code is
executed.

Don't understand.

Edward
 
And what if you try this exact same code before creating a new customer
instance (using the temp Guid you just got) and step through the code ?

It would allow to see if you properly read the guid. Could it be that this
value is properly initalized but later modified ?

--
Patrice


"Edward" <[email protected]> a écrit dans le message de

This just in;

Guid temp = (Guid)(aspnet_Membership.Key != null ?
aspnet_Membership.Key : GetGuestGuid());

The variable temp contains the correct value when the code is
executed.

Don't understand.

Edward
 
And what if you try this exact same code before creating a new customer
instance (using the temp Guid you just got)  and step through the code ?

It would allow to see if you properly read the guid. Could it be that this
value is properly initalized but later modified ?

Ah. Hangs head in shame. Sorry. Hadn't noticed that, for some
reason, the person who wrote the code, not content with initialising
the record with FKCreatedBy in the constructor, also explicitly set it
later in the code.

Thanks everyone.

Edward
 
Back
Top