converting vb.net code to c#
-
Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju
-
Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju
You've already asked this question not more than an hour ago - be patient. See my reply, because you seem to be missing a key feature of the .NET Framework - the fact that it doesn't matter in what language the source was written because the assemblies all contain IL (unless they're mixed mode, which currently only MC++ supports (from the Microsoft camp)).
Microsoft MVP, Visual C# My Articles
-
Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju
Besides, do you maybe want to put this in context? What are you trying to do? Extract a value from a collection? If all you did was convert from one language to another, you're constrained by what the rest of the code did. Now, if you convert the concept, you could use many types to do this. For example, you could use a
Hashtable
orStringDictionary
(or several other things in the BCL) to store string key/value pairs and refer to them by name:StringDictionary dict = new StringDictionary();
dict["One"] = "Apple";
dict["Two"] = "Orange";
dict["Three"] = "Banana";
Console.WriteLine(dict["Two"]); // Prints Orange to the console.When moving from one application framework to another, don't just convert your code. Advancements are made with newer frameworks. If all you're doing is porting your code just to run on the .NET Framework, you're doing a very stupid thing (any article by a decent author will tell you as much) - if it works, don't change it. If you have to change it to add new functionality later and to be maintainable by less-than-knowledge programmers, then change it in steps.
Microsoft MVP, Visual C# My Articles
-
Hi, can anyone help me converting vb.net code given below into c#. I tried some converting tools but it will put the same code in c# and will give reference to Microsoft.VisualBasic.Compatibility, but i don't want that. i want the c# code for this method without using the reference to Microsoft.VisualBasic.Compatibility, also, if i have .dll project, how do i convert the .dll executable at the same time.. i appreciate ur help. Thanks... vb.net code ------------- Private Function GetSingleProperty(ByRef sElementName As String) As String Dim vValues As Object vValues = g_pPropSet.GetProperty(sElementName) If IsNothing(vValues) Then GetSingleProperty = "" ElseIf VarType(vValues(LBound(vValues))) <> VariantType.String Then GetSingleProperty = "" Else GetSingleProperty = vValues(LBound(vValues)) End If End Function vijju
My VB is a bit rusty but I think this is your answer.
private string GetSingleProperty(string elementName)
{
// g_pPropSet must be defined to the entire class
// GetProperty must be a defined method within this objectobject Values = g_pPropSet.GetProperty(elementName);
if (Values == null)
return String.Empty;
if (Values.GetType() != typeof(String))
return String.Empty;
return (string)Values;
} -
You've already asked this question not more than an hour ago - be patient. See my reply, because you seem to be missing a key feature of the .NET Framework - the fact that it doesn't matter in what language the source was written because the assemblies all contain IL (unless they're mixed mode, which currently only MC++ supports (from the Microsoft camp)).
Microsoft MVP, Visual C# My Articles
Hi Heath, thanks for replying. actually i am not very good at vb, but i am a little comfortable programming with c#. that is one reason i wanted to convert that code into c#. also, i wanted to know more about coupling and de-coupling, if i want to run my .dll project (which is going to be plug-in) as an executable at the same time. if you suggest any examples or material to follow-up the understand the architecture design, that will be GREAT. and also i didn't knew that my question was posted more than one time, and sorry about that.. Thanks vijju
-
My VB is a bit rusty but I think this is your answer.
private string GetSingleProperty(string elementName)
{
// g_pPropSet must be defined to the entire class
// GetProperty must be a defined method within this objectobject Values = g_pPropSet.GetProperty(elementName);
if (Values == null)
return String.Empty;
if (Values.GetType() != typeof(String))
return String.Empty;
return (string)Values;
}That would still require the Microsoft.VisualBasic.Compatibility assembly, since whatever Type
g_pPropSet
seems to be from there (since everything else is defined in the BCL assemblies). He doesn't want to reference that assembly, even though it really doesn't matter.Microsoft MVP, Visual C# My Articles
-
Hi Heath, thanks for replying. actually i am not very good at vb, but i am a little comfortable programming with c#. that is one reason i wanted to convert that code into c#. also, i wanted to know more about coupling and de-coupling, if i want to run my .dll project (which is going to be plug-in) as an executable at the same time. if you suggest any examples or material to follow-up the understand the architecture design, that will be GREAT. and also i didn't knew that my question was posted more than one time, and sorry about that.. Thanks vijju
Coupling and decoupling has nothing to do with how code is executed - it has to do with how code is referenced. Knowing the C# language doesn't mean you know .NET. If you want to learn more, I suggest you read the SDK, which should be installed on your machine (it is installed by default with VS.NET, but can be installed separately) and available online at http://msdn.microsoft.com/library/en-us/dnanchor/html/netfxanchor.asp[^]. Understanding at least the basic concepts of the CLR (like what assemblies are) is important. Since you didn't tell me what
g_pPropSet
is defined as, I can only assume it's defined in the Microsoft.VisualBasic.Compatibility assembly. As I mentioned before - converting the code isn't a good idea, especially if you're new to .NET; you'll never learn anything that way. So, just converting the code to C# won't get rid of the assembly dependency - you'll have to rewrite it to use a similar class. Judging by what you seem to be trying to do, a suitable class might be theStringDictionary
as I mentioned in my other reply. If you want to know more about architecting .NET solutions, visit http://msdn.microsoft.com/patterns[^].Microsoft MVP, Visual C# My Articles