Use SSIS Variable in another class besides ScriptMain.cs - c#

I have a C# script task in SSIS that I can pass a variable to no problem. I have another class that I created in the script call otherclass.cs. How do I use the variable within otherclass.cs?
I tried to just do this:
_urlBase = Dts.Variables["User::URLBase"].Value.ToString();
But I get this error:
The name 'Dts' does not exist in the current context
I'm pretty new to C# and classes so I may not be asking the question correctly. Is what I'm asking even possible?
Thanks.
SOLUTION
Here is my solution to what I was trying to accomplish:
In ScriptMain.cs created new service like this:
OtherService ws = new OtherService(Dts.Variables["User::URLBase"].Value.ToString());
In the OtherService.cs I had this:
class OtherService
{
public OtherService(string urlBase)
{
_urlBase = urlBase;
//do other stuff
}
//other methods and stuff
}

When you add a C# Script Task in BIDS 2008, notice this line in the ScriptMain task that it generates for you:
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
This says that ScriptMain is a subclass of Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase. That base class (VSTARTScriptObjectModelBase) defines a member Dts of type Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel. This in turn has a member named Variables. That is what you are accessing when you type "Dts.Variables" in ScriptMain.
The class you define in "otherclass.cs" is not a subclass of VSTARTScriptObjectModelBase and therefore does not inherit its member Dts. That is why the compiler tells you "The name 'Dts' does not exist in the current context."
If otherclass needs access to a variable's value then as cfrag suggests you can pass this value in when you instantiate a member of your class or when you call the class member function that needs this value. If you will need to access multiple variables, consider something like this:
public otherclass(Microsoft.SqlServer.Dts.Runtime.Variables dtsVariables)
{
DtsVariables = dtsVariables;
// other stuff
}
private Microsoft.SqlServer.Dts.Runtime.Variables DtsVariables;
Now otherclass has an instance member named DtsVariables which its non-static members can use to access the Variables collection passed in when the object was created. You would instantiate it and call methods from ScriptMain like this:
otherclass oc = new otherclass(Dts.Variables);
string url = oc.MakeURL();
Inside otherclass.MakeURL(), you can use the instance variable DtsVariables like you would have used Dts.Variables in ScriptMain, e.g.:
public string MakeURL()
{
return DtsVariables["User::URLBase"].Value.ToString() + "/default.aspx";
}

Related

c# variable access from many different classes

What is the common way to access a variable in c#, that is used by many different functions and classes?
Should you just pass this variable to every class/function that needs it or should you better create a new instance of the class, that holds the variable and access it inside the class/function that needs to call the variable?
It depends on on type of project and files hierarchy
One way is to create one Base class and inherit from it to every class your using
class BaseController
{
string connectionString = "..." //this is your often used variable
}
class UsersController : BaseController
{
Connect(connectionString);
//rest of class
}
class GroupsController : BaseController
{
Connect(connectionString);
//rest of class
}
Another way is to create class with static varibles if you can and call them without class instance
class MyVariables
{
static string connectionString = "..." //this is your often used variable
}
And call it like this
Connect(MyVariables.connectionString);
I think The second option is better

Why member access modifier does matter for nameof()?

