• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Is it okay to call private method in constructor and call class public method with object initialize

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my class, i am creating a new token and calling service method which then delete a token. Instead of creating a new instance, generating new token and then calling service method, can i just create a new token in constructor and then call service method at the same time?

All the methods in GCService class require token to perform some operation like get balance, verify customer and etc.

I have read that it is legit to call non virtual methods in constructor. I have never used this approach before so not sure if its a normal practise and can this be improved?

Public Class Customer : Implements ICustomer
{
  //creating a new instance , setting class properties in constructor and then calling class method.

Dim result = New GCService(a,b).GetBalance(a);
}

public class GCService : IGCService
{
private string Token { get; set; }

//constructor
public GCService(string _a, string _a)
{
   //set some properties and get token
   Token = GetToken(_a, _b);
}

private string GetToken(string a, string b)
{
   //get token and return response
   return response;
}
}
 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Yes, you can call non-virtual methods from constructors. However, it's usually not a good idea to have constructors perform expensive operations. If you get the tokens from an external service, you may want to lazily initialize the token field when you actually need it:
 
It's exactly the same and completely different as this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic