473,241 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Custom Attribute trouble

Hello,

I've been dinking around with using Custom Attributes at the
encouragement of an answer to a previous post and found it pretty easy
to create and set them. But now I am having a lot of trouble figuring
out how to get the values out. I think I am overcomplicating it. (I am
going to leave the terribly awful code I am messing with out.)

I have the following:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class fooFieldAttribute : System.Attribute
{
public int RequiredLength { get; set; }
public bool LeadingZeros { get; set; }
public fooFieldAttribute()
{
}
}
public class fooHeader
{
private string _HeaderIdentifier = "H1"
[fooField(RequiredLength = 2, LeadingZeros = false)]
public string HeaderIdentifier
{
get { return _HeaderIdentifier ; }
}

private string _Count = "1"
[fooField(RequiredLength = 3, LeadingZeros = true)]
public string Count
{
get { return _Count ; }
}
private string FormatWithPadding(string str, int reqlength, bool
leadingzeros)
{
bool useDefault = string.IsNullOrEmpty(str);

Int32 i = reqlength - str.Trim().Length;
if (leadingzeros && !useDefault)
{
return str.Trim().PadLeft(i, '0');
}
else
{
return str.Trim().PadRight(i, ' ');
}
}

Now I want to do something like:

public void Write(fooHeader fooheader)
{
StringBuilder strHeader = new StringBuilder();
strHeader.Append = FormatWithPadding(fooHeader.HeaderIdentifier,
fooFieldAttribute.RequiredLength for HeaderIdentifier,
fooFieldAttribute.LeadingZeros for HeaderIdentifier);
strHeader.Append = FormatWithPadding(fooHeader.Count,
fooFieldAttribute.RequiredLength for Count,
fooFieldAttribute.LeadingZeros for Count);

Can anyone help?

}

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
3 1003
On Fri, 23 May 2008 16:35:23 -0700, Rowan <an***********@yahoo.comwrote:
Hello,

I've been dinking around with using Custom Attributes at the
encouragement of an answer to a previous post and found it pretty easy
to create and set them. But now I am having a lot of trouble figuring
out how to get the values out. I think I am overcomplicating it. (I am
going to leave the terribly awful code I am messing with out.)

[...]
Can anyone help?
AFAIK, you need to use reflection (put this method somewhere convenient):

static fooFieldAttribute GetFooFieldAttribute(Type type, string
strMember)
{
object[] rgobj =
type.GetMember(strMember).GetCustomAttributes(type of(fooFieldAttribute),
true);

// If the attribute is present, return the first instance
if (rgobj.Length 0)
{
return rgobj[0];
}

return null;
}

Then you could write:

public void Write(fooHeader fooheader)
{
StringBuilder strHeader = new StringBuilder();
fooFieldAttribute ffaHeader = GetFooFieldAttribute(typeof(fooHeader),
"HeaderIdentifier"),
ffaCount = GetFooFieldAttribute(typeof(fooHeader), "Count");

strHeader.Append = FormatWithPadding(fooHeader.HeaderIdentifier,
ffaHeader.RequiredLength, ffaHeader.LeadingZeros);
strHeader.Append = FormatWithPadding(fooHeader.Count,
ffaCount.RequiredLength, ffaCount.LeadingZeros);
}

Or something like that. I didn't bother to compile any of that, so my
apologies in advance for typos or minor conceptual problems. :)

Pete
Jun 27 '08 #2
Thanks...this has been a very big help. I am having a little trouble
with a cast and am unsure what to do.

When I try to return rgobj[0]; I get the error
Cannot implicitly convert type 'object' to ...fooFieldAttribute'. An
explicit conversion exists (are you missing a cast?)

Is there something easy I am overlooking? I've tried quite a few
variations with no luck.
Jun 27 '08 #3
Actually, I think I got it...return rgobj[0] as fooFieldAttribute;
works! I appreciate your help...soooo much better than what I was
messing with.

On May 27, 12:06*pm, Rowan <MaryERo...@gmail.comwrote:
Thanks...this has been a very big help. *I am having a little trouble
with a cast and am unsure what to do.

When I try to return rgobj[0]; I get the error
Cannot implicitly convert type 'object' to ...fooFieldAttribute'. An
explicit conversion exists (are you missing a cast?)

Is there something easy I am overlooking? *I've tried quite a few
variations with no luck.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: F. Da Costa | last post by:
Hi, I was wondering *why* there is a difference between the results of the following two statements. On the suface they seem to do the same (or do they?) frm => returns void ...
8
by: Martin Lapierre | last post by:
I try to make a custom CodeAccessSecurityAttribute, but hit the wall at every corner. I created a simple custom security attribute, which is working (see below). But... 1) I have to put the...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
21
by: One Handed Man \( OHM - Terry Burns \) | last post by:
When using a custom control. In order to check and see if values have changed one has to implement the IPostBackDataCollection interface. The values returned for the control seem to be simply a...
0
by: jahyen | last post by:
I'm having trouble getting my custom control to save properties that are decorated with the PersistenceMode.InnerProperty attribute. The properties that are saved with PersistenceMode.Attribute...
0
by: Will Waggoner | last post by:
I'm having trouble getting custom attributes to work properly. When I copy and paste the tutorial from the documentation into a console app, it works fine. The following code in a web form,...
7
by: Julian Jelfs | last post by:
Hi, I had an aspx pag in .Net 1.1 with a label on it. As such I had a code behind page with a declaration for that label. When I convert to Asp.Net 2.0 the code behind is converted to a...
3
by: Mr Newbie | last post by:
I'm testing error handling configurations and having some trouble. I created a WebForm called. ErrDefault.aspx and I am trying to use the Page error attribute to force the redirection to a custom...
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.