Home > Software engineering >  Assigning Dynamic Variables from an Input Model C#
Assigning Dynamic Variables from an Input Model C#

Time:12-09

I am having some issues understanding how I can assign dynamic values from another class into other variables - I have tried using the correct namespaces, correct syntax and reading up on the documentation that the error provides - however no luck even when trying to implement examples shown. I have very little knowledge in regards to C# as I am mainly doing front end, however have to step up and start picking up some Back end oriented things at the company I work at

The current code I have is as follows:

BrazeConnectionInputs.cs

namespace Workflow.Connector.Braze.Models
{
    public class BrazeConnectionInputs
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

CreateCampaign.cs

public class CreateCampaignRunner
    {
        private const string Username = BrazeConnectionInputs.Username; // BrazeConnectionInputs.Username errors
        private const string Password = BrazeConnectionInputs.Password; // BrazeConnectionInputs.Username errors
    }

CodePudding user response:

You need to learn about objects vs classes. You should have an instance of the source class (BrazeConnectionInputs) that might be called something like model.

You can then explicitly assign across by creating a new instance of CreateCampaignRunner like var runner = new CreateCampaignRunner() and then assign the values in a number of ways:

  • Explicitly like runner.UserName = model.UserName
  • By using an explicit constructor var runner = new CreateCampaignRunner(model)
  • Object initializer syntax
  • Other ways are available

Highly recommend you do a basic C# course

  • Related