|
|
They're used for a number of things:
To limit the use (amount of instances) of the class, see also Singleton Pattern.
Implicit and explicit casting support (So you can type 'myCommon A = (myCommon)"Hey there";').
Operator overloading ( +, -, *, / are operators), see also here[^].
|
|
|
|
|
Some methods don't need an instance of the class, so they can be made static, and invoked with the class name. E.g.
myCommon.GetCurrentDirectory ();
This can occur when the method doesn't reference any instance variables, but is still related to the class' purpose. The class is used to group related methods.
|
|
|
|
|
|
To perform operations that aren't specific to an instance of a class. For example, assuming an Employee class that contains a Guid Id member, the Delete() method could have either (or both of these) signatures:
1
2 public void Delete()
3 {
4 Employee.Delete (this.Id);
5 }
6
7
8 public static void Delete
9 (Guid id)
10 {
11 ....
12 }
/ravi
|
|
|
|
|
I generally try to avoid static in classes for examples like this.
The example given implies there is some global collection of Employees to/fomm which employees can be added, deleted, etc.
Instead, I prefer to make the collection explicit, with some collection List<employees>, or an explicit implementation class like EmployeeList.
That way, its possible later to create multiple instances if needed, for example, it can improve testability massively too, as EmployeeList can be mocked.
In C#, I generally try to limit use of static to those methods the framework requires me too - operator overloading etc.
Even the use to construct values is suspect - instead use a factory object, implemented as a separate class, or a factory method on an existing class to create instances.
Gilad Bracha has written a good blog entry[^] that highlights many of the dangers of static much better than I could.
<edit>That talks mainly about static state - the following blog entry[^] discusses the issue in context of constructors, which are static methods.
|
|
|
|
|
Rob Grainger wrote: The example given implies there is some global collection of Employees to/fomm which employees can be added, deleted, etc.
Right. The example is typical of APIs one would encounter in a data access layer, where the store is a database.
/ravi
|
|
|
|
|
Hello,
I have a very simple problem but I don't now how to search about this on Google.
I have a url I don't know it is in Unicode or what I am pasting it please if any one help me regarding this.
&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;
actually this is a part of url and I have to convert it to such coding to let my browser to understand it and I can use it.
Yes I know about HttpUtility.UrlDecode but as I told you earlier I don't know in which coding is my string, most when I paste my string in google and write that I want to decode it, google automatically convert it to original language, I want the same thing which google is doing but how?
Please help me out guys because I spend about 2 hours on this issue but no success.
Thanks in advance
|
|
|
|
|
I'm not sure quite what you are trying to do, but is it to display that string? Something like (I just threw this into a page of an MVC3 page to quickly test, but you can probably follow what I mean). It displays a Hebrew string - if that is what you are trying to do. But I may have interpreted you wrongly.
<p lang="he">@Html.Raw(@HttpUtility.HtmlDecode("&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;"))</p><pre>
|
|
|
|
|
http://easy.co.il[^]Thanks for reply
No am not trying to display this string my page.
Let me explain it again.
Following (1) and (2) is example
I have an url :
(1)
<a href="http://easy.co.il/&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;/">http://easy.co.il/&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;/</a>[<a href="http://easy.co.il/&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;/" target="_blank" title="New Window">^</a>]
I want to convert it to
(2) <a href="http://easy.co.il/%D7%97%D7%91%D7%A8%D7%95%D7%AA-%D7%AA%D7%99%D7%99%D7%A8%D7%95%D7%AA/">http://easy.co.il/%D7%97%D7%91%D7%A8%D7%95%D7%AA-%D7%AA%D7%99%D7%99%D7%A8%D7%95%D7%AA/</a>[<a href="http://easy.co.il/%D7%97%D7%91%D7%A8%D7%95%D7%AA-%D7%AA%D7%99%D7%99%D7%A8%D7%95%D7%AA/" target="_blank" title="New Window">^</a>]
As you can see (1) if I past it in browser it is not working and (2) is working fine.
So now I have a lot of urls like (1) and I have to convert them in to (2).
Please help me how I will do this conversion.
|
|
|
|
|
Please help me out , I am not able to solve this problem.
|
|
|
|
|
Hi,
I wrote a little decoder for it:
string s1="this is to be the input string, however the CP message editor page does not like Unicode";
string s2="";
for (int i=0; i<s1.Length; i++) {
if (s1[i]=='&') {
int n=0;
for (int j=6; j<10; j++) {
char c=s1[i+j];
int k="0123456789".IndexOf(c);
n=10*n+k;
}
s2+=(char)n;
i+=10;
} else {
s2+=s1[i];
}
}
log("s1="+s1);
log("s2="+s2);
It turns your specially encoded string into a regular Unicode string, which browsers seem to accept well.
Warning: there isn't any error checking as it is, you may want to improve on that. All I did is assume that every ampersand is the start of a 10-character sequence holding a 4-digit decimal number, which really should be checked.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
and this is a lot safer already:
string s1="the input string";
s1=HttpUtility.HtmlDecode(s1);
string s2="";
for (int i=0; i<s1.Length; i++) {
if (s1[i]=='&' && s1[i+1]=='#') {
i+=2;
int n=0;
for (; ; ) {
char c=s1[i++];
int k="0123456789".IndexOf(c);
if (k<0) break;
n=10*n+k;
}
s2+=(char)n;
i--;
} else {
s2+=s1[i];
}
}
log("s1="+s1);
log("s2="+s2);
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Wednesday, April 6, 2011 6:30 AM
|
|
|
|
|
thank you very much, It really help me out.
Thanks again.
|
|
|
|
|
You're welcome.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
A bit ugly, but seems to work...
private static void TestDecode()
{
UTF8Encoding enc = new System.Text.UTF8Encoding();
string output = "";
string smain = "&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;";
int len = smain.Length;
int pos = 0;
uint acode = 0;
while (pos < smain.Length)
{
int pos2 = -1;
if (smain.Length >= pos + 7 && smain.Substring(pos,6) == "&#")
{
pos2 = smain.IndexOf(";",pos + 6);
}
if (pos2 >= pos + 7 && uint.TryParse(smain.Substring(pos + 6, pos2 - pos - 6), out acode))
{
char[] c = { Convert.ToChar(acode) };
byte[] b = enc.GetBytes(c);
for (int i = 0; i < b.Length; i++)
{
output += String.Format("%{0:x2}", b[i]).ToUpper();
}
pos = pos2 + 1;
}
else
{
output += smain[pos];
pos += 1;
}
}
Console.WriteLine(output);
}
modified on Wednesday, April 6, 2011 7:05 AM
|
|
|
|
|
Thank you very much davidnz you guys really help me out.
Seriously I was stuck and you guys show me how to solve this problem.
|
|
|
|
|
You're welcome. I tidied mine up a bit this morning but have a feeling it could still be a lot simpler. I'm not much of a regex guru, but this is I think a lot tidier than my first attempt.
private static void TestReg2()
{
UTF8Encoding enc = new System.Text.UTF8Encoding();
string output = "";
string smain = "&#1495;&#1489;&#1512;&#1493;&#1514;-&#1514;&#1497;&#1497;&#1512;&#1493;&#1514;";
int len = smain.Length;
int pos = 0;
uint acode = 0;
smain = Regex.Replace(smain,@"&#[0-9]+;",(m) => {
Match m2 = Regex.Match(m.Value, @"[0-9]+");
if (m2.Length > 0)
{
acode = uint.Parse(m2.Value);
output = "";
char[] c = { Convert.ToChar(acode) };
byte[] b = enc.GetBytes(c);
for (int i = 0; i < b.Length; i++)
{
output += String.Format("%{0:x2}", b[i]).ToUpper();
}
return output;
}
else
return "";
});
Console.WriteLine(smain);
}
|
|
|
|
|
I am building a Windows Mobile Application for work and I am having an issue. I tried using VB and was successful but it is buggy and did not want to deal with all the bugs involved so I am gonna try C#.
I have the base which is a tabbed webrowser and several menu items.
What I cannot figure out is how to make one of those menu items link to a webpage in the browser.
So basically I have a menu item I want to link to a website and not sure what code I would use to do this.
Thanks in Advanced.
|
|
|
|
|
Hi,
if you have myWebBrowser , an instance of the System.Windows.Forms.WebBrowser class, you can make it navigate to some URL by issuing the statement myWebBrowser.Navigate(URLstring) .
Normal tabbed browsers have one browser-like control per tab page, so each of them can be active (e.g. fetching web pages) at the same time. So you may want that too.
PS: the way .NET works, whatever you tried in VB.NET will in the end look pretty much the same in C#. You are using the same classes after all, it is mainly a difference of syntax you will notice.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
It is telling me that myWebbrowser does not exist in this context; this is the issue I keep running into I just thought it was my coding (I guess it is lol)
|
|
|
|
|
Thanks for your help I was not able to make it work but you got me thinking and I figured it out I am using the following code.
WebBrowser thiswebpage = GetCurrentWebBrowser();<br />
thiswebpage.Url= new Uri("http://google.com");
|
|
|
|
|
hello
i need help i am working on database and i have connected it to the c# i need small problem i have created a serach button but i dont want to show the result on data grid view if u can help plz give me the answer and the code
ty
|
|
|
|
|
|
ace300 wrote: hello i need help i am working on database and i have connected it to the c#
i need small problem i have created a serach button but i dont want to show the
result on data grid view if u can help plz give me the answer and the code
I'm sorry, I don't have a clue what you're on about. You have a search button and you don't want to show the results on a data grid view. Well, don't write any code to bind the data to the data grid. Job done.
|
|
|
|