I'm a little bit confused with nameof() operator. So for example I can't use class's private fields in nameof() in another class, but I can use public non static fields using non static property, so I don't need instantiated object.
Is it consistently? Why member access modifier does matter for nameof()?
class A
{
private int X;
public int Y;
public A()
{
var x = nameof(A.X);//OK
var y = nameof(A.Y);//OK
}
}
class B
{
public B()
{
var x = nameof(A.X);//Compilation error
var y = nameof(A.Y);//OK
}
}
The purpose of access modifiers like private is to hide the implementation details. They are saying "Nope, you don't need to know this. This is implementation detail". That's why nameof is not allowed to access private properties. Whatever class you are in, that class should not know about the implementation details of some other class.
Static vs non-static is different. Its purpose is not to hide something that you don't need to know about. Its purpose is just to distinguish between members that belongs to instances of the class and members that belongs to the class itself. All you want here is just the name of that member, which requires no instances to be created, so why disallow you? Note that the member is accessible i.e. it's not something you shouldn't know about.
Field X in class A is private. door is locked, you cant access it no matter what you do.
This is not a nameof problem, its Access Modifier problem
Access Modifiers (C# Programming Guide)
All types and type members have an accessibility level, which controls
whether they can be used from other code in your assembly or other
assemblies. You can use the following access modifiers to specify the
accessibility of a type or member when you declare it:
and
public The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or struct.
protected The type or member can be accessed only by code in the same class, or in a class that is derived from that class. internal
The type or member can be accessed by any code in the same assembly,
but not from another assembly.
protected internal The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class
in another assembly.
private protected The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is
derived from that class.
Not a direct answer to your question, but I usually get around this but using a static helper class:
class A
{
public static class Properties
{
public const string X = nameof(A.X);
}
private string X { get; }
}
Then using
A.Properties.X
It's a little bit more verbose, but still enables refactoring tools to work effectively.

How can I access a list that is created in a static object that is part of another class?

What I would like to do is to have a list that is in an object counts that is defined in the App class as a static when my application starts:
Here's the object:
public class Counts
{
public Counts()
{
public static List<CntQty> CardClicks2m;
}
}
In my application I declare this
am using the following code:
public partial class App : Application
{
public static Counts counts = new Counts();
public App()
{
}
}
Now I try to use load some data into the list but it gives me the error below. Note that this function is in another class.
public void GetClickHistory()
{
App.counts.CardClicks2m = db2.Query<CntQty>(sql);
The last line of the code is giving me an error saying
counts.CardClicks2m cannot be accessed with an instance reference;
qualify it with a type name instead.
I have tried a few different ways to make this work. One by removing the static and creating the object in the app constructor. This also didn't seem to work so I am hoping someone can point me in the right direction or at least suggest something.
i guess scope of variable is not right , you need to do like this
public class Counts
{
public static List<CntQty> CardClicks2m;
public Counts()
{
}
}
The error is pretty self explanatory, you cannot access a static property using an instance variable. all you need to do is use the typename. So this:
App.Counts.CardClicks2m
Becomes this:
Counts.CardClicks2m
You may need to specify the full namespace of the Counts class:
Some.Namespace.Counts.CardClicks2m
The error you are receiving is because you are trying to access to a static member from a instance object
public void GetClickHistory()
{
App.counts.CardClicks2m = db2.Query<CntQty>(sql);
EDITED after comments:
Try this (accessing the static member from the class):
public void GetClickHistory()
{
Counts.CardClicks2m = db2.Query<CntQty>(sql);
As CardClicks2m is declared static, it must be accessed from the class scope. If Counts class doen't have any other code, you can declare
public static class Counts
And there is not neccesary to create an instance of counts
public static Counts counts = new Counts(); //this is not neccesary
As your CardClicks2m-member is static there´s no need to have any instance of your Counts- or aven your App-class. The member exists once per appdomain however. Having said this in order to access CardClicks2m you don´t have to create an instance of your Counts-class within App.
Use this instead:
class MyClass
{
void DoSometjing()
{
Counts.CardClicks2m = db2.Query<CntQty>(sql).ToList();
}
}
Be aware to that Query will surely not return a List<CntQty>, but an IQueryable<CntQty>, that´s why you should call ToList afterwards.
Furthermore you can´t declare a member within a method or constructor. Thus declare it within the class´-body instead of the constructor:
public class Counts
{
public static List<CntQty> CardClicks2m;
public Counts() { /* any further initialzation */ } }
}

Project is not recognizing constructor contained in a different assembly

I have one assembly that looks like this:
namespace AssemblyOne
{
class MyFirstClass
{
public MyFirstClass(String param)
{
// Assign stuff
}
}
}
Inside another assembly, I am trying to create an instance of this class. So, naturally, I have tried this:
namespace AssemblyTwo
{
public partial class SomeForm : Form
{
private MyFirstClass mfcObject = new MyFirstClass("Some String"); // Error here.
}
}
I have added the other project as a reference and inserted the necessary using statement. However, the line above where I create this object is giving a compiler error:
'AssemblyOne.MyFirstClass' does not contain a constructor that takes 1 arguments.
This works fine when the two are in the same assembly. Why is it not recognizing the constructor?
Because MyFirstClass should be declared as public. Modify your code to become:
public class MyFirstClass
Otherwise, it defaults to be internal

Accessing methods from another class in C#

I have a number of classes in a Classes file, and I want them all to be able to access the same, global method to save duplicating code. Problem is, I can't seem to access a method from another class in my file - any ideas?
So my class1.cs layout is similar to this:
public class Job1
{
public Job1()
{
}
}
public class Methods
{
public static void Method1()
{
//Want to access method here from Job1
}
}
You'll need to specify the class they are in. Like this:
public Job1()
{
Methods.Method1()
}
If the class Job1 is in a different namespace from Methods then you'll need to either add a using clause, or specify the the namespace when calling the method. Name.Space.Methods.Method1()
Actually. Public Job1(){} is a constructor and not a method. It can be called from main class by creating object form the JOB1 class. Here add the following code:
public static void method1()
{
Job1 j1=new Job1();
}
constructor can be invoked by creating a object to the corressponding class....
To access methods of other classes, the methods must be static with a public Access modifier.
static - Not bound to an instance of the class but shared by all other instances.
private - data can only be accessed from inside the same class.
public - data can be accessed from other classes but must be referenced.

Categories

Resources