Give an Object a Method in C# - c#

Is it possible to give a C# Object like
public string Name
{
get { return _name; }
set { _name = value; }
}
a Method doing something like:
private void addTextToName(){
_name = _name + " - Test";
}
so that I can call it like
Name.addTextToName();
Because (where I come from) in JavaScript you can do such things with .prototype
Is there any way to do this in C#?

If you are asking can I add a method to a string? then yes. Look at extension methods.
public static string AddTextToName(this string s)
{
return s + " - Test";
}
Use it like this:
"Hello".AddTextToName();
Will return Hello - test.

Yes, there is a way for C# Objects (you used a string there, but though...).
Take a look at the so-called "extension methods" in C# as they are exactly what you need I think.
For further reference, look e.g. here: https://msdn.microsoft.com/en-us/library/vstudio/bb383977%28v=vs.110%29.aspx (the magic is in the this as parameter for the method)

Using the extension method.
class Program
{
static void Main()
{
Example e = new Example();
e.Name = "Hello World";
var x = e.Name;
var y = x.addTextToName();
Console.WriteLine(y);
Console.ReadLine();
}
}
class Example
{
public string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public static class MyExtensions
{
public static string addTextToName(this string str)
{
return str += " - Test";
}
}

Related

Can't set data into list and then print it.

I am trying to store data into a complex type list and then print it later. I am trying to use properties and then print it but it seems like I am missing something. Please help.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Color");
string color = Console.ReadLine();
Console.WriteLine("Enter transmition");
string transmition = Console.ReadLine();
Console.WriteLine("Enter Name");
string name = Console.ReadLine();
List<DatabaseCar> arr = new List<DatabaseCar> { };
DatabaseCar databaseCar = new DatabaseCar(color, transmition, name);
arr.Add(databaseCar);
foreach (DatabaseCar data in arr)
{
Console.WriteLine(data);
}
Console.ReadKey();
}
}
abstract class Data
{
protected string color;
protected string engine;
protected string name;
public abstract void set(string color, string engine, string name);
public string Color
{
get
{
return color;
}
}
public string Engine
{
get
{
return engine;
}
}
public string Name
{
get
{
return name;
}
}
}
class DatabaseCar : Data
{
public override void set(string color, string engine, string name)
{
this.color = color;
this.engine = engine;
this.name = name;
}
public DatabaseCar(string color, string engine, string name)
{
set(color, engine, name);
}
}
The result I get from it is:
Enter Color:
Red
Enter transmition:
Manual
Enter Name:
John
ConsoleApp1.DatabaseCar
This is because Console.WriteLine calls ToString() on an object that isn't specifically supported by one of its other overloads. ToString() by default returns the string representation of the Type.
To address this, you'll have to override ToString() with a custom implementation that does what you want.
public override string ToString()
{
// construct the representation that you want and return it.
}
Your problem is in the way you print your data.
foreach (DatabaseCar data in arr)
{
Console.WriteLine(data);
}
data is an instance of the type DatabaseCar, which is a class defined by you. It contains many different properties of different types. So you need to tell C# how to print it.
There's two ways to go about it. One, a dedicated method to print an object of type DatabaseCar. You can customize how the print is done, this is a very basic example.
static void PrintDatabaseCar(DatabaseCar car)
{
Console.WriteLine("Name : {0}", car.Name);
Console.WriteLine("Color : {0}", car.Color);
Console.WriteLine();
}
Then you can call that method from your main like so:
foreach (DatabaseCar data in arr)
{
PrintDatabaseCar(data);
}
Option two, you can override the ToString() method and provide functionality to print an object of type DatabasCar. This method has to go in your DatabaseCar class definition.
class DatabaseCar : Data
{
public override void set(string color, string engine, string name)
{
this.color = color;
this.engine = engine;
this.name = name;
}
public DatabaseCar(string color, string engine, string name)
{
set(color, engine, name);
}
public override string ToString()
{
string result = string.Empty;
result += string.Format("Name : {0}", name) + Environment.NewLine;
result += string.Format("Color : {0}", color) + Environment.NewLine;
result += Environment.NewLine;
return result;
}
}
Got it.
Thanks Everyone
public override string ToString()
{
// construct the representation that you want and return it.
return String.Format("Color {0},Engine {1}, Name {2}",color,engine,name);
}

