I'm still very new to ASP.Net and C# and would appreciate some advice on how to perform a lookup when the list of items will be permanently fixed. A database lookup has been discarded as the list has just 5 items.
I've written a class (see below) that contains an array of the 5 items and a single function that can be called to retrieve an item from the array.
I have to instantiate the class, of course, in the calling routine before using it. I was wondering if it would be possible to use a static class thereby avoiding the need to instantiate the class in the calling routine. I have done this with some enumerated constants that I could be referring to frequently and tried to write a class to do the same thing for this series of 5 items but cannot work out what the code should be.
I'd be grateful for advice on the best way of achieving the required functionality.
Neil
The code of the class I've written is:
public
classATNames{
privatestring[] typenames;public ATNames()
{
typenames =newstring[5];
typenames[0] ="Undefined";
typenames[1] ="Broad";
typenames[2] ="Generic Allele";
typenames[3] ="Precise Allele";
typenames[4] ="String Allele";
}publicstringthis[int index]
{
get
{
if (index < 0 || index >= typenames.Length)
{
//set to Undefined
index = 0;
}
return typenames[index];
}
}
}Using static would be a good idea;
public class ATNames{private static string[] typenames; static ATNames(){typenames = new string[5];typenames[0] = "Undefined";typenames[1] = "Broad";typenames[2] = "Generic Allele";typenames[3] = "Precise Allele";typenames[4] = "String Allele"; } public static string Name(int index){if (index < 0 || index >= typenames.Length){//set to Undefinedindex = 0;}return typenames[index];}}To use;
Response.Write (ATNames.Name(1));
Aidy - Thanks, that worked great.
One last question on this concerning the use of static.
I was under the impression that in order to use a class you had to instantiate it in your code before using any of its properties, methods, etc unless your code readpublic static classclassname.
In your example code it is the constructor and Name method that are defined as static. Could you explain why a line like
txtAT.Text =ATNames.Name(3);
works without there first being a line such as
ATNames atn = new ATNames();
in the calling program.
Thanks again
Neil
I mistyped what I've written in the previous reply I meant to say:
Could you explain why a line like
txtAT.Text =ATNames.Name(3);
works when I would have expected to have had to use:
ATNames atn = new ATNames();
txtAT.Text =atn.Name(3);
in the calling program.
Thanks
Neil
Name is a static method. That's why you don't need to make an instance of the class ATNames before calling it.
--
Tarjei
A static method exists on the class itself. A non-static method exists on an instance of a class.
The result is that as static methods are tied to the class they can not be used to manipulate instance-specific data.
For example;
string myString1 = "Hello";string myString2 = "World";System.Diagnostics.Debug.WriteLine (myString1.CompareTo(myString2));System.Diagnostics.Debug.WriteLine (string.Compare(myString1, myString2));
In the above CompareTo is a non-static method and Compare is a static one. Being non-static, CompareTo needs an instance of the class, in this case myString1 is an instance, and CompareTo will compare *that* instance with myString2.
Compare is static and a method of the class itself (string being the class in question). As it is static it doesn't relate to a specific instance of string, so if we want to compare two strings we need to pass them both to the method.
Hi,
maybe I'm missing something, but can I know why you aren't using an enumeration?
Garbin:
Hi,
maybe I'm missing something, but can I know why you aren't using an enumeration?
I'm not using an enumeration as the return value needs to be a string.
I wasn't aware that you could have an enumeration of strings.
Hi,
zoetosis:
I wasn't aware that you could have an enumeration of strings.
yes, in the sense that you can get the named constants as strings. For example, if you declare an enum like
public enum MyEnum{ Undefined, Broad}you can then write a statement like:
string val = Convert.ToString(MyEnum.Undefined);or
string val = MyEnum.Broad.ToString();
Garbin:
Hi,
zoetosis:
I wasn't aware that you could have an enumeration of strings.
yes, in the sense that you can get the named constants as strings. For example, if you declare an enum like
public enum MyEnum{ Undefined, Broad}you can then write a statement like:
string val = Convert.ToString(MyEnum.Undefined);or
string val = MyEnum.Broad.ToString();
I can see that working however the last three strings all have two words (e.g. Generic Allele) so the enumeration technique wouldn't work in those cases.
Hi,
what about GenericAllele or Generic_Allele?
Garbin:
Hi,
what about GenericAllele or Generic_Allele?
It would certainly work but I doubt if the users would be very impressed![]()
Enclose it in brackets;
Garbin:
Hi,
what about GenericAllele or Generic_Allele?
[Generic Allele],
Then to use;
myEnum.[Generic Allele]
Aidy:
Enclose it in brackets;
Garbin:
Hi,
what about GenericAllele or Generic_Allele?
[Generic Allele],Then to use;
myEnum.[Generic Allele]
Aidy - I don't understand how you're suggesting the enum be set up. I tried the following but it gives meIdentifier expected syntax errors on the first two entries and']' expected syntax errors on those with two words:
publicenumAntigenTypeNames
{
[Undefined],
[Broad],
[Generic Allele],
[Precise Allele],
[String Allele],
}
Hi,
zoetosis:
It would certainly work but I doubt if the users would be very impressed
for display purposes, you could always format the string:
string str = myEnum.Generic_Allele.ToString().Replace('_',' ');
0 comments:
Post a Comment