0

Class1 is a class object which is already instantiated. Is there any alternative approach not to type every time Class1.?

Class1.Hostname = location.Ip;
Class1.UserName = location.Username;
Class1.Password = location.Password;
Class1.Port = 23;
Class1.GiveUpSecurityAndAcceptAnySshHostKey = true;
Class1.LogPath = "";
Class1.IsDebug = true;
Class1.Downloadwaitmiliseconds = 1000;
abc
  • 19
  • 3

4 Answers4

0

You can do it like this:

Class1 = new Class() 
{ 
  Hostname = location.Ip;
  UserName = location.Username;
  Password = location.Password;
  Port = 23;
  GiveUpSecurityAndAcceptAnySshHostKey = true;
  LogPath = "";
  IsDebug = true;
  Downloadwaitmiliseconds = 1000;

  AlreadySetPropety1 = Class1.AlreadySetPropety1;
  AlreadySetPropety2 = Class1.AlreadySetPropety2;
  …
}

That means that along your properties you have to set already set properties but in fact there is not issue in your code. You can refactor it to some method for instance if you don’t like personally.

Yarl
  • 728
  • 1
  • 7
  • 26
  • this is new instansiation and i am aware about this. – abc Mar 19 '19 at 10:49
  • You got me.Switch to VB as noted by moderators. Check also https://www.c-sharpcorner.com/UploadFile/a20beb/call-a-function-of-visual-basic-into-C-Sharp-page/. – Yarl Mar 19 '19 at 11:01
0

If you want to set the properties after instantiation, you can create a method that takes those as values as arguments and set them inside the method, where you would not need the variable name. Like a Fill method. Same as what you may do in the constructor of the class.

If you are able to do this during the instantiation you can do something like

var Class1= new YOURCLASS{
    Hostname = location.Ip;
    UserName = location.Username;
    Password = location.Password;
    Port = 23;
    GiveUpSecurityAndAcceptAnySshHostKey = true;
    LogPath = "";
    IsDebug = true;
    Downloadwaitmiliseconds = 1000;
};
kkica
  • 4,034
  • 1
  • 20
  • 40
0

You might add into your Class1 a set method, like:

class Class1 {
    public string Hostname { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public int Port { get; set; }
    public bool GiveUpSecurityAndAcceptAnySshHostKey { get; set; }
    public string LogPath { get; set; }
    public bool IsDebug { get; set }
    public int Downloadwaitmiliseconds { get; set; }

    public void setValues(string hostname, string username, string password, int port /* adding other values*/) 
    {
        Hostname = hostname;
        Username = username
        Port = port;
        /* The other values */
    }

}

Maybe is the way to do it if you have the object initialized

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0

There is no syntax that I am aware of to do that. You might be able to write a Setter along the lines of:

public void Set(hostname = null, username = null, password = null)
{
    if (hostname != null)
        Hostname = hostname;
    if (username != null)
        Username = username ;
    ...
}

and you can call it with explicit parameters:

Class1.Set(hostname="localhost", username="me")

Of course this only works when null is not a valid option for your properties

user1781290
  • 2,674
  • 22
  • 26