473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

calling a private constructor

375 256MB
Hello
I have created a class also a constructor which is (should be, one of the conditions) private.
Now I am supposed to call it in the main program. My team leader says there is a trick to call it. I can call the variable j(as it is static). but not con.
Please help me out.
Expand|Select|Wrap|Line Numbers
  1. public class con 
  2. {
  3.     private con() 
  4.     {
  5.         Console.WriteLine("private constructor"); 
  6.     }
  7.     public static int j = 5; 
  8. }
  9. }
  10.  
Thanking you
cmrhema
Sep 10 '07 #1
15 1488
weaknessforcats
9,208 Expert Mod 8TB
This is C#.

I am moving this to the .NET forum.
Sep 10 '07 #2
Plater
7,872 Expert 4TB
You would need to make a public wrapper for that constructor I think

so like:
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2. {
  3.    public myclass(string temp)
  4.    {
  5.       return myclass();
  6.    }
  7.    private myclass()
  8.    {//private constructor
  9.    }
  10. }
  11.  
Sep 10 '07 #3
Frinavale
9,735 Expert Mod 8TB
You would need to make a public wrapper for that constructor I think

so like:
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2. {
  3.    public myclass(string temp)
  4.    {
  5.       return myclass();
  6.    }
  7.    private myclass()
  8.    {//private constructor
  9.    }
  10. }
  11.  
I'm just wondering why you would want to have a private constructor only to expose it publicly? Why not just make it public since you need to call it in another function anyways?

-Frinny
Sep 10 '07 #4
r035198x
13,262 8TB
You would need to make a public wrapper for that constructor I think

so like:
Expand|Select|Wrap|Line Numbers
  1. public class myclass
  2. {
  3.    public myclass(string temp)
  4.    {
  5.       return myclass();
  6.    }
  7.    private myclass()
  8.    {//private constructor
  9.    }
  10. }
  11.  
Erm, constructors cannot return a value.

Perhaps you meant something like

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class myclass {
  3.     public myclass():this("") {
  4.  
  5.     }
  6.     private myclass(String s) {
  7.         Console.WriteLine("Private constructor called");
  8.     }
  9.     public static void Main() {
  10.         new myclass();
  11.     }
  12. }
Sep 10 '07 #5
Plater
7,872 Expert 4TB
Oops, yeah. I knew there was something funny about it but I was moving really quick at the time
Erm, constructors cannot return a value.

Perhaps you meant something like

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class myclass {
  3.     public myclass():this("") {
  4.  
  5.     }
  6.     private myclass(String s) {
  7.         Console.WriteLine("Private constructor called");
  8.     }
  9.     public static void Main() {
  10.         new myclass();
  11.     }
  12. }
Sep 10 '07 #6
cmrhema
375 256MB
Erm, constructors cannot return a value.

Perhaps you meant something like

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class myclass {
  3.     public myclass():this("") {
  4.  
  5.     }
  6.     private myclass(String s) {
  7.         Console.WriteLine("Private constructor called");
  8.     }
  9.     public static void Main() {
  10.         new myclass();
  11.     }
  12. }
Thanks for the reply

Yes I exactly meant very similiar thing. Infact I should write a method that should return a value. The only condition was that I should create a class and a private constructor inside it. Thereafter call in the main function.
I tried the above but it did not call.
Sep 11 '07 #7
r035198x
13,262 8TB
Thanks for the reply

Yes I exactly meant very similiar thing. Infact I should write a method that should return a value. The only condition was that I should create a class and a private constructor inside it. Thereafter call in the main function.
I tried the above but it did not call.
I didn't try it last night but I've just tried it now and it works.
Something must be rotten in the state of Denmark then.
Sep 11 '07 #8
cmrhema
375 256MB
I didn't try it last night but I've just tried it now and it works.
Something must be rotten in the state of Denmark then.

Sorry ro35198x
It works So silly of me to forget "static" main

thank you
regards
cmrhema
Sep 11 '07 #9
r035198x
13,262 8TB
Sorry ro35198x
It works So silly of me to forget "static" main

thank you
regards
cmrhema
No need to apologize for that static. What you need to apoligize for is spelling my name wrong.
It's r035198x not ro35..whatever.
Sep 11 '07 #10
cmrhema
375 256MB
No need to apologize for that static. What you need to apoligize for is spelling my name wrong.
It's r035198x not ro35..whatever.
Hello
r035198x
I was about to edit it(your name) because I wanted to add one more question to the problem.

Now I need to call a method inside the private constructor and I should call from the main. Kindly help
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class myclass {
  3.     public myclass():this("") {
  4.  
  5.     }
  6.     private myclass(String s) {
  7. // A method to be added here with a return value
  8.         Console.WriteLine("Private constructor called");
  9.     }
  10.     public static void Main() {
  11.         new myclass();
  12. // need to call the method and value here
  13.     }
  14. }
Thank you r035198x once again

regards
cmrhema
Sep 11 '07 #11
Plater
7,872 Expert 4TB
Now I need to call a method inside the private constructor and I should call from the main. Kindly help
I am assuming you mean you need the private constructor to call a method?
Just create another one in the class (mark it private if you want)
And tell the private constructor to call it.

This is starting to sound like a homework/project for school though.
Sep 11 '07 #12
r035198x
13,262 8TB
...
This is starting to sound like a homework/project for school though.
And for homework we really like to see some effort first.
Sep 11 '07 #13
cmrhema
375 256MB
And for homework we really like to see some effort first.
should have replied long long ago.
This is what I wanted

using System;
using System.Collections.Generic;
using System.Text;

namespace constructor
{
class Program
{
static void Main(string[] args)
{

myclass ji = myclass.con();

}
}
public class myclass
{

public myclass()
: this("")
{
}
private myclass(string a)
{
Console.WriteLine("private constructor opened");


a = "static";


}
public static myclass con()
{

Console.WriteLine("ccheckin insided static");

return new myclass();

}

}
}
Sep 28 '07 #14
r035198x
13,262 8TB
should have replied long long ago.
This is what I wanted

using System;
using System.Collections.Generic;
using System.Text;

namespace constructor
{
class Program
{
static void Main(string[] args)
{

myclass ji = myclass.con();

}
}
public class myclass
{

public myclass()
: this("")
{
}
private myclass(string a)
{
Console.WriteLine("private constructor opened");


a = "static";


}
public static myclass con()
{

Console.WriteLine("ccheckin insided static");

return new myclass();

}

}
}
1.) Please use code tags when posting code.
2.) What is this code supposed to be doing?
Sep 28 '07 #15
Plater
7,872 Expert 4TB
1.) Please use code tags when posting code.
2.) What is this code supposed to be doing?
It's the answer to his question. Ive seen that static constructor type pop up in peoples code a bunch lately. Must be a new style they're teaching these days.
Sep 28 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Grandma Wilkerson | last post by:
Hi, I have a class whose constructor accepts a Socket as an argument. The constructor then makes a call to Socket.BeginReceive(). The callback delegate passed to BeginReceive() is a method of my...
0
by: tiwy | last post by:
On Andreas Lagemann <andreas.lagemann@freenet.de> wrote: > > Class Base > { > public: > Base() {} > > Base(A* a, B* b); >
4
by: Girish Shetty | last post by:
> Hi All, > > Some Strange thing with C++ Constructer !! > I never knew that we can initialize an object / a variable more than once > using C++ Constructer till I started doing some RnD...
0
by: Mike Collins | last post by:
I apologize for using this newsgroup for what seems like a VB6 question, but I did not see a newsgroup for VB6. I also think I may not have the C# code setup correctly for calling from VB6. If...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
5
by: Bennett Haselton | last post by:
I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
2
by: dodger_web | last post by:
Hi, Is there a way to call a constructor from a constructor ? let's say i have this class : public class Dot { private int x; private int y;
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
4
by: Rob Blackmore | last post by:
Hi, Can anyone advise how to call a private constructor using reflection? I currently get the above error which is rectified by changing the New() to Public from Friend but I ideally wish to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.