Trying to maintain DRY in a C# BLL

I am working inside a public BLL with a number of different static methods. Inside a few of these methods, I need to display a certain string to the user depending on an ID# passed into the method. The situation is identical across these few methods. It's currently like this:
public class myBLL
{
public static addByID(int ID)
{
string myString = grabString(ID);
//do some stuff
Console.Writeline("You have added: " + myString);
}
public static removeByID(int ID)
{
string myString = grabString(ID);
//do some other stuff
Console.WriteLine("You have removed: " + myString);
}
public static grabString(int ID)
{
if(ID == 1)
return "string 1";
else
return "string 2";
}
}
I feel like I am violating DRY in myMethod1 and myMethod2 because why bother calling grabString twice? However I cannot come up with a way around this.
What about to use delegate for different parts of methods
public static void myGeneralMethod(int ID, Action<string> method )
{
string myString = grabString(ID);
method(myString);
Console.WriteLine(myString);
}
public static void SomeStuffForAdd(string myString)
{
}
public static void SomeOtherStuffRemove(string myString)
{
}
Or if you want to keep Add/Remove:
public static void removeByID(int ID)
{
myGeneralMethod(ID, SomeStuff);
}
It's a bit hard to evaluate what you really are looking for. But might I suggest some sort of logger? Or maybe a general class that store the value for later use?
public class myBLL
{
public static addByID(int ID)
{
//do some stuff
LogString("You have added: {0}", ID);
}
public static removeByID(int ID)
{
//do some other stuff
LogString("You have removed: {0}", ID);
}
public static LogString(string message, int ID)
{
string myString = "";
if(ID == 1)
myString = "string 1";
else
myString = "string 2";
Console.WriteLine(string.Format(message, myString);
}
}
To be honest, I would remove those static function and create a proper class.
public class MyObject
{
int ID;
public GetString()
{
if(ID == 1)
return "string 1";
else
return "string 2";
}
}
That way, the string could even be cached.

Difficulties with using static functions and variables in C#

I have a class like this
#region Properties
private static string inputURL;
public static string InputURL
{
get { return inputURL; }
set { inputURL = value; }
}
private static string outputURL;
private static string ffBaseURL = "format=xml&";
public static string FFBaseURL
{
get { return ffBaseURL; }
set { ffBaseURL = value; }
}
private static string excludeParam = "fullurl,log";
public static string ExcludeParam
{
get { return excludeParam; }
set { excludeParam = value; }
}
private static string currentCategoryID = "234";
public static string CurrentCategoryID
{
get { return currentCategoryID; }
set { currentCategoryID = value; }
}
private static string navigationParameters = "query=*&log=navigation&filterCategoryId=" + currentCategoryID;
public static string NavigationParameters
{
get { return navigationParameters; }
set { navigationParameters = value; }
}
#endregion
#region Methods
public static string NavigationCall()
{
List<string> excludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
foreach (string key in HttpContext.Current.Request.QueryString.Keys)
{
if (!excludeParams.Contains(key))
{
FFBaseURL += key + "=" + HttpContext.Current.Request[key] + "&";
}
}
FFBaseURL += NavigationParameters;
if (Common.IsInternalIP())
{
FFBaseURL += "&log=internal";
}
outputURL = ffBaseURL;
return outputURL;
}
#endregion
As you can see I have a static function called NavigationCall() ,it is mandatory that this function remains static.And when I calls this function from my website the function returns wrong values in each function call because of the static properties i declared.We all know static properties will retain their values after the exection of the programe.
So lets say when i call these function first time I gets a result "tesresult1",second time when i reloads my webpage it gives me a result "testresult1testresult1".I think you got the problem now.
I Have tried to solve this issue by declaring static variable values again ,but it does not looks like a good way to programe things.
I tried to make the properties non static .but it returns error as NavigationCall() is a static function i can't call non static properties inside it.
Now I am searching for a correct way to resolve this issue, I think this problem came to me because of the wrong understanding of OOPS concept.Can any one lend a hand here to solve the case or if the issue is vast point to some resources where i can understand how to find a solution?
Instead of using static properties, you can pass all the parameters to your static method.
public static string NavigationCall(
string inputURL,
string ffBaseURL,
string excludeParam,
string currentCategoryID,
string navigationParameters
)
{
// the body of your method
}
You can also bundled all properties into Custom object and pass it to method. Also you have to make NavigationCall thread safe for any solution. Are static methods thread safe ?
public static string NavigationCall(CustomNavigation objCustomNavigation)
//Custom object.
public class CustomNavigation
{
public string InputURL {get;set;}
public string FBaseURL{get;set;}
public string ExcludeParam{get;set;}
public string CurrentCategoryID {get;set;}
public string NavigationParameters{get;set;}
}
I'd suggest to introduce a parameter object (as #mit suggested) and use the opportunity to encapsulate some of your logic there. This should instantly simplify your method. Maybe you could then make some of these properties private, because they'll only be needed in logic encapsulated in the parameter object.
//Your static method
public static string NavigationCall(CustomNavigation customNavigation)
//Disclaimer: I have no idea, whether this is an appropriate name.
//It really depends on what you want to do with his class
class CustomNavigation
{
public string InputURL { get; private set; }
public string FFBaseURL { get; private set; }
public IEnumerable<string> ExcludeParams { get; private set; }
public string CurrentCategoryID { get; private set; }
public string NavigationParameters { get; private set; }
public CustomNavigation(string inputUrl, string excludeParam, string fBaseUrl, string currentCategoryID, string navigationParameters)
{
// various guard clauses here...
NavigationParameters = navigationParameters;
CurrentCategoryID = currentCategoryID;
FFBaseURL = fBaseUrl;
InputURL = inputUrl;
// Parse string here -> Makes your method simpler
ExcludeParams = new List<string>(excludeParam.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
}
//Example for encapsulating logic in param object
public void AddKeys(HttpContext currentContext)
{
var keys = currentContext.Request.QueryString.Keys
.Cast<string>()
.Where(key => !ExcludeParams.Contains(key));
foreach (var key in keys)
FFBaseURL += key + "=" + currentContext.Request[key] + "&";
}
}

How to assign a value from a C# static method to a label

I have the following static function in c#
public static string Greet(string name)
{
string greeting = "welcome ";
// is it possible to pass this value to a label outside this static method?
string concat = string.Concat(greeting, name);
//error
Label1.text = concat;
//I want to return only the name
return name;
}
As you can see in the comments, I want to retain only the name as the return value, however I want to be able to take out the value of the concat variable to asign it to a label, but when i try the compiler refuses, can it be done? Is there a work around?
Thank you.
If the method must be static for some reason, the main approach here would be to pass any required state into the method - i.e. add a parameter to the method that is either the label or (better) some typed wrapper with a settable property like .Greeting:
public static string Greet(string name, YourType whatever)
{
string greeting = "welcome ";
whatever.Greeting = string.Concat(greeting, name);
return name;
}
(where YourType could be your control, or could be an interface allowing re-use)
What you don't want to do is use static state or events - very easy to get memory leaks etc that way.
For example:
public static string Greet(string name, IGreetable whatever)
{
string greeting = "welcome ";
whatever.Greeting = string.Concat(greeting, name);
return name;
}
public interface IGreetable {
string Greeting {get;set;}
}
public class MyForm : Form, IGreetable {
// snip some designer code
public string Greeting {
get { return helloLabel.Text;}
set { helloLabel.Text = value;}
}
public void SayHello() {
Greet("Fred", this);
}
}
Either non-static:
public string Greet(string name)
{
const string greeting = "welcome ";
string concat = string.Concat(greeting, name);
Label1.Text = concat;
return name;
}
Or still static passing the label like Greet("John", Label1):
public static string Greet(string name, Label label)
{
const string greeting = "welcome ";
string concat = string.Concat(greeting, name);
label.Text = concat;
return name;
}
But not sure why you need to return the name in either case...if you had it when calling the function, you already have it in the scope you'd be returning to. Example:
var name = "John";
Greet(name);
//can still call name here directly
The problem is that you try to instantiate a class variable from static method.
Maybe I'm missing the point but couldn't you just do:
public static string Greet(string name)
{
return string.Concat("Welcome ", name);
}
Then use it like:
string name = "John";
label1.Text = Greet(name);
Web methods do not have to be static.

Correct use of Properties to maintain a HUD in a nested Form?

Edit 1
Is it possible to do this with get/set? Something like the below? This works for me but I am worried I am missing something not to mention all the staticness.
///<summary>
/// Class to track and maintain Heads Up Display information
///</summary>
public static class HUD
{
///<summary>
///Declare variables to store HUD values
///</summary>
private static string _lastName;
private static string _firstName;
private static string _middleName;
private static string _suffix;
private static string _sSN;
private static string _personID;
private static string _masterID;
private static string _enrollmentID;
private static string _planID;
// Store a reference to THE form that holds the HUD and is visible
private static FrmModuleHost _frmHUDHost;
public static string PersonId
{
get { return _personID; }
set
{
FrmHudHost.tbxHUD_PersonID.Text = value;
_personID = value;
}
}
public static string SSn
{
get { return _sSN; }
set
{
FrmHudHost.tbxHUD_SSN.Text = value;
_sSN = value;
}
}
public static string MiddleName
{
get { return _middleName; }
set
{
FrmHudHost.tbxHUD_MiddleName.Text = value;
_middleName = value;
}
}
public static string FirstName
{
get { return _firstName; }
set
{
FrmHudHost.tbxHUD_FirstName.Text = value;
_firstName = value;
}
}
public static string LastName
{
get { return _lastName; }
set
{
FrmHudHost.tbxHUD_LastName.Text = value;
_lastName = value;
}
}
public static string Suffix
{
get { return _suffix; }
set
{
FrmHudHost.tbxHUD_SuffixName.Text = value;
_suffix = value;
}
}
public static string MasterID
{
get { return _masterID; }
set
{
FrmHudHost.tbxHUD_MasterID.Text = value;
_masterID = value;
}
}
public static string EnrollmentID
{
get { return _enrollmentID; }
set
{
FrmHudHost.tbxHUD_EnrollmontPeriod.Text = value;
_enrollmentID = value;
}
}
public static string PlanID
{
get { return _planID; }
set
{
FrmHudHost.tbxHUD_PlanID.Text = value;
_planID = value;
}
}
public static FrmModuleHost FrmHudHost
{
get { return _frmHUDHost; }
set { _frmHUDHost = value; }
}
}
Original Post
I have a class that is responsible for updating a Heads Up Display of current selected member info. My class looks like this -->
public static class HUD
{
///<summary>
///Declare variables to store HUD values
///</summary>
public static string _lastName;
public static string _firstName;
public static string _middleName;
public static string _suffix;
public static string _sSN;
public static string _personID;
public static string _masterID;
public static string _enrollmentPeriod;
public static string _planID;
///<summary>
/// Method to update the display with current information
///</summary>
public static void UpdateHUD (FrmModuleHost frm, params String[] args)
{
frm.tbxHUD_LastName.Text = args[0];
_lastName = args[0];
frm.tbxHUD_FirstName.Text = args[1];
_firstName = args[1];
frm.tbxHUD_MiddleName.Text = args[2];
_middleName = args[2];
frm.tbxHUD_SSN.Text = args[3];
_sSN = args[3];
frm.tbxHUD_PersonID.Text = args[4];
_personID = args[4];
}
}
What I am trying to figure out is how I can tell what args are being passed. What you see below is what is called from the Search Page as that is all that is available at that point. The other 4 values will be loaded 1 at a time on various pages. A person HAS A enrollment which HAS A plan if that helps.
private void GetResults()
{
var lName = getCurrentRowVal("Last Name");
var fName = getCurrentRowVal("First Name");
var pID = getCurrentRowVal("Person ID");
var sSN = getCurrentRowVal("SSN");
var mName = getCurrentRowVal("Middle Name");
HUD.UpdateHUD(FrmWwcModuleHost, lName, fName, mName, sSN, pID);
}
Now when I call this from the Enrollment Page I will want everything to stay and add the EnrollmentID.
private void GetResults()
{
var enrollmentID = getCurrentRowVal("EnrollmentID");
HUD.UpdateHUD(FrmWwcModuleHost, enrollmentID);
}
My question is, How do I do that and know which arg[] index to call and not overwrite the existing values?
Is it as simple as always providing ALL parameters as they are set? So my call from the Enrollment Page would instead look like this -->
private void GetResults()
{
var enrollmentID = getCurrentRowVal("EnrollmentID");
HUD.UpdateHUD(FrmWwcModuleHost, HUD._lastName, HUD._firstName, HUD._middleName, HUD._sSN, HUD._personID, enrollmentID);
}
Thanks for any insights!
You'll really need to ditch the params style call and establish real parameters for your methods. Just create multiple overloads for your most common call signatures.
I did not see a reference to a particular version of .net you are using. Here is how I handle this in .net 3.5.
First create a class for passing the update values in, but make all of the properties nullable (since all of your items are string, they are already nullable). If the values are nullable, add actual property setters, and LastNameChanged properties.
public class UpdateData {
public string LastName { get; set;};
public string FirstName { get; set;};
public string MiddleName { get; set;};
...
}
Now your method signature looks like this:
public static void UpdateHUD (FrmModuleHost frm, UpdateData data)
{
if (!string.IsNullOrEmpty(data.FirstName) {
frm.tbxHUD_LastName.Text = data.FirstName;
_lastName = data.FirstName;
}
if (!string.IsNullOrEmpty(data.LastName) {
frm.tbxHUD_FirstName.Text = data.LastName;
_firstName = data.FirstName;
}
if (!string.IsNullOrEmpty(data.MiddleName) {
frm.tbxHUD_MiddleName.Text = data.MiddleName;
_middleName = data.FirstName;
}
Next is the setting the UpdateData and calling the method:
UpdateHUD(FrmWwcModuleHost, new UpateData{ FirstName = "test1", LastName = "test2", ...});
Final note: you are using a lot of statics here. You might consider changing most of them. Move the static variables to an actual class with properties (but no statics), and reference the class in your code.
Perhaps instead of
params String[] args
you should do
params KeyValuePair<String,String>[] args
where any given param's properies would be assigned something like this:
Key = "Last Name"
Value = "Hare"
Then inside your UpdateHUD method you could check to see what the Key of the parameter was so you would know which value was being passed.
You could also create a separate class to pass in the params array. Something like:
public class HUDParam {
public HUDParam(paramName, paramValue) { /*[init code here...]*/ }
public string Name { get; set; }
public string Value { get; set; }
}
Then:
HUD.UpdateHUD(frm, new HUDParam("FirstName", "Tom"),
new HUDParam("LastName", "Thompson");
In your update method, you can just check the name and set the appropriate value in your HUD.
C# 4.0 has optional params which are much different from the params keyword. Params keyword is literally no different at runtime than if you didn't have the params keyword at all. In other words, you just get an array of values with no way of knowing which are which. Params keyword is just a C# convenience at compile time.
Although I haven't used C# 4.0 optional parameters yet, I imagine they behave similarly to the way VB.NET optional parameters did in that you can specify a default value at the function and that default will be used if the caller doesn't specify it. But the caller still has to indicate (either with missing values or paramName:= syntax) which parameter they are specifying.
In your case I think you're better off using either normal named parameters and passing null when you don't need them, or pass a dictionary/hashtable instead.
You could pass the arguments with a Dictionary where you can assign a value to a certain name.
The cleanest version would be to make a method for each variable.
e.g.
void UpdateLastName(string name)
{
frm.tbxHUD_LastName.Text = _lastName = name;
}
If you can use Framework 4.0, you can use optional and named parameters. Until that time, you will need to pass all the arguments.
But, I think by your question you might misunderstand what's happening slightly. The arguments you send in params are going into the method as an argument. Their scope therefore is the method itself, so you not "overwriting" anything. The array that you used for the first call is gone (out of scope) and the array you use for the second call will have whatever you put in it. Best way to do optional parameters before 4.0 is using multiple method overloads.
If you have a name association with a value but possibly unknown name values this is a candidate for a Dictionary .
Optional parameters involve no less checking than checking a dictionary if a key exists or if a member of a class is null. Since there already exists a primitive for this and it's more flexible I see no reason to use variable parameters or optional parameters.
What I would do is use an ananoymous type (which can be turned into a dictionary at runtime) and then changing the values which are present.
New function definition:
public static void UpdateHUD (FrmModuleHost frm, object args);
And client code:
UpdateHUD(frm, new {
MiddleName = "Jed",
SSN = "..." });
Using the code from Roy Osherove's Blog, this can be turned into a dictionary. The new implementation would be similar to:
var dictionary = MakeDictionary(args);
if ( dictionary.ContainsKey("SSN") ) { _sSN = dictionary["SSN"]; }
Of course, this will all be irrelevant when C# 4.0 comes out.

Categories

Resources