I have static class as container for some static strings:
public static class Constants
{
public static string ERROR_MESSAGE_LOGIN= "Unable to login with {0}";
public static string ERROR_MESSAGE_SEND_MAIL= "Unagle to send mail to {0}";
public static string GetFormatedString(string constantName, object [] params)
{
return
string.Format((string)typeof(Constants).GetField(constantName).GetValue(null),params);
}
}
As you can see I want to return formatted string, that based on name provided. And it is seems to work. However, when I writing method call in some other class I want that it give me list of suggestion for first parameter that contains "ERROR_MESSAGE_LOGIN" and "ERROR_MESSAGE_SEND_MAIL".
I know that I can create enum with those name or just use it like
Constants.GetFormatedString(Constants.ERROR_MESSAGE_SEND_MAIL, someparams);
But is there any way to tell method there is list of string parameters that you accept and show it to user in Visual Studio IntelliSense?
Thank you.
Related
I'm attempting to construct a database using the C# Console, but I'm getting an error thrown that I can't seem to find the problem with. The problem is in my Query function, when I attempt to define the path based off of user input. Essentially, it looks into a given folder in the user folder, and within it another subfolder containing the files wherein is the database information. Im using the Path.Combine function to dynamically define the path throughout my code, but I'm getting the following error;
ArgumentNullException: Value cannot be null. (Parameter 'path3')
Even though all strings are defined and not null.
this is what the line of code looks like:
path = Path.Combine(Info.path, "infolder", classrooms.inventory, classrooms.geninv, userinput)
And here is all of the code that the strings in the path arguement are referencing:
class classrooms
{
public static string Teacher;
public static string Roomnumber;
public static string Changes;
public static string inventory = "inventory";
public static string geninv = "GenInv";
public static string classinv = "ClassInv";
}
and
class Info
{
public static string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
if necessary I can attach my full code.
Screenshot of Error
I figured it out.
It was an improperly defined string that the pathcombine function was using in another instance of the code, and it was just getting caught up once i reused the path.combine function. Thanks for all the answers, I wouldn't have thought to look at that without all the input.
Say I want to create a method that accepts enum instead of a string so I could specify a parameter instead of writing string value representing the passed parameter.
For instance, let's say mods are strings of names so each of the variants has items of it's own and do has to place a call to chosen item.
public void DoWithMode(chosenMod parModifier)
{
string SelectedM = parModifier.ToString();
dedicateLineTo(Seleted...) or something
}
public class Com
{
public enum chosenMode{dummy1, dummy2};
}
public class Tel:Com
{
public new emum chosenMode {Telmod1, Telmod2};
public Tel
{
DoWithMode(chosenMode.Tel1mode);
}
}
class fax:Com
{
emum new chosenMode {Faxmod1, Faxmod2};
public fax
{
DoWithMode(chosenMode.Faxmod2);
}
}
What if I would like to make a generic ChosenMode. Say, for example, phone and fax have some common usage with the Do() method, i could make some overrides but this is only 2 kinds what if I have 20 or more classes and each has its own items to pass?
So it's only strings or method overrides though I was thinking of the comfortable usage of enum rather a class of const strings.
What is the workaround for this case?
I could do something else using enum type but then intellisense would not auto-suggest it so i would need to write something like
DoSomething(namespaceA.classT.subclassV.classX....class.modifier.item)
I want to apply this:
private string _name;
public string name
{
get { return _name; }
set { _name = Fix(value); }
}
to all string the members of a class, and don't want to repeat the same code for all the class members.
An obvious solution would be to put that code on a class to handle the problem and declare all string members as: myString instead of string, however that would mean that I would have to access the main class members like this: email.fixed instead of just email.
So I was wondering, is there is some kind of template I can define and then apply easily?
You could create a Code Snippet for Visual Studio to handle building a property this way.
MSDN includes documentation on Creating a Code Snippet, which can include replacement parameters (the name).
You might want to research Aspect Oriented Programming, which allows you to easily do things like this.
http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS
Create a type with an implicit conversion to and from string:
public class FixedString
{
private string str;
public FixedString(string str){this.str = str;}
public static implicit operator FixedString(string str)
{
return new FixedString(Fix(str));
}
public static implicit operator string(FixedString fixed)
{
return fixed.str;
}
}
You can now create a property of this type, but treat it as if it's a string.
Create a regex replace and use Visual Studio's (v2012/2013) find and replace regex functionality.
For example let us say you have a field like this to change into a property
public string Name;
and you want to change it to have a backing field and the setter you desire.
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = Fix(value); }
}
The find /replace regex pattern in Visual Studio to find is this
public\s+string\s([^;]+);
the replace pattern (with appropriate line spacings and tabs) is this
private string _$1;\r\n\r\n\tpublic string $1\r\n\t{\r\n\t\tget\r\n\t\t\t{ return _$1; }\r\n\t\tset\r\n\t\t\t{\r\n\t\t\t\t_$1 = Fix(value);\r\n\t\t\t}\r\n\t\t}
Then step through the finds and do a replace as needed within your class.
I have done similar to add INotifyChange method calls on blank properties created after doing the code snippet <tab><tab>prop action.
First of all, apologies if I posted it in the wrong place, I'm new here and I'm not sure if I posted in the right place.
Well, I'm trying to build a generic search method, where I'll add search parameters to mount a SQL Query and execute it on the database. All that using C#. My goal is that the parameter corresponding to the field I'll search, to be a property of the class the method is in. For example:
public foo
{
public string CustomerCode { get; set; }
public string CustomerName { get; set; }
public void AddSearchParameter(???, EnumOperator Operator, object Value)
}
Whenever I want to specify a parameter to add on the search, I would like it to look like this:
foo xxx = new foo();
xxx.AddSearchParameter(foo.CustomerCode, EnumOperator.Equal, txtCustomerCode.text);
My question is how to do it?
If you are trying to pass the member information (so that the AddSearchParameter can inspect the MemberInfo and write suitable SQL), then you'd need to use either a string literal (i.e. "CustomerCode"), or an expression tree. The latter is richer, but involves learning the Expression<T> API. But fundamentally:
public void AddSearchParameter(Expression<Func<object>> expression, ...)
...
xxx.AddSearchParameter(() => foo.CustomerCode, ...)
This, however, is not a trivial area of .NET.
If I were doing something like this, I would probably make the Search() method on foo check for the existence of values in the various this properties, and then build the query based on that.
public List<Results> Search()
{
if (!String.IsNullOrEmpty(this.CustomerCode))
{
// add search value to query
}
// etc.
}
Extending core classes in javascript is dead easy. I get the impression it's not quite so easy in C#. I was wanting to add some things to the String class so that I could do stuff like:
string s = "the cat's mat sat";
string sql = s.smartsingleQuote();
thus giving me
the cat''s mat sat
Is that even feasible, or do I have to write a function for that?
Yes it is possible using Extension Methods - MSDN
Here is a sample code.
public static class Extns
{
public static string smartsingleQuote(this string s)
{
return s.Replace("'","''");
}
}
Disclaimer : Not tested.
Yes you can do this, with an extension method. It'll look something like that:
public static class NameDoesNotMatter {
public static string smartSingleQuote(this string s) {
string result = s.Replace("'","''");
return result;
}
}
The magic is the keyword "this" in front of the first argument. Then you can write your code and it'll work:
string s = "the cat's mat sat";
string sql = s.smartsingleQuote();
You cannot accomplish exactly what you are talking about as the string class is sealed
You can accomplish the aesthetic of this by creating an extension method
public static class StringExtensions
{
public static string SmartSingleQuote(this string str)
{
//Do stuff here
}
}
The this keyword in the parameter allows you to take that parameter and put it in front of the method name for easier chaining like you requested done in your question. This, however, is equivalent to:
StringExtensions.SmartSingleQuote(s);
It just depends on your preference at that point :)
Here is a good SO answer on extension methods