|
|
ClassA should provide a static method or a static property which returns the instance created inside it. This method/property will help others access that instance.
|
|
|
|
|
If having a static reference (or the Singleton pattern, basically the same thing) is inappropriate, you could pass it in in the constructor or as a method parameter on class B.
I'm surprised this works, though - maybe it's the pseudo-code you've presented, but this:
public class ClassA()
{
private ClassA myClass = new ClassA();
}
is the same as:
public class ClassA()
{
private ClassA myClass = null;
public ClassA()
{
myClass = new ClassA();
}
}
Won't this end up with a stack overflow?
|
|
|
|
|
GlobX wrote: Won't this end up with a stack overflow?
Why not just run it and see for yourself. I guess it will.
|
|
|
|
|
It was more intended as a rhetorical question, like a "have you thought about this...?" 'sides, too busy hating on BizTalk to play around
|
|
|
|
|
GlobX wrote: hating on BizTalk
Just work on SharePoint as well and you have witnessed hell.
|
|
|
|
|
Pass it to the constructor or a property.
B b = new B() ;
A a = new A ( b ) ;
or
a.B = b ;
|
|
|
|
|
Hi,
You need to use the Singleton pattern to achieve this. Please find the example below
public class A
{
private static A _instance;
private A()
{}
public static A GetInstance()
{
if(_instance == nulll)
_instance = new A();
return _instance;
}
}
public class B
{
public void Somemethod()
{
A a = A.GetInstance();
.
.
.
do somethig
.
.
}
}
Note that we make the constructor of class A private. And class A holds a instance of itself in _instance variable. This is for to make sure that an out side method cannot create an instance of A. Then we will provide a static method in class A like GetInstance where we create a new instance if an instance is already not exist and return the current instance
Hope this helps
Nitheesh George
http://www.simpletools.co.in
|
|
|
|
|
No, never a singleton, especially with .net languages; they're not needed and generally a symptom of poor design. I have never found a situation that wuold benefit from a singleton.
Nitheesh George wrote: cannot create an instance of A
In .net everyone has access to your privates.
And give this[^] a read.
|
|
|
|
|
Hi,
I want to separate number and date and time from a string in c#. here is an example:
+CMGR: "REC READ","+923365483536",,"10/09/15,08:16:11+20"
from this i want to put cell number in one array and date and time in another.I dont know how to do this.
Thanks for any kind of help in advance
Regards,
Aeman
|
|
|
|
|
Well, you could always use a string split on this based on the commas and take the array items at index 1 and index 3.
|
|
|
|
|
index 3 and 4!
I cannot remember: What did I before google?
|
|
|
|
|
So cell isn't stored in the bit +9....? Index 3 is an empty string in his example.
Nevermind - I see what you're saying. I'd missed the , in the date.
I'm not a stalker, I just know things. Oh by the way, you're out of milk. Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Onyx
modified on Tuesday, March 29, 2011 9:40 AM
|
|
|
|
|
You could use a regular expression but then you may end up with not one problem but two
using System.Text.RegularExpressions;
...
string txt = "+CMGR: \"REC READ\",\"+923365483536\",,\"10/09/15,08:16:11+20";
Match m = Regex.Match(txt, @"[0-9]{2}/[0-9]{2}/[0-9]{2},[0-9]{2}:[0-9]{2}:[0-9]{2}");
if(m.Success)
{
String date = m.Value.Replace(",", " ");
DateTime dt = DateTime.Parse(date);
}
The snippet above should work but you may want to refine the expression. I just hacked that one up quickly
"You get that on the big jobs."
|
|
|
|
|
May I humbly suggest Rive[^]?
|
|
|
|
|
Try to use regular expression,MAN.
Hope this helps.
Hi, i am from china.Like to make friends with you from all of the world.
|
|
|
|
|
It's very easy
First store the entire string in a string variable
and then use the split method and assign it to a character array
split(",,")
and from that array fetch the appropriate value like
string s="+CMGR: "REC READ","+923365483536",,"10/09/15,08:16:11+20";
char[] temp=s.split(",,");
now your temp[1] consists both time and date.
if you want separate both use the same technique.
|
|
|
|
|
Hi,
I get a program c# when googling that validates the IBAN :
static bool isIban (string code_iban)
{
code_iban = code_iban.ToUpper () ;
StringBuilder sb = new StringBuilder ();
foreach ( char car in code_iban)
{
if (char.IsDigit (car) || char.IsLetter (car))
sb.Append (car);
}
code_iban = sb.ToString ();
if (code_iban.Length < 15 || code_iban.Length > 34)
return false;
string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
StringBuilder sb2 = new StringBuilder ();
foreach (char c in iban)
{
int entier;
if (char.IsLetter(c))
entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
else
entier = Convert.ToChar (c) - Convert.ToChar ("0");
sb2.Append (entier);
}
string checkSumString = sb2.ToString();
int checksum = int.Parse(checkSumString.Substring(0, 1));
int i = 1;
while (i < checkSumString.Length)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum = checksum % 97;
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
}
if (checksum == 1)
return true;
else
return false;
}
}
I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
ty
|
|
|
|
|
Well, Math.Max is retrieving the maximum of i or i-1. Interlocked.Increment is used to increment i and store it all as an atomic action.
|
|
|
|
|
Hi,
If u can give me the // algorithme of this.I can do this like this simply
Math.Max( ref i , i-1)
i ++;
why using of Interlocked.Increment???????
ty
|
|
|
|
|
You shouldn't. In this case, it's absolutely no use whatsoever - frankly, it looks like somebody is trying to be too clever for their own good. Interlocked increment only makes sense in multi-threaded scenarios where there is a possibility that somebody else may increment a shared variable. As i is local, it's pointless. This code looks like obfuscation through stupidity.
|
|
|
|
|
Pete O'Hanlon wrote: obfuscation through stupidity
5
|
|
|
|
|
This line:
System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
seems utterly stupid to me. Comparing a number which is increased to what it was before and taking the maximum will always return the increased number. Even so, nothing is done with the return value. And why is this increase done using the interlocked class? Doesn't look terribly multi-threaded to me and if it is then you need additional locking.
i++ is probably what you want, and I would expect the loop initialisation to set i = 0 , rather than i = 1 on casual inspection.
Regards,
Rob Philpott.
|
|
|
|
|
it doesn't make sense at all; i is a local variable, no other thread can touch it, so the whole statement boils down to i++ and the while loop should have been a for loop.
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.
|
|
|
|
|
You have already asked about this here[^]
I know the language. I've read a book. - _Madmatt
|
|
|
|